Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
software:git [2014/12/19 14:24]
cyril [Local repository management]
software:git [2024/11/06 22:20] (current)
cyril [Editing commits]
Line 40: Line 40:
 alias gitk='gitk --color-words' alias gitk='gitk --color-words'
 </code> </code>
 +
 +==== General tips ====
 +  * ''<commit-hash>'' is the hash of a commit, but it can also be just the few first characters as long as it is not ambiguous. 
 +
  
 ==== Local repository management ==== ==== Local repository management ====
Line 58: Line 62:
   * % **''git branch -D <branch-name>''** : delete the given branch (force even if not fully merged, -d otherwise)   * % **''git branch -D <branch-name>''** : delete the given branch (force even if not fully merged, -d otherwise)
   * % **''git merge <branch-name>''** : merge current branch with the given branch   * % **''git merge <branch-name>''** : merge current branch with the given branch
-  * % **''git rebase <branch-name>''** : rebase the branch on top of the given branch (if you have to resolve conflicts, do "''git add .''" and "''git rebase --continue''" once you resolved conflicts for a commit) +  * % **''git rebase <branch-name>''** : rebase the current branch on top of the given branch (if you have to resolve conflicts, do "''git add .''" and "''git rebase --continue''" once you resolved conflicts for a commit) 
-  * % **''git cherry-pick <commit-hash>''** : apply the changes introduced by a given commit to the current branch+  * % **''git cherry-pick <commit-hash>''** : apply the changes introduced by a given commit to the current branch (ie import the commit)
  
 ==== Collaborative repository management ==== ==== Collaborative repository management ====
Line 65: Line 69:
   * % **''git clone <repo-addr>''** : copy the distant repository (<repo-addr> can be "http://<repo>" or "ssh://<user>@<repo>")   * % **''git clone <repo-addr>''** : copy the distant repository (<repo-addr> can be "http://<repo>" or "ssh://<user>@<repo>")
   * % **''git pull [<repo-addr>]''** : update from the distant repository (fetch and merge)   * % **''git pull [<repo-addr>]''** : update from the distant repository (fetch and merge)
-  * % **''git pull --rebase [<repo-addr>]''** : instead of merging your pending commits with the new ones on master, rebase your pending commits on the top of master ; it creates a cleaner tree and avoids artificial "merged" commits. +  * % **''git pull --rebase [<repo-addr>]''** : instead of merging your pending commits with the new ones on master, rebase your pending commits on the top of master (fetch and rebase) ; it creates a cleaner tree and avoids artificial "merged" commits. 
-  * % **''git stash ; git pull|merge|rebase ; git stash apply''**: to pull or merge or rebase when you have non-commited modifications ; store the modifs, pull or rebase, apply the modifs back+  * % **''git stash ; git pull|merge|rebase ; git stash pop''**: to pull or merge or rebase when you have non-commited modifications ; store the modifs, pull or rebase, apply the modifs back.
   * % **''git push <repo-addr>''** : send to the distant repository (needs ssh clone and write permissions)   * % **''git push <repo-addr>''** : send to the distant repository (needs ssh clone and write permissions)
   * % **''git push origin <local-branch>[:<dist-branch>]''** : push a local branch into a new distant branch   * % **''git push origin <local-branch>[:<dist-branch>]''** : push a local branch into a new distant branch
Line 176: Line 180:
 <code> <code>
 git rebase -i <latest-commit-to-keep-unchanged> git rebase -i <latest-commit-to-keep-unchanged>
-# set "reword" if you want to change message +# set "r(reword) if you want to change message 
-# set "edit" if you want to amend the commit (git commit --amend; git rebase --continue) +# set "e(edit) if you want to amend the commit (git commit --amend; git rebase --continue) 
-# set "squash" if you want to merge with the previous commit+# set "s" (squash) if you want to merge with the previous commit and merging or editing the commit message 
 +# set "f" (fixup) if you want to merge with the previous commit without interaction
 # you can even reorder the commits! # you can even reorder the commits!
 </code> </code>
 +Tips with ''rebase -i'':
 +  * If you have a bunch of fixup commits, first move them one by one (i.e. move one, apply, rebase again, move another one, apply, etc), so that if you prefer to abort due to conflicts you don't lose the ones before, and then really mark them "f" (nothing can go wrong afterwards)
 +  * If you are just moving and merging some fixup commits, you may want to check that the end result is identical, by making a copy and then performing a diff.
 +  * If you move a fixup commit up and keep getting conflicts after its initial position, it means that you made a mistake in the conflict resolution
  
 +Change author or date: use ''rebase -i'', chose "edit", and commit with:
 +<code>
 +git commit --amend --author "Name <email-address>" --date "$(date -R)"
 +</code>
  
 +To split a commit: use ''rebase -i'', chose "edit", and reset the index to the commit before, and commit: 
 +<code> 
 +git reset --mixed @~ 
 +git add -p . 
 +git commit 
 +</code>
 ==== Archive generation ==== ==== Archive generation ====
  
Line 212: Line 230:
  
 [[http://www.dmo.ca/blog/20080307124544/]] [[http://www.dmo.ca/blog/20080307124544/]]
 +
 +==== Recovery ====
 +
 +For finding a lost commit you can use "git reflog".
 +
 +For finding a lost stash (eg "git stash drop") or file (eg "git rm"), you can try "git fsck --full", and then use "git show" with the hash to see the content of the objects.
 +
 +[[https://github.com/blog/2019-how-to-undo-almost-anything-with-git]]
software/git.1418999095.txt.gz · Last modified: 2014/12/19 14:24 by cyril
CC Attribution-Share Alike 4.0 International
Driven by DokuWiki Recent changes RSS feed Valid CSS Valid XHTML 1.0