简体   繁体   中英

Git: Managing Branches

I want to "sync" a remote branch up with head, so that when I finally merge it, it won't be a headache. Thus, I want to try pulling head changes into my branch to see how different it is.

How can I accomplish the following workflow in Git?

  1. checkout a remote branch.
  2. Once I check it out, pull changes from head into it.
  3. edit the branch some
  4. push the branch, which now is pretty similar to HEAD, back to the remote version of the same branch (without affecting head).

Any tips on a better workflow that accomplishes the same thing would be very useful.

This is all pretty basic stuff:

# make sure your notion of the remote is up to date
git fetch origin
# create and check out a branch, at the same place as the remote master branch
git checkout -b origin-master origin/master
# merge your local master
git merge master
# test, edit away, hack hack hack
git add ...
git commit ...
# push back to origin
git push origin origin-master:master

Terminology notes:

  • Pulling is a combination of fetching and merging. When you're operating with local branches, there's no need to fetch, so you're merging, not pulling.

  • HEAD doesn't mean what you think it means. (Maybe you're a cvs/svn person.) HEAD is simply the currently checked-out commit (usually referred to via a branch name). So you're not merging HEAD, you're merging that branch. I called it master here.

As for your question about better workflows to do the same thing... well, that's pretty hard to answer. Your goals are a little ambiguous. You said "slowly" sync up and refer to "finally merging it", but the steps you outline do it all at once, so... well, it's all merged. There's nothing to do later. If you wanted to do it incrementally, you could simply repeat the steps I gave, picking intermediate commits in the history to merge each time. It's also a little unclear which direction you want the merging in. Maybe you actually wanted to start from your branch, and merge remote stuff into it?

 git checkout -b master-merging master
 git fetch origin
 git merge origin/master
 # test, hack, commit, push...
 git push origin master-merging:master

Or with incremental merges:

 git checkout -b master-merging master
 git fetch origin
 git merge origin/master~20    # 20 commits before origin/master
 # test, hack, commit
 git merge origin/master~10    # 10 commits before origin/master
 # test, hack, commit
 git merge origin/master
 # test, hack, commit, push...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM