Skip to content

git

About 763 wordsAbout 3 min

2021-09-21

Branch

# Delete branches that exist locally but not remotely
git remote prune origin

# Delete branches that have been merged into master
git branch --merged master | grep -v '^\*\| master' | xargs -n 1 git branch -d

# View the correspondence between remote branches and local branches
git remote show origin

Commit

# Rewrite the last commit message
git commit --amend -m "new message"

# Modify the latest commit without changing the commit message
git commit --amend --no-edit

Configuration

# Get configuration help information
git help config
# Configure global user name
git config --global user.name "name"
# Configure global mailbox
git config --global user.email "email"
# Configure global colors to beautify git output
git config --global color.ui auto
# Edit global configuration files in a text editor
git config --global --edit
# Delete global configuration
git config --global --unset <entry-name>
# View local repo configuration
git config --list

Solve Chinese garbled characters

git config --global core.quotepath false

No longer treat file permission changes as changes

git config core.fileMode false

Set case sensitivity

# Case sensitive
git config --get core.ignorecase
# There are two identical directories in the remote, clear them in this way, and then submit the record
git rm -r --cached <dirOrFile>

Proxy

# View proxy
git config --global http.proxy
git config --global https.proxy
git config --global socks.proxy

# Set proxy
# Applicable to privoxy to convert socks protocol to http protocol http port
git config --global http.proxy http://127.0.0.1:1080
git config --global https.proxy http://127.0.0.1:1080
git config --global socks.proxy 127.0.0.1:1080

# Cancel proxy
git config --global --unset http.proxy
git config --global --unset https.proxy
git config --global --unset socks.proxy

# Set proxy only for github.com
git config --global http.https://github.com.proxy socks5://127.0.0.1:1080
git config --global https.https://github.com.proxy socks5://127.0.0.1:1080

# Cancel github.com proxy
git config --global --unset http.https://github.com.proxy
git config --global --unset https.https://github.com.proxy

Statistics query

# Commit number statistics
git log --oneline | wc -l
# View the committer of the file content
git blame <file-name>
# Total size of the warehouse
git count-objects -vH
# Warehouse size
git ls-files | xargs -r du -hs
# Find content in commit log
git log --all --grep='<given-text>'

View personal code volume

# Username needs to be changed to personal username
git log --author="username" --pretty=tformat: --numstat | awk \
'{ add += $1; subs += $2; loc += $1 - $2 } END { printf "added lines: %s, removed lines: %s, total lines: %s\n", add, subs, loc }' -

Number of lines added and deleted by each person

git log --format='%aN' | sort -u |\
while read name; do echo -en "$name\t";\
git log --author="$name" --pretty=tformat: --numstat | awk \
'{ add += $1; subs += $2; loc += $1 - $2 } END { printf "added lines: %s, removed lines: %s, total lines: %s\n", add, subs, loc }' -; done

View the ranking of committers

# Take the top ten rankings
git log --pretty='%aN' | sort | uniq -c | sort -k1 -n -r | head -n 10

submodule submodule

# Add a repository containing submodules
git clone <repo_url> --recursive
# View submodules in the current repository
git submodule status

# Add submodules
git submodule add <repo_url> <submodule_path>

# Initialize submodule
git submodule init

# Update submodule
git submodule update --remote

# Delete submodule
git submodule deinit <path_to_submodule>
git rm <path_to_submodule>

Switch to a specific commit of the submodule

cd <path_to_submodule>
git checkout <commit_hash>

Switch to a specific commit of the parent repository and update the submodule

git submodule update --remote
git checkout <commit_hash>

Get and switch the latest tag of the submodule

cd <path_to_submodule>
git fetch --tags
git checkout $(git describe --tags $(git rev-list --tags --max-count=1))

Submodule recursion

# Add all existing submodules
git submodule foreach --recursive git submodule add <repo_url>

# Update all submodules to the latest commit
git submodule foreach --recursive git pull origin master

# Check out a specific submodule path
git submodule foreach --recursive git checkout <branch_name>

# Get all submodule changes in the repository
git submodule foreach --recursive git fetch

# Get and merge the remote branch of the submodule
git submodule foreach --recursive git pull origin <branch_name>

# Restore the submodule to the initial commit in the parent repository
git submodule foreach --recursive git checkout .

# Get updates to the submodule and ignore local modifications
git submodule foreach --recursive git fetch --all
git submodule foreach --recursive git reset --hard origin/master