Git pull origin <branch> overwrites master?
Asked Answered
T

1

26

I have a repository on Github, aav1

On my laptop I have two branches, one called master and one called vs12up The master branch was when the software was Visual Studio 2008, vs12up is converted to Visual Studio 2012.

On my laptop everything seems fine and I pushed the new branch to github, appears correct.

On my desktop I tried to pull the remote branch:

git pull origin vs12up

It wrote the changes to my master branch on the desktop, git log shows the commits made on the vs12up branch, but git branch only shows master, which is the current branch.

How can I revert the changes to the master branch and pull the vs12up branch on my desktop to match the repository on my laptop?

Tolland answered 10/5, 2013 at 1:28 Comment(0)
W
55

If you do a git pull with a remote branch name, it will fetch the remote branch and then merge it into your current local branch. So to undo that, you will first have to reset your local branch to the remote master, then create a new local vs12up branch from the corresponding remote branch.

  1. Reset your local master to match the remote repository's master (WARNING: be sure that you don't have any uncommitted changes you want to keep before issuing the following command):

    git reset --hard origin/master
    
  2. Fetch all remote branches into your local repository:

    git fetch origin
    
  3. Create a new local vsup12 branch from the remote vsup12 branch, and switch to this new local branch:

    git checkout -b vsup12 origin/vsup12
    

Note that when you subsequently just do a git pull while switched to the vsup12 branch, you'll fetch and merge the latest changes from the vsup12 branch on Github into your local vsup12

Watkins answered 10/5, 2013 at 1:43 Comment(6)
Thank you. Two questions, what does the -b argument do?Tolland
And do I just switch back by git checkout master?Tolland
The -b specifies the name of the new branch. git checkout -b foo is shorthand for creating a branch called foo (with git branch foo) and then switching to it (with git checkout foo).Watkins
On the desktop, after I followed instructions above git status returns # Your branch is ahead of 'aav1/vs12up' by 1 commit. - but it says everything is up to date, nothing to commit. Is this normal?Tolland
Assuming that your remote is really called aav1 and not origin, that would indicate that you have a new commit locally which is not yet in your remote repository. When doing the above, that'd not be normal. Maybe you did an inadvertent pull/merge in between? But I fear that's either a separate question or something for chat.Watkins
Sorry - the remote is called origin on my laptop, aav1 on my desktop. I messed up on my naming, my mistake. I didn't do a merge, just the pull that I asked the question above. Everything seems up to date however. I'm going to make a small commit and compare the differences. Thank you for your help again.Tolland

© 2022 - 2024 — McMap. All rights reserved.