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

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/

    View Slide

  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

    View Slide

  3. About Melanie
    2011

    View Slide

  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

    View Slide

  5. Git 101
    git push
    git commit
    git add
    git pull

    View Slide

  6. Git 101
    git push
    git commit
    git add
    git pull

    View Slide

  7. Git 101
    git push
    git commit
    git add
    git pull

    View Slide

  8. Git 101
    git push
    git commit
    git add
    git pull
    Local repository
    Stage
    Workspace
    Remote repository

    View Slide

  9. Git basics
    Local repository
    Stage
    Workspace
    Remote repository

    View Slide

  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

    View Slide

  11. Git 101
    git push
    git commit
    git add
    git pull
    Local repository
    Stage
    Workspace
    Remote repository

    View Slide

  12. 12
    What is a commit, actually?

    View Slide

  13. Git push fails, git pull results in conflicts
    git push
    git commit
    git add
    Local repository
    Stage
    Workspace
    Remote repository
    git pull
    git mergetool

    View Slide

  14. View Slide

  15. Stashes
    git stash
    git push
    git commit
    git add
    Local repository
    Stage
    Workspace
    Remote repository
    git stash pop git stash
    Stash
    git pull
    -u ==
    --include-untracked

    View Slide

  16. Branches
    git branch
    itty-feature
    big-feature
    * master
    HEAD
    -a == all
    -r == remote
    --merged
    time
    commit

    View Slide

  17. Switching between branches
    git checkout git checkout -
    * itty-feature
    big-feature
    master

    View Slide

  18. A new branch
    git checkout -b git merge master
    itty-feature
    * big-feature
    master

    View Slide

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

    View Slide

  20. History
    git log
    --graph
    --oneline

    View Slide

  21. Don‘t lose your HEAD

    View Slide

  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

    View Slide

  23. Reflog is your friend
    git reflog

    View Slide

  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

    View Slide

  25. When you
    fucked up…
    Keep calm and get help

    View Slide

  26. Started working in master

    View Slide

  27. Started working in master
    git stash
    git checkout
    git stash pop
    git commit –a
    git push
    or create a new one with “-b”, then you don’t
    need to stash

    View Slide

  28. Retrieving stash causes conflicts

    View Slide

  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

    View Slide

  30. Stash is super old

    View Slide

  31. 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

    View Slide

  32. Typo in branch name

    View Slide

  33. Typo in branch name
    git branch –m rename branch

    View Slide

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

    View Slide

  35. Staged a file, but it needs to be ignored
    git rm --staged removes file from tracking but leaves the file
    untouched on disk

    View Slide

  36. Typo in commit message

    View Slide

  37. Typo in commit message

    View Slide

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

    View Slide

  39. Forgot to commit a file

    View Slide

  40. Forgot to commit a file
    git add
    git commit --amend uses the previous commit message

    View Slide

  41. Undo all this amending

    View Slide

  42. Undo all this amending
    git reset --soft HEAD@{1}
    git commit –C HEAD@{1} -C: reuse log message, author, timestamp

    View Slide

  43. Delete the branch

    View Slide

  44. Delete the branch
    git branch –d
    git branch –D
    git push origin :
    delete branch
    force delete branch. Like rm -rf
    also remote

    View Slide

  45. Intermission
    Coffee break for your brain

    View Slide

  46. Committed broken code and pushed it

    View Slide

  47. 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.

    View Slide

  48. Committed and pushed to wrong branch

    View Slide

  49. 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

    View Slide

  50. Committed in detached HEAD

    View Slide

  51. Committed in detached HEAD
    git checkout -b
    git merge origin
    now you can push like in any other branch
    adds current state of branch to latest changes

    View Slide

  52. Don’t want to deal with merge conflicts now

    View Slide

  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

    View Slide

  54. Need files from another branch

    View Slide

  55. Need files from another branch
    git checkout --
    git checkout -- /*
    copy file without merging
    copy all files in folder

    View Slide

  56. Need a single commit from another branch

    View Slide

  57. 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

    View Slide

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

    View Slide

  59. 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

    View Slide

  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

    View Slide

  61. Accidentally used hard reset,
    last commit is gone

    View Slide

  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
    shows moving to HEAD~1 plus last commit

    View Slide

  63. Someone named branch like an existing file

    View Slide

  64. 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

    View Slide

  65. Danger Zone
    Changing history

    View Slide

  66. Committed and pushed a password
    Changing history

    View Slide

  67. Committed and pushed a password
    git filter-branch --force --index-filter
    'git rm --staged --ignore-unmatch ’
    --prune-empty --tag-name-filter cat -- --all
    echo "" >> .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)

    View Slide

  68. Committed and pushed a password
    Nuclear option

    View Slide

  69. 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

    View Slide

  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

    View Slide

  71. Configs
    Have it your way

    View Slide

  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)$ '

    View Slide

  73. Autocomplete Git
    .bash_profile
    curl http://git.io/vfhol > ~/.git-completion.bash
    && echo '[ -f ~/.git-completion.bash ] && . ~/.git-completion.bash'
    >> ~/.bash_profile
    Tab

    View Slide

  74. ZSH prompt

    View Slide

  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)

    View Slide

  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

    View Slide

  77. 78

    View Slide

  78. [email protected]
    @mgapatrick
    PHP developer
    Admin and Core Tools Team
    Melanie G. A. PATRICK
    https://speakerdeck.com/mgapatrick/git-how-to-unfuck

    View Slide

  79. View Slide