Git undoMichal Zmuda
Agenda
Stories● undo local changes ● undo file changes to SHA● modify last commit● delete commit● undo local branch commits● reverse commit(s)● prepare and apply patch● undo rebase
undo local changes - tracked files
git reset --hard
* reset is reverse operation to git add* --hard - any changes to tracked files are discarded* light version for not staged files: git checkout .
undo local changes - untracked files
git clean -fd
* only untracked files are deleted* all untracked files and directories are deleted* -fdx option removes also ignored files
undo file changes to SHA
git checkout 816367 GrailsTask.groovygit commit -m "reverted GrailsTask.groovy"
modify last commit
git commit --amend
* file content or commit comment can be changed* last commit should not be pushed to remote repository before
delete commit
before: add few local commits
git rebase -iremove line with commit and exit vim (:wq)
* commits can be reordered, squashed and comments can be changed* delete commits only when they are not pushed to remote repository* if you remove everything, the rebase will be aborted
undo local branch commits
git reset --hard origin/master
* origin/master - remote branch
Reverse commit(s)
git revert 9973c1a --no-commit
* can be used to revert remote changes* --no-commit - use to preview changes* to revert range of revisions: 52ea36..0bbc55
Prepare and apply patch
Prepare patchgit diff LATEST_SHA OLDER_SHA > mypatch.patchgit diff 0bbc55 52ea36d > mypatch.patch
Apply patchgit apply mypatch.patch
* can be used to revert remote changes
undo rebase
add 2 commits and rebase interactive with squash and edit comments
Select history entrygit reflog
git reset --hard HEAD@{3}
* use only for local changes
Thanks