Cjku/git: Difference between revisions

1,150 bytes added ,  29 October 2014
Line 379: Line 379:
# <span style="color:red">(__"Similar"__ to git-reset-hard)</span> Regenerate the index according to the tree of the new assigned commit, and update working directory. There are two places that git-reset-hard is different with git-checkout:
# <span style="color:red">(__"Similar"__ to git-reset-hard)</span> Regenerate the index according to the tree of the new assigned commit, and update working directory. There are two places that git-reset-hard is different with git-checkout:
#*First, git-reset-hard never abort, while git-checkout will.  
#*First, git-reset-hard never abort, while git-checkout will.  
#*Second, git-reset-hard compares the working directory and new tree, replace all modified tracked files in working directory by content in new tree; <span style="color:red">the rule of git-checkout is still a mystery for me. Surf on the internet, there is no document reveal git internal behavior. Still need time to figure out how merge_working_tree works. TBD.</span>
#*Second, git-reset-hard compares the working directory and new tree, replace all modified tracked files in working directory by content in new tree; if your change in working directory does not cause any conflicts in checkout process, git will not erase the change. If the change indeed causes conflicts, then checkout aborting. Please see the next section for more details


Scott Chacon/Ben Straub wrote a fantastic article with regards to reset, and deserves you spend time on: http://git-scm.com/blog/2011/07/11/reset.html
Scott Chacon/Ben Straub wrote a fantastic article with regards to reset, and deserves you spend time on: http://git-scm.com/blog/2011/07/11/reset.html
Line 400: Line 400:
   Switched to branch 'new_branch'  
   Switched to branch 'new_branch'  


How to resolve aborting? Different strategies for different situations
What is A "conflict"?
# Force checkout: if you don't change lose at all, submit "git checkout -f <branch>" bravely.
# Modify a file: you modify the content of a file, which exist in two branches
# Hard reset to HEAD: almost the same with previous one. Hard reset and checkout again.
#* If the content of this file is the same in two branches, no conflict. You change keeps stay in working directory after checkout done.
# git-merge --merge: If you want to bring what you change in working directory into the new branch, choose this one. With --merge option, a three-way merge between the current branch, your working directory, and the new branch is done, and you will be on the new branch. And for sure, you have to fix conflicts if any on the new branch.
#* If the content of this file is not the same in two branches, conflict and abort.
# Add a file: you add a new file in working directory, it doesn't cause any conflict, no matter you cache this file or not. If you already cache this file, it will still stay in cache after checkout done.
# Delete a file:
#* You git-rm a file in working directory and cache, which exists in two branch, conflict and abort.
#* You git-rm a file in working directory and cache, which exists in current branch only, no conflict.
#* You rm a file in working directory and the content of this file is the same in two branches, no conflict. The file you remove will not exist in working directory after checkout.
#* If you rm a file in working directory and the content of this file is not the same in two branches, no conflict. The file you remove will come back in working directory after checkout.
 
So, how to resolve aborting? Different strategies for different situations
# Force checkout: if you don't care change lose at all, use "git checkout -f <branch>" bravely.
# Hard reset: almost the same with previous one. Reset hardly and checkout again.
# Checkout merge: If you want to bring what you change in working directory into the new branch, choose this one. With --merge option, a three-way merge between the current branch, your working directory, and the new branch is done, and you will be on the new branch. And for sure, you have to fix conflicts if any on the new branch.
# Commit change: keep everything you change into object store by a new commit, then checkout again
# Commit change: keep everything you change into object store by a new commit, then checkout again
# Stash: you need to rush into another branch to fix emergency thing. You don't want to commit you change in the current branch without more analysis. Use git stash.
# Stash: you need to rush into another branch to fix emergency thing. You want to keep the context of current working directory, the index and the tree, use "git stash save" and "git stash pop".


==Branch==
==Branch==
Confirmed users
1,643

edits