跳转至

Git Tips#

Some configs#

  1. Set up multiple git accounts

    # ~/Workspace  # working related
    # ~/Code       # personal use
    
    $ vim ~/.gitconfig.work
    
    [user]
    name = work_name
    email = work_name@company.com
    
    $ vim ~/.gitconfig.personal
    
    [user]
    name = person_name 
    email = person_name@xyz.local
    
    $ vim ~/.gitconfig
    
    [includeIf "gitdir:~/Workspace/"]
        path = .gitconfig.work
    [includeIf "gitdir:~/Code/"]
        path = .gitconfig.personal
    
    # Check the validation
    $ cd ~/Workspace
    $ git config --get user.email
    
  2. Save username/password locally to avoid entering password interactively, useful with scripts but less security.

    % vim ~/.gitconfig
    [credential]
    helper = store --file $HOME/git-credentials/global.gitcredentials
    helper = store
    
    % vim ~/.git-credentials
    https://readonly:XXXXXXXXX@gitlab.your-own-domain.com
    

    OR

    git config --global credential.helper store    # Generate file ~/.gitconfig if non exist, and append `helper = store` to the file
    

    OR edit by using git config --global -e

    # update-alternatives --config editor
    There are 4 choices for the alternative editor (providing /usr/bin/editor).
    
      Selection    Path                Priority   Status
    ------------------------------------------------------------
    * 0            /bin/nano            40        auto mode
      1            /bin/ed             -100       manual mode
      2            /bin/nano            40        manual mode
      3            /usr/bin/vim.basic   30        manual mode
      4            /usr/bin/vim.tiny    15        manual mode
    
    Press <enter> to keep the current choice[*], or type selection number: 3
    update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/editor (editor) in manual mode
    

    select-editor won't take effect on Ubuntu 20.04

    # select-editor
    
    Select an editor.  To change later, run 'select-editor'.
      1. /bin/nano        <---- easiest
      2. /usr/bin/vim.basic
      3. /usr/bin/vim.tiny
      4. /bin/ed
    
    Choose 1-4 [1]: 2
    
  3. ~/.gitignore ignore files and/or directories file-system globally, while as per the .gitignore file under the root of each project directory locally.

Other Useful Tips#

  • Change author of history commits

    • Only the last commit

      git commit --amend --reset-author
      
    • All commits in history

      #!/bin/bash
      
      git filter-branch -f --env-filter '
      OLD_EMAIL="a@b.c"
      CORRECT_NAME="x"
      CORRECT_EMAIL="x@y.z"
      if [ "$GIT_COMMITTER_EMAIL" = "$OLD_EMAIL" ]
      then
      export GIT_COMMITTER_NAME="$CORRECT_NAME"
      export GIT_COMMITTER_EMAIL="$CORRECT_EMAIL"
      if [ "$GIT_AUTHOR_EMAIL" = "$OLD_EMAIL" ]
      then
      export GIT_AUTHOR_NAME="$CORRECT_NAME"
      export GIT_AUTHOR_EMAIL="$CORRECT_EMAIL"
      fi' HEAD
      

      OR

      git filter-branch -f --env-filter 'if [ "$GIT_AUTHOR_EMAIL" = "a@b.c" ]; then
          GIT_AUTHOR_EMAIL=x@y.z;
          GIT_AUTHOR_NAME="x";
          GIT_COMMITTER_EMAIL=$GIT_AUTHOR_EMAIL;
          GIT_COMMITTER_NAME="$GIT_AUTHOR_NAME"; fi' -- --all
      
  • Correct usage of .gitignore

  • Make git log show non-ASCII characters correctly

    git config --global core.quotepath false
    
  • Create a new empty branch in Git

    $ git checkout --orphan NEWBRANCH
    $ git rm -rf .
    
    --orphan creates a new branch, but it starts without any commit. After running the above command you are on a new branch "NEWBRANCH", and the first commit you create from this state will start a new history without any ancestry.
    

    You can then start adding files and commit them and they will live in their own branch. If you take a look at the log, you will see that it is isolated from the original log.