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

More Decks by Melanie G. A. PATRICK

Other Decks in Technology


  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. “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
  4. Git 101 git push git commit git add <filename> git

    pull Local repository Stage Workspace Remote repository
  5. 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
  6. Git 101 git push git commit git add <filename> git

    pull Local repository Stage Workspace Remote repository
  7. 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
  8. 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
  9. 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>
  10. 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
  11. 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
  12. 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
  13. 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
  14. Staged a file, but it needs to be ignored git

    rm --staged <filename> removes file from tracking but leaves the file untouched on disk
  15. Forgot to commit a file git add <missing file> git

    commit --amend uses the previous commit message
  16. Undo all this amending git reset --soft HEAD@{1} git commit

    –C HEAD@{1} -C: reuse log message, author, timestamp
  17. 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
  18. 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.
  19. 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
  20. 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
  21. 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
  22. Need files from another branch git checkout <branch> -- <filename>

    git checkout <branch> -- <folder>/* copy file without merging copy all files in folder
  23. 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
  24. 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
  25. 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
  26. 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
  27. 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)
  28. 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
  29. 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
  30. 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)$ '
  31. Autocomplete Git .bash_profile curl http://git.io/vfhol > ~/.git-completion.bash && echo '[

    -f ~/.git-completion.bash ] && . ~/.git-completion.bash' >> ~/.bash_profile Tab
  32. 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)
  33. 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
  34. 78

  35. [email protected] @mgapatrick PHP developer Admin and Core Tools Team Melanie

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