Do not believe a thing because you read it in a book! Do not believe a thing because another has said it so! Find out the truth for yourself.

Bill Vaughan, according to the Internet

Don't believe all I say, RTFM.

Move your HEAD with Git

revert, checkout, reset

git revert

git revert

  • Cancels a commit by creating an opposite commit
  • Useful to "remove" commits already publicly published
  • Cancel live breaking merge

git revert

Format: git revert <git-object>

example: git revert 123abc

 

git revert

Revert merges

git revert

  • Special cases, merges have multiple parents
  • Which part of the merge to revert
  • In previous example: myBranch1, myBranch2 or both can be reverted
  • -m # option specify the parent to revert

Revert merges

git checkout

git checkout

  • Different behaviour depending on arguments
  • git object and path can be provided or just git object

git checkout [gitObject] path

  • Change state of given file 

git checkout [gitObject] path

  • Does NOT change HEAD
  • If no object given, discards any unstaged changes
  • If an object is given, changes the file to match the object. Stages the diff between the object and HEAD

git checkout gitObject

  • Change the position of HEAD and the state of the working tree
  • Links HEAD to targeted object
  • If object is NOT a branch, HEAD is detached
  • Used to switch between versions of the code
  • Does NOT change the state of branches

"cd" for git

git checkout gitObject

git checkout gitObject

git reset

git reset

  • Different behaviour depending on arguments
  • git object and path can be provided or just git object

git reset [gitObject] path

  • Set the staged state of given file at the state of the given object
  • Sets the modified state of the file to keep the file unchanged
  • Corollary: If no object is provided (HEAD used by default), the command unstages the file

git reset gitObject

  • Moves HEAD to the provided object
  • If HEAD was on a branch, moves the branch tip as well
  • makes the commits from the branch's old position potentially unaccessible (unless using reflog, that is)
  • Useful to "get rid of" the n last commits of a branch

git reset gitObject

git reset gitObject

git reset gitObject

git reset gitObject

What about changes in between?

 

  • git reset --soft : keep difference staged
  • git reset --mixed : keep differences in working tree (default)
  • git reset --hard : discard differences

Conclusion

Know what you want to do to know what to do

Conclusion

If you want to:

  • Cancel a published commit

git revert gitObject

Conclusion

If you want to:

  • Go somewhere in your commit history
  • Go to a specific branch

git checkout gitObject

Conclusion

If you want to:

  • Create a new branch and switch on it

git branch theBranch

git checkout theBranch

 

or

 

git checkout -b theBranch

Conclusion

If you want to:

  • remove local modifications in a file
  • change a file to a desired point in the history

git checkout [gitObject] path

Conclusion

If you want to:

  • move the tip of a branch to somewhere else (eg remove the last commits or take the remote version of a branch)

git reset --hard newPosition

Conclusion

If you want to:

  • lose all local changes (staged or not)
  • cancel an ongoing merge

git reset --hard

Conclusion

If you want to:

  • unstage your changes

git reset

Made with Slides.com