Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Git - How to unfuck

Git - How to unfuck

Use Git on the command line – useful hints and how to undo common mistakes

Code for customizing the command prompt: https://gist.github.com/mgapatrick/8af91f342a2ab2ba940467bd693404b1

C58bef3994f8071a4c2cb6c34b7367f5?s=128

Melanie G. A. PATRICK

November 07, 2019
Tweet

More Decks by Melanie G. A. PATRICK

Other Decks in Technology

Transcript

  1. GIT: How to unfuck Use GIT on the command line

    – useful hints and how to undo common mistakes https://www.flickr.com/photos/kalexanderson/5421517469/
  2. 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
  3. About Melanie 2011

  4. “The command line is the only place you can run

    all Git commands.” https://git-scm.com/book/en/v2/Getting-Started-The-Command-Line
  5. Git 101 git push git commit git add <filename> git

    pull
  6. Git 101 git push git commit git add <filename> git

    pull
  7. Git 101 git push git commit git add <filename> git

    pull
  8. Git 101 git push git commit git add <filename> git

    pull Local repository Stage Workspace Remote repository
  9. Git basics Local repository Stage Workspace Remote repository

  10. Just looking around git diff git diff HEAD git diff

    origin git diff --staged git diff Local repository Stage Workspace Remote repository -w == --ignore-all-space
  11. Git 101 git push git commit git add <filename> git

    pull Local repository Stage Workspace Remote repository
  12. 12 What is a commit, actually?

  13. Git push fails, git pull results in conflicts git push

    git commit git add <filename> Local repository Stage Workspace Remote repository git pull git mergetool
  14. None
  15. Stashes git stash git push git commit git add <filename>

    Local repository Stage Workspace Remote repository git stash pop git stash Stash git pull -u == --include-untracked
  16. Branches git branch itty-feature big-feature * master HEAD -a ==

    all -r == remote --merged time commit
  17. Switching between branches git checkout <branchname> git checkout - *

    itty-feature big-feature master
  18. A new branch git checkout -b <branchname> git merge master

    itty-feature * big-feature master
  19. Going back in time itty-feature big-feature * master git checkout

    <SHA> git checkout HEAD
  20. History git log --graph --oneline

  21. Don‘t lose your HEAD

  22. 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 <branchname>
  23. Reflog is your friend git reflog

  24. 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
  25. When you fucked up… Keep calm and get help

  26. Started working in master

  27. Started working in master git stash git checkout <branchname> git

    stash pop git commit –a git push or create a new one with “-b”, then you don’t need to stash
  28. Retrieving stash causes conflicts

  29. Retrieving stash causes conflicts git stash pop git reset --soft

    HEAD if there are conflicts when you run “git stash pop”, Git will not drop the stash
  30. Stash is super old

  31. Stash is super old git stash list git stash show

    -p stash@{5} git stash branch <branchname> stash@{5} shows content of stash creates a new branch from the commit where stash was created, checks it out and retrieves the stash
  32. Typo in branch name

  33. Typo in branch name git branch –m <oldbranch> <newbranch> rename

    branch
  34. Staged a file, but it needs to be ignored

  35. Staged a file, but it needs to be ignored git

    rm --staged <filename> removes file from tracking but leaves the file untouched on disk
  36. Typo in commit message

  37. Typo in commit message

  38. Typo in commit message git commit --amend changes the last

    commit message
  39. Forgot to commit a file

  40. Forgot to commit a file git add <missing file> git

    commit --amend uses the previous commit message
  41. Undo all this amending

  42. Undo all this amending git reset --soft HEAD@{1} git commit

    –C HEAD@{1} -C: reuse log message, author, timestamp
  43. Delete the branch

  44. Delete the branch git branch –d <branchname> git branch –D

    <branchname> git push origin :<branchname> delete branch force delete branch. Like rm -rf also remote
  45. Intermission Coffee break for your brain

  46. Committed broken code and pushed it

  47. Committed broken code and pushed it git revert <SHA> 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.
  48. Committed and pushed to wrong branch

  49. Committed and pushed to wrong branch git checkout <wrong_branch> git

    reset --soft HEAD^ git stash git checkout <branchname> git stash pop git commit git push origin <branchname> git checkout <wrong_branch> git push --force origin <wrong_branch> 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
  50. Committed in detached HEAD

  51. Committed in detached HEAD git checkout -b <branchname> git merge

    origin <source_branch> now you can push like in any other branch adds current state of branch to latest changes
  52. Don’t want to deal with merge conflicts now

  53. Don’t want to deal with merge conflicts now git merge

    --abort undo the merge and already resolved conflicts, so you go back to your last commit
  54. Need files from another branch

  55. Need files from another branch git checkout <branch> -- <filename>

    git checkout <branch> -- <folder>/* copy file without merging copy all files in folder
  56. Need a single commit from another branch

  57. Need a single commit from another branch git checkout <another_branch>

    git log git checkout <branchname> git cherry-pick <SHA> returns list of commit hashes and messages apply the changes introduced by this commit
  58. Need changes from another branch, but must not commit them

  59. Need changes from another branch, but must not commit them

    git merge <other_branch> git reflog git reset --soft <SHA> git add -p go back before the merge, but keep changes in workspace only commit your changes using patch mode
  60. 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
  61. Accidentally used hard reset, last commit is gone

  62. 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 <SHA> shows moving to HEAD~1 plus last commit
  63. Someone named branch like an existing file

  64. Someone named branch like an existing file git checkout <xxx>

    git checkout <xxx> -- will checkout the file will checkout the branch “--” makes clear that the <xxx> part is a branch, everything after “--” must be a file path
  65. Danger Zone Changing history

  66. Committed and pushed a password Changing history

  67. Committed and pushed a password git filter-branch --force --index-filter 'git

    rm --staged --ignore-unmatch <file_path>’ --prune-empty --tag-name-filter cat -- --all echo "<file_path>" >> .gitignore git commit -a git push --force --all --v --dry-run git push --force --all git push --force --tags git for-each-ref --format='delete %(refname)' refs/original | git update-ref --stdin git reflog expire --expire=now --all git gc --prune=now removes specified file, as well as any empty commits as a result, overwrites existing tags add file to .gitignore try it out first collaborators need to rebase or check out anew remove dangling commits (after you’re sure everything still works)
  68. Committed and pushed a password Nuclear option

  69. Committed and pushed a password Nuclear option rm <filename> rm

    -rf .git/ delete repository and create a new one git init git commit . git remote add origin <remote URL> git push remove all Git info from your code manually on GitHub / Bitbucket / Gitlab etc. push code to a new repository
  70. 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
  71. Configs Have it your way

  72. Bash prompt .bash_profile function parse_git_dirty { [[ $(git status 2>

    /dev/null | tail -n1) != "nothing to commit, working directory clean" ]] && echo "*” } function parse_git_branch { git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e "s/* \(.*\)/[\1$ (parse_git_dirty)]/” } export PS1='\u@\h \[\033[1;33m\]\w\[\033[0m\]$(parse_git_branch)$ '
  73. Autocomplete Git .bash_profile curl http://git.io/vfhol > ~/.git-completion.bash && echo '[

    -f ~/.git-completion.bash ] && . ~/.git-completion.bash' >> ~/.bash_profile Tab
  74. ZSH prompt

  75. Configs git config --global ... core.excludesfile ~/.gitignore_global core.editor “atom --wait”

    merge.tool meld commit.template ~/.gitmessage.txt help.autocorrect 50 global gitignore, e.g., “.DS_Store” or “.idea/” set Atom as editor for commit messages set graphical tool for “git mergetool” set default commit message, e.g., “WEB-” accepts typos, e.g., “git pulll” (50 == 5sec)
  76. 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
  77. 78

  78. melanie.patrick@trivago.com @mgapatrick PHP developer Admin and Core Tools Team Melanie

    G. A. PATRICK https://speakerdeck.com/mgapatrick/git-how-to-unfuck
  79. None