If you already know something, don’t feel bored – something new will come along! If you are a beginner, don’t try to understand everything – slides are online and can be used as cheat sheets Personal style and experience, real life examples – there’s more than one way to do things Disclaimer
references the commit 1 before HEAD references to the 1st parent of HEAD history of movement of HEAD, can be seen in git reflog reattach HEAD to tip of branch Don’t lose your HEAD git checkout HEAD~1 git checkout HEAD^1 git checkout HEAD@{1} git checkout
Moving the HEAD around git reset --hard HEAD@{1} git reset --soft HEAD@{1} removes one commit and all work – all uncommitted changes are lost forever undoes one commit and puts the work from that commit back in the workspace
Stash is super old git stash list git stash show -p stash@{5} git stash branch stash@{5} shows content of stash creates a new branch from the commit where stash was created, checks it out and retrieves the stash
Committed broken code and pushed it git revert git commit Creates a new commit that's the inverse of the given hash. Anything removed in the old commit will be added in the new commit. Anything added in the old commit will be removed in the new commit.
Committed and pushed to wrong branch git checkout git reset --soft HEAD^ git stash git checkout git stash pop git commit git push origin git checkout git push --force origin make sure you’re in the wrong branch reset the branch back one commit you will need to rewrite the message force push commit deletion to original branch
Need a single commit from another branch git checkout git log git checkout git cherry-pick returns list of commit hashes and messages apply the changes introduced by this commit
Need changes from another branch, but must not commit them git merge git reflog git reset --soft git add -p go back before the merge, but keep changes in workspace only commit your changes using patch mode
Don’t want to commit all changes in one file git add -p Stage this hunk [y,n,q,a,d,/,j,J,g,e,?]? patch mode y - yes, stage this hunk n - no, do not stage this hunk ? - print help
Accidentally used hard reset, last commit is gone https://philna.sh/blog/2017/01/04/git-back-to-the-future/ git reflog git reset --hard shows moving to HEAD~1 plus last commit
Someone named branch like an existing file git checkout git checkout -- will checkout the file will checkout the branch “--” makes clear that the part is a branch, everything after “--” must be a file path
Committed and pushed a password Nuclear option rm rm -rf .git/ delete repository and create a new one git init git commit . git remote add origin git push remove all Git info from your code manually on GitHub / Bitbucket / Gitlab etc. push code to a new repository
My Git history is different from the one I pulled git fetch origin git rebase origin/master git stash git fetch origin git reset --hard origin/master git stash pop delete folder and clone again when someone else changed the history when you have local changes you want to keep when you prefer a clean slate
Use the command line, Luke Al says: commit early, commit often Try not to push when you notice your error Keep calm and almost anything can be undone Takeaways