330

This sounds so simple, but I just can't figure it out. I made an experimental branch a while ago, and now I'd like to pull in all the changes that happened on master since I made it. This is all local. I want to pull from local master into local my_branch, but I can't do it. This doesn't seem to work, telling me that master isn't a git repository:

git pull master
nulltoken
  • 64,429
  • 20
  • 138
  • 130
Phil Kulak
  • 6,960
  • 8
  • 45
  • 50

5 Answers5

514

You have to tell Git from where to pull, in this case from the current directory/repository (.):

git pull . master

But when working locally, you can simply use merge (pull internally calls merge):

git merge master
knittl
  • 246,190
  • 53
  • 318
  • 364
  • 3
    @JosiahYoder no, `pull .` specifically tells Git to pull from the repository located at `.` (i.e. the current directory/repository). `origin` is just a shorthand for "whatever repository location defined in `.git/config` file (usually set up automatically when cloning a repository) – knittl May 04 '19 at 23:24
  • 2
    Oh! So ```git pull . master``` would pull from the local repository instead of origin? (No fetch from origin!?) Is there any advantage of ```git pull . master``` over ```git merge master``` then? – Josiah Yoder May 06 '19 at 13:21
  • 4
    @JosiahYoder yes exactly, it would "pull" from the local repository instead of the origin repository. No fetching is done (because everything from the local repository is already here!). There's no advantage – both commands do more or less the same. If you are doing fast-forwards, you could use `push . origin/branch:branch` (not pull) to update local branches, without checking them out first. – knittl May 06 '19 at 17:47
  • 1
    I have tried both the options. "Git merge" works perfect but "git pull . master" just displays the following message but doesnt update the repo "$ git pull . master From . * branch master -> FETCH_HEAD Already up to date. " – Kiran Vedula Nov 28 '20 at 20:04
  • If you're looking to see the diffs of the branch merged in, you would have to `git reset HEAD~1` though. – glimmbo Aug 26 '22 at 00:28
58

What you are looking for is merging.

git merge master

With pull you fetch changes from a remote repository and merge them into the current branch.

KingCrunch
  • 128,817
  • 21
  • 151
  • 173
54

Quite old post, but it might help somebody new into git.

I will go with

git rebase master
  • much cleaner log history and no merge commits (if done properly)
  • need to deal with conflicts, but it's not that difficult.
techriften
  • 421
  • 2
  • 16
Lukino
  • 1,407
  • 13
  • 14
  • 4
    there are certainly many ways for a development team to manage their branching processes. Personally, I have been using git for 8 years, and have I never had to rebase. I always use merge, and it has always met my needs. Surely with rebase, just like merge, scenarios might arise where git can't automatically decide the correct outcome of "merging" two sets of changes. Regarding the point you made about a "cleaner" commit history. Personally, my preference is to maintain all commit history, just in case I ever need to do some "commit archaeology" (as often happens!) – user3441604 Jun 05 '20 at 15:38
  • I did git rebase master, do some changes, push to ogirin branch, and got rejected because it cannot fast-forward. So I have to force push `git push -f origin [branch]`, which I don't want. – Jeaf Gilbert Oct 14 '20 at 04:51
  • 1
    As explained in first comment, way how to proceed with merging have pros and cons. It depends on the size of the team, speed of development, etc. I use mixed approaches now as well. In your case your should've first pull, so steps would be `git checkout master`, `git pull origin master`, `git checkout bug-fix-branch`, `git rebase master` (do resolving merge conflicts), `git checkout master`, `git merge master` and finally `git push origin master`. – Lukino Oct 15 '20 at 06:57
  • @user3441604 Rebasing doesn't mean that you will lose any history. It's just that the commit will be aligned in a single line instead of parallel lines that can be hard to read. @Lukino Local branches (see OP) don't need this process you describe. And even then, you just need: `git pull --rebase origin master` and then `git push origin master`. With a correct config, you can do just `git pull && git push` – Sharcoux Mar 06 '21 at 12:50
  • @lukino I think you meant `git checkout master` followed by `git merge bug-fix-branch`. – Erwann Mar 22 '22 at 05:06
0

If you want to do the step 3 shown in the picture. Then follow the step below.

  1. git switch b (make sure you are in the same branch where you want to merge)
  2. git merge b ( this command indicating that you are merging b into a)
-3

If you are looking for a brand new pull from another branch like from local to master you can follow this.

git commit -m "Initial Commit"
git add .
git pull --rebase git_url
git push origin master
theRana
  • 704
  • 9
  • 10