Skip to content

Chapter_7

Chris McIntosh edited this page Dec 2, 2019 · 5 revisions

7 - Git Tools

1 - Revision Selection

  • You can specify revisions by a partial SHA-1
    • At least 4 characters long and unique
    • git log --abbrev-commit --pretty=oneline
  • Branch references
    • use the branch name instead of the commit if it is at the tip of the branch
    • git rev-parse <branch> shows the commit a branch points to
  • Reflog
    • git reflog shows what HEAD has pointed to for the last few months
    • git show HEAD@{n} refers to the reflog number
    • git show master@{yseterday} would show you yesterday, but only works for what is in the reflog
    • git log -g shows the reflog
    • reflog is strictly local
  • <commit>^<n> means the nth parent of the commit
  • <commit>~<n> means the nth parent of the parent, so HEAD~2 would be the grandparent
  • <branch>..<branch> shows you the range of commits in one branch but not the other
  • ^<branch> and --not <branch> allow you to specify multiple branches and trim out any you don't want
  • <branch>...<branch> indicates all commits that are in either branch but not in both

2 - Interactive Staging

  • git add -i enters interactive staging
    • Allows you to unstage changes
    • p allows you to stage or unstage parts of a file

3 - Stashing and Cleaning

  • Stashing allows you to save dirty workspaces when context switching
  • git stash --keep-index stashes and leaves the changes locally I guess?
  • git stash -u saves untracked files into the stash as well
  • git stash --patch lets you interactively decide what to stash
  • git stash branch <branch name> creates a new branch from the stashed changes
  • git clean removes EVERYTHING from your working directory that isn't tracked
  • git stash --all moves everything in your working directory into the stash, safer than clean usually

4 - Signing your work

  • Can use GPG to sign your work
  • gpg --gen-key
  • git config --global user.signingkey <key>
  • git tag -s
  • git commit -S

5 - Searching

  • Git grep allows you to search through the trees
  • git grep <search term
  • Log searching with -L lets you search for changes in a function
  • git log -L :<funnction name>:<path>

6 - Rewriting History

  • git commit --ammend lets you redo a commit
  • git rebase -i HEAD~<n> lets you rebuild the last n commits
  • git filter-branch (--tree-filter|--commit-filter) lets you fix commit or file data in all of history (IE change the email in all commits or remove a password file from all commits)

7 - Rest

  • reset --soft Changes Head to the desired commit
  • reset --mixed is the default it sets HEAD to the desired commit and the index to the desired commit
  • reset --hard does the above two as well as gets rid of your working directory and it cant be recovered
  • Resetting a file skips the head changes

8 - Advanced merging

  • git merge --abort cancels a merge

$ git show :1:hello.rb > hello.common.rb $ git show :2:hello.rb > hello.ours.rb $ git show :3:hello.rb > hello.theirs.rb

Will get the 3 merge artifacts for you  
* `git merge-file ours common theirs > file` will merge them back together which you can commit to fix your merge  
* `git diff (--theirs|--ours)` lets you see specific diffs  
* `git checkout --conflict=diff3` lets you pick the different conflict markers  
* `git config --global merge.conflictstyle diff3` makes it permanent  
 
## 9 - Rerere 
* Allows you to save merge resolutions  
* Workflow seems to be this   
    * Periodically merge master into a long lived branch  
    * Resolve conflicts and save them with rerere somehow   
    * revert changes in your branch   
    * Some time in the future merge cleanly  
* `git config --global rerere.enabled true`  

## 10 - Debugging with Git
* `git blame` lets you annotate source files for the author of a particular line  
* `git bisect` is an interactive tool that will let you binary search through commits to determine when the file broke 

## 11 - Submodules  
* Submodules are distinct git repos within a repo such as a library 

## 12 - Bundling  
* Lets you bundle your entire push into a single binary that you can share   
* `git bundle create`   

## 13 - Replace  
* Allows you to essentially symlink a commit  
* Can't truly delete a git object but this allows you to pretend  
* Use case was to truncate history on a new repo and store the old history in a history repo  
 
## 14 - Credential Storage  
* Allows you to cache or store or use your OS keychain  
* Uses a global config as youd expect  
* If you store it without the keychain then it is in a plaintext file in your home directory  

## 15 - Summary 
* Useful Commands 
    * `git log --abbrev-commit --pretty=oneline`
    * `git rev-parse <branch>` 
    * `git reflog`
    * `git commit --ammend`
    * `git merge --abort`