Skip to content
DUONG Phu-Hiep edited this page Jun 23, 2024 · 2 revisions

git worktree

Working on multiple branch in parallel

Move branch around

git branch -f <branch> <commit>

cherry-pick + squash multiple commits

git cherry-pick -n A B C
git cherry-pick -n A..B   (A is excluded)

(the squash is not committed you have to git commit yourself)

Modify the dates of the 2 last commits to "NOW"

git rebase HEAD~2 --ignore-date

Rebase from a commit X to commit Y

git rebase --onto master X Y     (X is excluded)

True nature of git rebase --onto

image

git rebase --onto master server client

image

Display changes from commit X to Y (format csv)

if use my alias

git changes X..Y

Raw command (if not use my alias)

git log --format=format:'%h%x3B%s%x3B%cN%x3B%ai' X..Y

git bash prompt and auto completion

https://github.com/git/git/blob/master/contrib/completion/git-prompt.sh

Append this block to ~/.bashrc

#git prompt: https://github.com/git/git/blob/master/contrib/completion/git-prompt.sh

# unstaged (*) and staged (+) changes will be shown next to the branch
GIT_PS1_SHOWDIRTYSTATE=yes
# If something is stashed, then a '$' will be shown next to the branch name
GIT_PS1_SHOWSTASHSTATE=yes
# If you would like to see the difference between HEAD and its upstream,
# set GIT_PS1_SHOWUPSTREAM="auto".  A "<" indicates you are behind, ">"
# indicates you are ahead, "<>" indicates you have diverged and "="
# indicates that there is no difference. You can further control
# behaviour by setting GIT_PS1_SHOWUPSTREAM to a space-separated list
# of values:
#
#     verbose       show number of commits ahead/behind (+/-) upstream
#     name          if verbose, then also show the upstream abbrev name
#     legacy        don't use the '--count' option available in recent
#                   versions of git-rev-list
#     git           always compare HEAD to @{upstream}
#     svn           always compare HEAD to your SVN upstream
GIT_PS1_SHOWUPSTREAM="verbose git"
#The prompt will include "|CONFLICT".
GIT_PS1_SHOWCONFLICTSTATE="yes"
#The colors are based on the colored output of "git status -sb".
GIT_PS1_SHOWCOLORHINTS="yes"
# If there're untracked files, then a '%' will be shown next to the branch name
GIT_PS1_SHOWUNTRACKEDFILES="yes"
#GIT_PS1_HIDE_IF_PWD_IGNORED="yes"
source ~/.git-prompt.sh
source ~/.git-completion.bash
PS1_DEFAULT=$PS1
if [ "$color_prompt" = yes ]; then
    PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u\[\033[00m\]:\[\033[01;34m\]\w\[\033[01;33m\]$(__git_ps1 " (%s\[\033[01;33m\])")\[\033[00m\]\$ '
else
    PS1='${debian_chroot:+($debian_chroot)}\u:\w$(__git_ps1 " (%s)")\$ '
fi

Merge branch, resolve conflicts

https://medium.com/@duongphuhiep/git-merge-conflicts-5f3919dd0fd0

Course Git

https://classroom.udacity.com/courses/ud775

Setup working space

https://classroom.udacity.com/courses/ud775/lessons/2980038599/concepts/33331589510923

SVN-like revision number

In SVN the "Revision Number" could tell the order of commit. In GIT, we have git describe.

git describe [<SHA>]

https://codepen.io/duongphuhiep/post/git-revision-number

Compare anything

git difftool fc7bb30..3def1e1 -- animal/index.html
git diff mybranch..master -- myfile.cs

the <SHA> might coming from 2 different branches

Understand HEAD~1 and HEAD^

Source

  • HEAD^1, HEAD^2: select parent 1 or 2
  • HEAD~3: select the precedent 3rd commits (ancestor)
  • Not just HEAD, it also works with other <commit-sha> (eg: fc7bb30~2^1)
G   H   I   J
 \ /     \ /
  D   E   F
   \  |  / \
    \ | /   |
     \|/    |
      B     C
       \   /
        \ /
         A

A =      = A^0
B = A^   = A^1     = A~1
C = A^2  = A^2
D = A^^  = A^1^1   = A~2
E = B^2  = A^^2
F = B^3  = A^^3
G = A^^^ = A^1^1^1 = A~3
H = D^2  = B^^2    = A^^^2  = A~2^2
I = F^   = B^3^    = A^^3^
J = F^2  = B^3^2   = A^^3^2

Git Alias

Here is my ~/.gitconfig

Use default graphic tools

  • git gui to build commit
  • gitk --all to see history and all branches
  • git difftool to compare

Tips for Windows Users

Shorten the command line

doskey tt='TortoiseGitProc.exe /command:'$*

Show history

tt log
tt log /path:Foo\Bar.js

Switch branches

tt switch