配置
- config user
| 1
2
 |   git config --global user.email "you@example.com"
  git config --global user.name "Your Name"
 | 
 
- Ignore Git permission changes
| 1
 | git config core.fileMode false
 | 
 
- Fix .gitignore
git仓库
- 初始化一个版本仓库
- clone远程版本库
| 1
 | git clone git@github.com:myself659/FFmpeg.git
 | 
 
- 添加远程版本库origin
| 1
 | git remote add origin git@github.com:myself659/rtmpserver_demo.git
 | 
 
- 查看远程仓库信息
- 删除远程仓库
| 1
 | git remote rm <repository>
 | 
 
git修改
- 添加当前修改的文件到暂存区
- 提交修改到本地
| 1
 | git commit -m "fix bug 0001"
 | 
 
- 提交修改到远程
| 1
 | git  push -u origin  master  
 | 
 
- 查看修改状态
- 重命名文件
- 从版本库中删除文件
- 取消对文件修改
- 修改最新一次修改注释
- 显示所有提交
| 1
 | git show <commit> --stat
 | 
 
| 1
 | git show <commit> -- <filepath>
 | 
 
- 恢复最后一次提交的状态
- 恢复某次提交的状态
- Remove untracked files & directories
| 1
2
3
4
 | # To remove untracked files
git clean -f
# TO remove untracked directories
git clean -fd
 | 
 
- Discard local changes
- Delete untracked files
git commit
- Edit last commit in both local and remote repository
| 1
2
3
4
5
6
7
8
9
 | # Step 1: Check recent commit history
git show HEAD
# Step 2: Update commit locally
git add <file-path(s)>
git commit --amend -m "<New-commit-message>"
# Step 3: Check recent commit history to verify the correct edit
git show HEAD 
# Step 4: Push the edit upstream
git push -f # Then, verify it in the web interface of the hosting service
 | 
 
- Delete last commit in both local and remote repository
| 1
2
3
4
5
6
7
 | # Step 1: Check recent commit history to check
git log --oneline -5
# Step 2: Delete commit locally and push the change to remote
git reset --soft HEAD~1 # Delete commit locally
git push -f # Delete commit upstream
# Step 3: Check recent commit history to check
git log --oneline -5
 | 
 
git repository
- rename repository
|  1
 2
 3
 4
 5
 6
 7
 8
 9
10
 | # Step 1: Rename local repository
cd .. # Go to the directory that holds the repository
mv <repo-old-name> <repo-new-name>
cd <repo-new-name>
# Step 2: Check remote url
git remote -v
# Step 3: Update remote url
git remote set-url origin <new-url>
# Step 4: Check remote url for verification
git remote -v
 | 
 
git log
- 查看修改log
- 查看指定文件每次提交记录
- 查看最近两次详细修改内容的diff
- 查看提交统计信息
- Move a commit from one branch to another
| 1
 | git cherry-pick COMMIT-HASH
 | 
 
- 查看项目各个成员提交代码统计
| 1
 | 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
 | 
 
- git log graph
| 1
 | git log --graph --format=format:'%C(bold blue)%h%C(reset) - %C(bold green)(%ar)%C(reset) %C(white)%an%C(reset)%C(bold yellow)%d%C(reset) %C(dim white)- %s%C(reset)' --all
 | 
 
git分支
- 查看远程分支
- 创建新分支
| 1
 | git branch <new_branch>
 | 
 
| 1
 | git checkout -b NEW-BRANCH-NAME
 | 
 
- 查看各分支创建最后提交信息
- 删除指定分支
| 1
2
 | git  branch  -d <branch_name>
git push origin -d <branch-name>
 | 
 
- 强制删除指定分支
| 1
 | git  branch  -D  <branch_name>
 | 
 
- 查看已经被合并到当前分支的分支
- 查看尚未被合并到当前分支的分支
- 合并分支
| 1
 | git merge <merge_branch>
 | 
 
- 切换分支
| 1
 | git checkout <branch_name>  -f
 | 
 
- 抓取远程仓库所有分支更新并合并到本地
- 抓取远程仓库所有分支更新并合并到本地,不要快进合并
- 抓取远程仓库更新
- push所有分支
- 将本地主分支推到远程主分支
- 将本地主分支推到远程(如无远程主分支则创建,用于初始化远程仓库)
| 1
 | git  push -u  origin  master
 | 
 
- 创建远程分支
| 1
 | git push origin <local_branch>:<remote_branch>
 | 
 
- 查看所有分支名称
- check  branch  status
- rename  local branch
| 1
 | git branch -m OLD-BRANCH-NAME NEW-BRANCH-NAME
 | 
 
- rename remote branch
| 1
 | git push origin :OLD-BRANCH-NAME NEW-BRANCH-NAME
 | 
 
- Delete a branch on a remote repository
| 1
 | git push REMOTE-NAME --delete BRANCH-NAME
 | 
 
- clean git branches exclude main and master
| 1
 | git branch | grep -v "master" | grep -v "main" | grep -v ^\* | xargs git branch -D;
 | 
 
- Create a local copy of the existing remote branch
| 1
2
 | git fetch origin
git switch --track origin/<branch-name>
 | 
 
git diff
- 比较当前文件和暂存区文件差异
- 比较两次提交之间的差异
- 比较两个分支
| 1
 | git diff <branch1> <branch2>
 | 
 
- 比较统计信息
- 提交git diff
How to Include Diff into Git Commit Message
|  1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
 | #!/bin/bash
COMMIT_MSG_FILE=$1
COMMIT_SOURCE=$2
SHA1=$3
RESULT="# Differences to be committed:"
while IFS= read -r line
do
  RESULT+="$IFS#    $line"
done <<< $(git diff --staged)
echo "$RESULT" >> $COMMIT_MSG_FILE
 | 
 
- 查看指定文件的修改
misc
- 提交文件超过100M
| 1
 | git filter-branch -f --index-filter "git rm -rf --cached --ignore-unmatch slides/tt.sql" -- --all
 | 
 
提交代码到新分支
- 创建新分支
| 1
 | git checkout  -b  feature-43
 | 
 
- 添加修改
- 提交修改到本地
- 将本地修改推送到指定分支
| 1
 | git push  origin  feature-43
 | 
 
丢弃本地修改
| 1
 | git reset --hard <the sha1 hash>
 | 
 
Reset to the last commit
| 1
 | git reset -hard origin/BRANCH-NAME
 | 
 
拉取所有分支
| 1
2
3
4
 | #!/bin/bash
for branch in $(git branch --all | grep '^\s*remotes' | egrep --invert-match '(:?HEAD|master)$'); do
    git branch --track "${branch##*/}" "$branch"
done
 | 
 
| 1
2
3
4
5
6
7
8
9
 | #!/bin/sh
# Usage: fetchall.sh branch ...
set -x
git fetch --all
for branch in "$@"; do
    git checkout "$branch"      || exit 1
    git rebase "origin/$branch" || exit 1
done
 | 
 
查看远程git地址
| 1
 | git config --get remote.origin.url
 | 
 
merge
| 1
2
 | git checkout feature
git merge master
 | 
 
将当前分支合并到原来master分支。
rebase
| 1
2
 | git checkout feature
git rebase master
 | 
 
将master后面的修改合并到当前分支,将当前分支作为个人开发master分支。
跳转到根目录
| 1
2
3
 | gitroot(){
    cd $(git rev-parse --show-toplevel 2> /dev/null)
}
 | 
 
gitstart
| 1
2
 | mkdir your-project
gitstart go
 | 
 
git submodule
delete
在path-to-submodule目录下删除.git文件夹
| 1
 | git rm -rf --cached path-to-submodule
 |