2

I wanted to fetch a single remote branch and then rebase my current working branch against that as I am sharing it with someone. Usually I would just do:

git fetch

git rebase origin/branch_im_working_on

That seems to work ok but it appears to fetch all branches from the remote repository. So I looked around and found this:

git fetch origin branch_im_working_on

When I do this git tells me it fetched HEAD and then try to do:

git rebase origin/branch_im_working_on

git tells me that I am up to date and there is nothing to do even though I know there are changes pushed to remote.

If I try to do:

git rebase origin branch_im_working_on

I get a lot of merge conflicts so had to resort back to the original commands to get my branch up to date:

git fetch

git rebase origin\branch_im_working_on

Can someone help me understand what is happening here?

13
  • git rebase origin\branch_im_working_on will rebase your local with its counterpart on origin. SO if both are in sync then there is nothing to do. Are you sure that fetch happened successfully? And that the origin version is head of yours? Commented Nov 24, 2017 at 8:53
  • I thought it did because it told me it fetched HEAD but maybe it didnt actually fetch anything? I guess omitting the 'origin' from git fetch and just using the branch name alone is the same? Commented Nov 24, 2017 at 8:57
  • Well, if you have just one remote which is origin, you shouldn't have any problems, you can check it with git remote -v. That will list the remotes you have. Also you can check what you fetched, by looking at the log of origin/your_branch Commented Nov 24, 2017 at 9:00
  • 1
    git fetch origin <branch> && git rebase FETCH_HEAD. Commented Nov 24, 2017 at 9:00
  • @ElpieKay, But, FETCH_HEAD should be the same as refs/remotes/origin/his_branch/HEAD right? Commented Nov 24, 2017 at 9:02

1 Answer 1

4

Differences between git fetch and git fetch origin

  • If the git repo only has one remote, origin (you can check remotes by git remote -v), the two commands work the same.
  • If the git repo contains more than one remote, such as origin and upstream. git fetch will fetch all the changes from both remotes. git fetch origin will only fetch the changes from the remote origin.

Besides, if you only want to fetch a certain branch from a remote, you can use git fetch remotename branchname. For example, git fetch origin branch_im_working_on will only fetch the changes from the origin remote for the branch_im_working_on branch.

Rebase your local changes on the top on the remote branch:

Assume the commit history looks as below after fetching:

…---A---B---C---D  branch_im_working_on
         \
          E---F   origin/branch_im_working_on

If you want your local changes (commit C and commit D) on top of the origin/branch_im_working_on (latest commit), any one of the below commands can work:

git rebase origin branch_im_working_on
git rebase origin/branch_im_working_on
git fetch origin branch_im_working_on --rebase

Then the commit history will be:

…---A---B---E---F---C---D  branch_im_working_on
                |
     origin/branch_im_working_on

But the command git rebase origin\branch_im_working_on will not work (for Windows OS) since origin\branch_im_working_on is not a valid branch (neither local branch nor tracking branch).

Sign up to request clarification or add additional context in comments.

7 Comments

"But the command git rebase origin\branch_im_working_on can not be work since origin\branch_im_working_on is not a valid branch" everything else you said makes sense except this. I just did that and my branch IS now rebased against origin, further more I just tried git log origin/feature/my_branch and I get the git log for it, minus my rebased changes which are applied to the top of my tracking feature branch of the same name.
What's the OS do you use? At least \ and / are sensitive for windows. If you are using windows, you will find the command git rebase origin\branchname is invalid.
The operating system is Centos
I get what you mean now, I just flipped the slash as I dont use windows!
"git fetch will fetch all the changes from the remotes origin and upstream. git fetch origin will only fetch the changes from remote origin" - this is incorrect, the documentation says: "When no remote is specified, by default the origin remote will be used, unless there’s an upstream branch configured for the current branch."
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.