Git Fetch vs Git Fetch Origin
Git Fetch vs Git Fetch Origin
https://stackoverflow.com/questions/47469380/git-fetch-vs-git-fetch-origin
Asked 8 years, 1 month ago Modified 1 year ago Viewed 19k times 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?
gitversion-control
Share
Improve this question
Follow
edited Nov 24, 2017 at 9:31
asked Nov 24, 2017 at 8:43
berimbolo’s user avatar
berimbolo
3,9091212 gold badges5353 silver badges8989 bronze badges
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? –
Antho Christen
CommentedNov 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? –
berimbolo
CommentedNov 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 –
Antho Christen
CommentedNov 24, 2017 at 9:00
1
git fetch origin
Highest score (default) 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).
Share Improve this answer Follow edited Jan 6, 2025 at 16:30 PeterM’s user avatar PeterM 1533 bronze badges answered Nov 24, 2017 at 9:13 Marina Liu’s user avatar Marina Liu 38.4k55 gold badges6868 silver badges8080 bronze badges Sign up to request clarification or add additional context in comments.
7 Comments
berimbolo Over a year ago “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.
Marina Liu Over a year ago 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.
berimbolo Over a year ago The operating system is Centos
berimbolo Over a year ago I get what you mean now, I just flipped the slash as I dont use windows!
1615903 Over a year ago “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.”