Thursday, December 27, 2012

Git Merge and Stash


When you are in the middle of something, you learn that there are upstream changes that are possibly relevant to what you are doing. Two ways to solve it

Method 1:


PRE-MERGE CHECKS
Before applying outside changes, you should get your own work in good shape and committed locally, so it will not be clobbered if there are conflicts. git pull and git merge will stop without doing anything when local uncommitted changes overlap with files that git pull/git merge may need to update.
To avoid recording unrelated changes in the merge commit, git pull and git merge will also abort if there are any changes registered in the index relative to the HEAD commit. (One exception is when the changed index entries are in the state that would result from the merge already.)
Warning: Running git merge with uncommitted changes is discouraged: while possible, it leaves you in a state that is hard to back out of in the case of a conflict.


After commit, you can do a git pull or merge.
If seeing a conflict, you can do two things:
  • Decide not to merge. The only clean-ups you need are to reset the index file to the HEAD commit to reverse 2. and to clean up working tree changes made by 2. and 3.; git merge --abort can be used for this.
  • Resolve the conflicts. Git will mark the conflicts in the working tree. Edit the files into shape and git add them to the index. Use git commit to seal the deal.

$ git commit
$ git pull
$ git add
$ git commit


Method 2:


Pulling into a dirty tree
When you are in the middle of something, you learn that there are upstream changes that are possibly relevant to what you are doing. When your local changes do not conflict with the changes in the upstream, a simplegit pull will let you move forward.
However, there are cases in which your local changes do conflict with the upstream changes, and git pull refuses to overwrite your changes. In such a case, you can stash your changes away, perform a pull, and then unstash, like this:
$ git pull
 ...
file foobar not up to date, cannot merge.
$ git stash
$ git pull
$ git stash pop
 that basic stash support is now available in the "Git Repositories" view.
git stash ["Stash Changes" operation on right-click menu]
git stash list [the stashes are listed directly in this view under "Stashed Commits"]
git stash apply ["Apply stashed changes" from the right-click menu of items in "Stashed Commits"]
git stash show [double-click a listed stash to see it in a commit view]

No comments:

Post a Comment