Git Relevant Operations

Oh My Zsh’s git plugin responds slowly in big repo

Add following lines into (local) .git/config or (global) ~/.gitconfig: link

1git config --add oh-my-zsh.hide-status 1
2git config --add oh-my-zsh.hide-dirty 1

Recover from shallow clone

1git fetch --unshallow

https://stackoverflow.com/a/6802238

Shallow clone (clone –depth) misses remote branches

  1. git remote set-branches origin 'remote_branch_name' or git remote set-branches origin '*'
  2. git fetch --depth 1 origin remote_branch_name
  3. git checkout remote_branch_name

References: this and this Reason of this phenomenon: link

Compare the log of two branches

1git log --graph --oneline <current_branch> <other_branch>

Show log around a commit

  • List parent (older) commits:
    1git log a2c25061 -n 10
    
  • List children (later) commits:
    1git log --oneline --reverse <since_hash>..REF | head -n 10
    
    or to include the commit:
    1git log --oneline --reverse <since_hash>^..REF | head -n 10
    
    The fact that git’s log is a single link list makes the reverse listing harder. Ref. 1 and 2 .

Push a branch to another branch

1git push <remote> <local_branch>:<remote_name>
2# example:
3# git push origin my-feature:feature

Amend multiple committes

https://jacopretorius.net/2013/05/amend-multiple-commit-messages-with-git.html

1git rebase -i HEAD~5

Bypass proxy for specific targets

For one connection

https://stackoverflow.com/a/41440395

1git -c http.proxy= clone https://github.com/foo/bar.git

For repective URLs

https://stackoverflow.com/a/41440648

1[http "https://proxy@github.com/"]
2    proxy = https://10.144.1.10:8080/

Clone only one branch

1git clone -b <branch-name> --single-branch <url> [local-name]

Get another branch afterwards:

1git remote set-branches --add origin <remote-branch>
2git fetch origin <remote-branch>[:local-branch]
3git checkout <local-branch>

https://stackoverflow.com/a/27860061

Change commit messages

The most recent commit

1git commit --amend -m "New commit message."

Generally amending a commit that is already pushed is NOT recommended. Given that you really need to do that and you truly understand the consequences, the commit would have to be force pushed:

1git push --force branch-name

Older or multiple commits

An interactive git rebase can be used. As said above, it is strongly discouraged to rebase commits that are already pushed to the remote Git repository.

  1. Use git rebase -i HEAD~N, where N is the number of commits to perform a rebase on. For example, if you want to change the fourth and the fifth latest commits, set N to 5:

    1git rebase -i HEAD~5
    

    The command will display the latest N commits in your default text editor:

    1pick 43f8707f9 fix: update dependency json5 to ^2.1.1
    2pick cea1fb88a fix: update dependency verdaccio to ^4.3.3
    3pick aa540c364 fix: update dependency webpack-dev-server to ^3.8.2
    4pick c5e078656 chore: update dependency flow-bin to ^0.109.0
    5pick 11ce0ab34 fix: Fix spelling.
    6
    7# Rebase 7e59e8ead..11ce0ab34 onto 7e59e8ead (5 commands)
    

    (Note that the latest commit goes to the bottom.)

  2. Move to the lines of the commit message you want to change and replace pick with reword:

    1reword 43f8707f9 fix: update dependency json5 to ^2.1.1
    2reword cea1fb88a fix: update dependency verdaccio to ^4.3.3
    3pick aa540c364 fix: update dependency webpack-dev-server to ^3.8.2
    4pick c5e078656 chore: update dependency flow-bin to ^0.109.0
    5pick 11ce0ab34 fix: Fix spelling.
    6
    7# Rebase 7e59e8ead..11ce0ab34 onto 7e59e8ead (5 commands)
    
  3. Save the changes and close the editor.

  4. For each chosen commit, a new text editor window will open. Change the commit message, save the file, and close the editor.

    1fix: update dependency json5 to ^2.1.1
    
  5. If needed, force push the changes to the remote repository:

    1git push --force branch-name
    

Check tags of a commit

https://stackoverflow.com/a/33581248

1git describe --contains <commit_id> # closest tag
2git tag --contains <commit_id> # all tags

Delete tags with specific pattern

from https://gist.github.com/shsteimer/7257245#gistcomment-2623569

  • Delete remote:

    1git push -d origin $(git tag -l "tag_prefix*")
    
  • Delete local:

    1git tag -d $(git tag -l "tag_prefix*")
    

It solves the problem that when doing git name-rev in upstream linux repo, it gives me a useless next-XXX tag that doesn’t indicate any kernel version.

Find the commit that deleted a line

Try the following:

Find the commit that removed a file

https://stackoverflow.com/questions/6839398/find-when-a-file-was-deleted-in-git

For linux kernel, it’s better to filter merge commits:

1git log --no-merges --full-history -- <file>

只跟踪某一个/几个分支

通常 git clone 下来的仓是跟踪所有分支的:

1[remote "origin"]
2	url = https://github.com/schacon/simplegit-progit
3	fetch = +refs/heads/*:refs/remotes/origin/*

可以修改 .git/config 以指定要跟踪的分支,以及它在本地的名称。

1[remote "origin"]
2	url = https://github.com/schacon/simplegit-progit
3	fetch = +refs/heads/master:refs/remotes/origin/master
4	fetch = +refs/heads/experiment:refs/remotes/origin/experiment

参考:Git Refspec


动态加载过程与 PLT、GOT 表
LSM 启动过程