开发者

Pulling git changes into a branch that hasn't been checked out

I have a git repository that contains two branches, 1.0 and master. If I want to work on 1.0 I clone it using the following command:

git clone ssh://user@server/project 开发者_运维知识库-b 1.0 project-1.0

And all is well. If I issue a git branch command I see:

1.0

git branch -a looks like this:

* 1.0
  remotes/origin/1.0
  remotes/origin/HEAD -> origin/master
  remotes/origin/master

However, if someone else pushes changes to master and I make changes to 1.0 and attempt to push, it will fail because I need to update my repo with the master changes. To do this I checkout master, pull the changes and then switch back to 1.0:

git checkout master
git pull
git checkout 1.0

However, is there a simpler way I can pull the latest changes without having to checkout master first?


If you use git fetch origin, then all the remote tracking branches under the origin remote will be updated. This won't change any of "your" branches, but you can then merge from any of the remote tracking branches into your 1.0 branch.

In the simplest terms, a git pull is essentially a git fetch followed by a git merge.


In this special case, when master can be fast-forwarded to origin/master, I do git push . origin/master:master (Yes, it's a dot '.').


According to your git branch -a, you don’t have a local branch named master. Thus, git log master should not work. A remote tracking branch — namely, origin/master — is always updated when you fetch. So if you want to see the log of the master branch from the origin remote, all you have to do is git fetch; git log origin/master. It sounds like you do not want to have your own version of master, thus git checkout master may have been a mistake which is just confounding your workflow.

However, if someone else pushes changes to master and I make changes to 1.0 and attempt to push, it will fail because I need to update my repo with the master changes.

This isn’t true. As long as no one has pushed divergent changes to 1.0, you can push to it. There’s nothing special about the master branch. You could use git branch -d master (which, if it has diverged from 1.0, will notify you that it has been merged to origin/master but not HEAD), since it seems like you’re not interested in maintaining your own version of master.

Again, someone pushing to master does not stop you from pushing to 1.0, and you do not need to checkout a local master simply to inspect origin/master.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜