Checking out branch created at home on work computer doesn't pull latest changes

时间:2018-04-18 18:10:59

标签: git github

I decided to get some work done at home yesterday on my personal computer.

Both my work and home computers have clones of repo work and are using fork and pull so git remote --v shows:

origin  git@github.com:username/work.git (fetch)
origin  git@github.com:username/work.git (push)
upstream    git@github.com:companyname/work.git (fetch)
upstream    git@github.com:companyname/work.git (push)

I created a branch at home with:

git checkout master                       # start from scratch
git pull --all; git pull upstream master; # alias I use to start fresh
git checkout -b new_branch                # new feature branch to do work on

I finished my work and used git push --set-upstream origin new_branch which fixes the warning I receive whenever I use git push for the first time after using git checkout -b in my situation.

I then initiated a pull request on github from username:new_branch to companyname:master to merge my work back into the remote master branch.

I went to work the next day and fired up my work computer and changes were requested to my PR so naturally I did:

git checkout new_branch

Which says new branch created to track changes. Now that can't be right because the branch already exists. I made it just last night. Do a lot of googling and then do the following:

git branch -a              # shows my branch as remotes/origin/new_branch
git branch -d new_branch   # remove the local branch with no changes in it
git checkout -b new_branch origin/new_branch # correctly check out remote branch and supposedly the changes

But here's where it's confusing. git log shows the commit I made last night as the most recent commit but the code I'm looking at doesn't reflect the changes of the latest commit in the log.

I've also tried:

git checkout origin/new_branch   # shows detached HEAD but no changes
git checkout sha1ofcommit        # shows detached HEAD but no changes
git checkout -b new_branch remotes/origin/new_branch # shows no changes

Any idea what's going on here?

1 个答案:

答案 0 :(得分:2)

  

其中说创建新分支以跟踪更改。现在这不是正确的,因为分支已经存在。我昨晚做了。

不,这是对的。分支机构是特定于回购的。您在家用计算机的回购中创建了它,然后将其推送到origin;但你的工作电脑还没有。然后你在工作中获取了,所以你有一个远程跟踪引用(origin/new_branch),告诉你new_branchorigin的位置;此仍然不是您工作计算机的本地存储库中的分支。

当你说git checkout new_branch时,git看到在一个遥控器中有一个new_branch的远程跟踪参考,所以然后它创建了相应的本地分支,指向与origin上的提交相同。此时您的代码将处于正确的状态。

然后你删除了本地分支并用-b重新创建了它;在这种情况下,git假设您的意思是创建一个新的本地分支,独立于任何远程可能存在的任何东西。因此,如果您在执行此操作时没有origin/new_branch提交,那么您所做的就是将new_branch移至错误的提交。

有几种方法可以解决这个问题。由于您可能希望正确设置跟踪,我说最简单的是

git branch -D new_branch
git branch new_branch

它将再次告诉你它正在创建一个新的分支,但是然后查看代码的状态。如果它不对,那么除了你迄今为止传达的内容之外还会发生其他事情。