git tips

  • Git basic
    
      man git--
      
  • When remote system password, but you stored it in local credential, you should earse local cache first.
    
      git credential-osxkeychain erase
      
  • You can not create a branch with a relationship of contains
    
      # disable 
      git checkout -b branchA
      git checkout -b branchA/branchB
      # enable
      # git checkout -b branchA
      git checkout -b branchA/branchB
      
  • Stage changes in Hunks
    
      git add -p 
      
  • Checkout Your Last Branch
    
      git checkout -
      
  • Show Which Branches are Merged
    
      git branch --merged
      git branch --no-merged
      git branch --merged | xargs git branch -d
      
  • Auto-Completion
    • Base dir : contrib/completion
    • How:
      
        # in .bashrc
        source ~/.git-completion.bash 
        # or copy to specific dir
        cp ~/.git-completion.bash /opt/local/etc/bash_completion.d
        cp ~/.git-completion.bash /etc/bash_completion.d/
        
  • Grab a File from Another Branch without Switching Branches
    
      git checkout  -- path/to/file.rb
      
  • Git Branches Sorted by Last Commit
    
      git for-each-ref --sort=-committerdate --format='%(committerdate:short) %(refname:short) [%(committername)]'
      # add a alias 
      git config --global alias.latest "for-each-ref --sort=-committerdate --format='%(committerdate:short) %(refname:short) [%(committername)]'"
      
  • Git Aliases
    
      git config --global alias.co checkout
      git config --global alias.ds 'diff --staged'
      git config --global alias.st 'status -sb'
      git config --global alias.amend 'commit --amend -C HEAD'
      git config --global alias.undo 'reset --soft HEAD^'
      git config --global alias.br branch
      git config --global alias.ci commit
      git config --global alias.st status
      git config --global alias.ls 'log --pretty=format:"%C(yellow)%h %C(blue)%ad%C(red)%d %C(reset)%s%C(green) [%cn]" --decorate --date=short'
      git config --global alias.unstage 'reset HEAD --'
      git config --global alias.visual '!gitk'
      git config --global alias.standup 'log --since '1 day ago' --oneline --author ****'
      git config --global alias.graph 'log --graph --pretty=format':%C(yellow)%h%Cblue%d%Creset %s %C(white) %an, %ar%Creset''
      
  • Git blame

    
      git blame -w  # ignores white space
      git blame -M  # ignores moving text
      git blame -C  # ignores moving text into other files
      
  • Git Attribute file to avoid in-consistent EOL

    
      # These files are text and should be normalized (convert crlf => lf)
      *.cs      text diff=csharp
      *.xaml    text
      *.csproj  text
      *.sln     text
      *.tt      text
      *.ps1     text
      *.cmd     text
      *.msbuild text
      *.md      text
    
      # Images should be treated as binary
      # (binary is a macro for -text -diff)
      *.png     binary
      *.jepg    binary
    
      *.sdf     binary
      
  • Good Resource: