配置

  1. config user
1
2
  git config --global user.email "you@example.com"
  git config --global user.name "Your Name"
  1. Ignore Git permission changes
1
git config core.fileMode false
  1. Fix .gitignore
1
git rm -r --cached .

git仓库

  1. 初始化一个版本仓库
1
git init
  1. clone远程版本库
1
git clone git@github.com:myself659/FFmpeg.git
  1. 添加远程版本库origin
1
git remote add origin git@github.com:myself659/rtmpserver_demo.git
  1. 查看远程仓库信息
1
git remote -v
  1. 删除远程仓库
1
git remote rm <repository>

git修改

  1. 添加当前修改的文件到暂存区
1
git add .
  1. 提交修改到本地
1
git commit -m "fix bug 0001"
  1. 提交修改到远程
1
git  push -u origin  master  
  1. 查看修改状态
1
git  status  
  1. 重命名文件
1
git mv README  readme
  1. 从版本库中删除文件
1
git rm readme
  1. 取消对文件修改
1
git checkout -- readme
  1. 修改最新一次修改注释
1
git commit amend
  1. 显示所有提交
1
git show  
1
git show <commit> --stat
1
git show <commit> -- <filepath>
  1. 恢复最后一次提交的状态
1
git revert HEAD
  1. 恢复某次提交的状态
1
git revert <$id>
  1. Remove untracked files & directories
1
2
3
4
# To remove untracked files
git clean -f
# TO remove untracked directories
git clean -fd

git log

  1. 查看修改log
1
git log
  1. 查看指定文件每次提交记录
1
git log <filename>
  1. 查看最近两次详细修改内容的diff
1
git log -p -2
  1. 查看提交统计信息
1
git log --stat
  1. Move a commit from one branch to another
1
git cherry-pick COMMIT-HASH
  1. 查看项目各个成员提交代码统计
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
  1. 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. 查看远程分支
1
git branch -r
  1. 创建新分支
1
git branch <new_branch>
1
git checkout -b NEW-BRANCH-NAME
  1. 查看各分支创建最后提交信息
1
git branch  -v
  1. 删除指定分支
1
git  branch  -d <branch_name>
  1. 强制删除指定分支
1
git  branch  -D  <branch_name>
  1. 查看已经被合并到当前分支的分支
1
git branch --merged
  1. 查看尚未被合并到当前分支的分支
1
git branch  --no-merged
  1. 合并分支
1
git merge <merge_branch>
  1. 切换分支
1
git checkout <branch_name>  -f
  1. 抓取远程仓库所有分支更新并合并到本地
1
git pull  
  1. 抓取远程仓库所有分支更新并合并到本地,不要快进合并
1
git  pull --no-ff
  1. 抓取远程仓库更新
1
git fetch origin
  1. push所有分支
1
git push
  1. 将本地主分支推到远程主分支
1
git  push  origin  master
  1. 将本地主分支推到远程(如无远程主分支则创建,用于初始化远程仓库)
1
git  push -u  origin  master
  1. 创建远程分支
1
git push origin <local_branch>:<remote_branch>
  1. 查看所有分支名称
1
git branch --all
  1. check branch status
1
git status
  1. rename local branch
1
git branch -m OLD-BRANCH-NAME NEW-BRANCH-NAME
  1. rename remote branch
1
git push origin :OLD-BRANCH-NAME NEW-BRANCH-NAME
  1. Delete a branch on a remote repository
1
git push REMOTE-NAME --delete BRANCH-NAME
  1. clean git branches exclude main and master
1
git branch | grep -v "master" | grep -v "main" | grep -v ^\* | xargs git branch -D;

git diff

  1. 比较当前文件和暂存区文件差异
1
git diff <file>
  1. 比较两次提交之间的差异
1
git diff <$id1> <$id2>
  1. 比较两个分支
1
git diff <branch1> <branch2>
  1. 比较统计信息
1
git diff --stat
  1. 提交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
  1. 查看指定文件的修改
1
git show  -- 

misc

  1. 提交文件超过100M
1
git filter-branch -f --index-filter "git rm -rf --cached --ignore-unmatch slides/tt.sql" -- --all

提交代码到新分支

  1. 创建新分支
1
git checkout  -b  feature-43
  1. 添加修改
1
git add  .
  1. 提交修改到本地
1
git commit  -m  "f43"
  1. 将本地修改推送到指定分支
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
git pull --all
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