Git Manual

{% note %}

阅读进度 Git - Undoing Things

✅ 2.1-2.3;2.5-2.7; 🕒 7.7,2.4

✅ 3.1;3.2; 3.5;3.6; 🕒 3.3-4

🕒 5.1-5.4; 🕒 6.1-6.6

{% endnote %}

基本操作

config

可用git config命令设置git配置:

1
2
3
4
git config --global user.name "[name]" 
git config --global user.email "[email address]"
git config --global color.ui auto #设定输出的color ui
git config --global core.editor  vim

也可在配置文件~/.gitconfig中直接编辑,如添加alias可写入:

1
2
3
4
[alias]
 unstage = reset HEAD --
  last = log -1 HEAD
  co = checkout

diff

  1. git diff可以查看工作目录与「暂存区」或「仓库(if not staged)」之间的文件差异.

  2. git diff --staged 可以查看「暂存区」和「仓库」之间的文件差异

1
2
git diff # see changes that not staged
git diff --staged # see changes that will go to the next commit
  1. 可以使用 git difftool调用其它编辑器查看修改内容; 使用 git difftool --tool-help 查看受支持的编辑器。

commit

git commit -a-a选项可以自动将所有已跟踪的文件加入到暂存区。

Adding the -a option to the git commit command makes Git automatically stage every file that is already tracked before doing the commit, letting you skip the git add part:

tagging

标签分为 Annotated 和 Lightweight 两种类型。其中 Annotated 需要添加 commit 信息, 保存的内容更多。

A lightweight tag is very much like a branch that doesn’t change — it’s just a pointer to a specific commit.

Annotated tags, however, are stored as full objects in the Git database. They’re checksummed; contain the tagger name, email, and date; have a tagging message; and can be signed and verified with GNU Privacy Guard (GPG). It’s generally recommended that you create annotated tags so you can have all this information; but if you want a temporary tag or for some reason don’t want to keep the other information, lightweight tags are available too.

标签查看

1
2
3
git tag #列出标签
git tag -l "v1.8.5*" #使用正则表达式筛选标签
git show "v1.8.5" # 查看标签具体信息

标签创建

1
2
3
git tag -a v1.4 -m "my version 1.4" # 添加Annotated Tags
git tag <light-weight> # 添加 Lightweight Tags
git tag -a v1.2 9fceb02  # 为某个commit追加标签

标签删除

1
git tag -d <tagname>

提取对应标签内容

1
2
git checkout  <tagname>  # it will put your repository in "detached HEAD" state.
git checkout -b <branch-name> <tag-name>

Remote Push

git push 默认不会同步标签的添加和删除信息,需要手动添加。

同步标签创建信息

1
2
3
git push <remote> <tagname>  # push 单个标签
git push <remote>--tags # push 所有标签
git push <remote> --follow-tags # push Annotated tags.

同步标签删除信息

1
2
3
4
$ git push origin :refs/tags/v1.4-
# The way to interpret the above is to read it as the null value before the colon is being pushed to the remote tag name, effectively deleting it. 
# Another way is following:
$ git push origin --delete <tagname>

rm

使用cached选项删除已经staged的文件

1
git rm --cached filename

log

输出格式

  1. -p 选项将打印出diff信息;
  2. --stat提交统计信息(statistics);
  3. --pretty将打印出格式化的信息。
  4. --graph将以ASCII 图形的方式打印出分支和合并历史
1
2
3
4
5
6
git log # print log of commit
git log -p -2 # print log of last two commits with patch info (the diff of file). 
git log --stat # print abbreviated stats for each commit,
git log --pretty=oneline  # use pretty option to format print
git log --pretty=format:"%h - %an, %ar : %s"
git log --pretty=format:"%h %s" --graph
Option Description
-p Show the patch introduced with each commit.
--stat Show statistics for files modified in each commit.
--shortstat Display only the changed/insertions/deletions line from the --stat command.
--name-only Show the list of files modified after the commit information.
--name-status Show the list of files affected with added/modified/deleted information as well.
--abbrev-commit Show only the first few characters of the SHA-1 checksum instead of all 40.
--relative-date Display the date in a relative format (for example, “2 weeks ago”) instead of using the full date format.
--graph Display an ASCII graph of the branch and merge history beside the log output.
--pretty Show commits in an alternate format. Option values include oneline, short, full, fuller, and format (where you specify your own format).
--oneline Shorthand for --pretty=oneline --abbrev-commit used together.

日志筛选

1
2
3
4
5
git log -2 # 输出最后两条记录
git log --since=2.weeks # 最近两周的记录
git log --since="2021-04-01"
git log --author="Bao Hengtao" # 限定作者
git log --grep="modify" # 匹配commit message
Option Description
-<n> Show only the last n commits
--since, --after Limit the commits to those made after the specified date.
--until, --before Limit the commits to those made before the specified date.
--author Only show commits in which the author entry matches the specified string.
--committer Only show commits in which the committer entry matches the specified string.
--grep Only show commits with a commit message containing the string
-S Only show commits adding or removing code matching the string

使用git log -- path/to/file 可将日志限定在某一文件或文件夹。注意路径必须写在最后。

分支管理

分支查看与管理

1
2
3
4
5
6
git branch -vv # 查看分支
$ git branch [--merged | --no-merged] [branch] #Listing branches merged or not merged with respect to <branch> or current branch.
git branch <newbranch> # 创建新分支
git branch 
git branch -d [branch-name] # 删除分支
git branch --move bad-branch-name corrected-branch-name # 重新命名branch

分支切换

1
2
3
4
5
6
7
8
git checkout <branch> # 切换分支
git checkout -b <new_branch> # 创建并切换到新分支
git checkout --orphan <new_branch> #建立一个全新的孤儿分支,

# with new  switch command
git switch <branch> # 切换分支
git switch -c new-branch # 创建并切换到新分支
git switch - # 切换到上次使用的分支

分支合并

1
2
git merge [branch] #合并分支
git mergetool # 冲突时选择合并工具

Rebase

Suppose the commit history is as following:

         A---B---C topic*
        /
   D---E---F---G master

The rebase command

1
2
git rebase master
git rebase master topic

will result in:

1
2
3
                   A'--B'--C' topic*
                  /
     D---E---F---G master

Working with Remote

远程分支管理与查看

查看仓库

1
2
3
git remote -v  # 查看远程仓库列表
git remote show <remote-name> # 插件远程仓库具体情况
$ git ls-remote <remote> # 获取remote references列表

管理仓库

1
2
3
git remote add <remote-shortname> <remote-url> # 添加原创仓库
git remote rename <old-name> <new-name> #重命名仓库
git remote remove <remote-name> #删除仓库

仓库获取与

1
2
git fetch <remote> # 获取仓库
git push <remote> local-branch-name:remote-branch-name #  提交

提取分支

1
2
3
4
5
$ git checkout -b <local-branch>  <remote>/<branch> # 获取分支
# following command is same, they will automatical track origin/serverfix
$ git checkout -b serverfix origin/serverfix
$ git checkout --track origin/serverfix
$ git checkout serverfix

设置track

1
2
git branch -u origin/serverfix
git branch --set-upstream-to origin/serverfix

查看track

1
git branch -vv

删除分支

1
git push <remote> --delete <branch>

Submodule

将一个仓库添加为submodule, 类似于git clone

1
2
# 
git submodule add <url> <dir> 

获取一个含有submodule的git仓库

1
2
3
git clone <url> # 获取一个含有submodule的git仓库
git submodule init # init an submodule
git submodule update # fetch all data and checkout appropriate commit

以上命令可合并为一条

1
git clone - --recurse-submodule <url>

Draft

Files Management

1
2
3
4
git commit --amend :  Amending last commit
git reset HEAD <file> : Unstaging a staged file
git checkout -- <file> : Unmodifying a Modified File
git commit -[a] -m 'msg' => using option `a` to automatically stage every file that is already tracked.

Remote Branches

1
2
3
4
5
6
7
git merge <remote>/<branch> #Merge remote branch
git checkout -b  <branch> <remote>/<branch> #Create a new branch that tracks the remote branch
git pull #fetch and merge for tracking branches.
git checkout --track <remote>/<branch> #Set tracking branch
git branch --set-upstream-to <remote>/<branch>  #Set upstream
git branch -vv #List out local branches with tracking information
git push <remote> --delete <branch>  #Delete branch from remote

Git on the Server

1
2
3
4
5
6
7
8
9
$ git clone --bare my_project my_project.git #almost equivalent to `$ cp -Rf my_project/.git my_project.git`
$ scp -r my_project.git [email protected]:/srv/git
$ git clone [email protected]:/srv/git/my_project.git

# Git on the Server in Action
# on server git dir
git clone --bare CodProj  gitdir/codproj.git
# on local machine
$ git clone $GIT/codproj.git
updatedupdated2023-06-052023-06-05
Update https-ca.md