Git 使用详解

本文列举了 Git 的常用配置及使用方法。

1
2
3
$ git config -g user.name "khs1994"

$ git config -g user.email "khs1994@khs1994.com"

常见错误

将本地分支与远程分支关系建立起来

1
$ git branch --set-upstream dev origin/dev

永久删除大文件

自行替换 *.db 为自己的文件规则

1
2
3
4
5
6
7
8
$ git filter-branch --force --index-filter 'git rm --cached --ignore-unmatch *.db' --prune-empty --tag-name-filter cat -- --all

$ rm -rf .git/refs/original/
$ git reflog expire --expire=now --all
$ git gc --prune=now
$ git gc --aggressive --prune=now
$ git push origin --force --all
$ git push origin --force --tags

子模块

1
2
3
4
# 拉取子模块
$ git submodule update --init --recursive

$ git submodule add URL DIR

拉取 PR

1
2
3
$ git fetch origin pull/365/merge:branch-fix-1

$ git checkout branch-fix-1

配置

查看配置

1
$ git config -l

或者直接编辑 ~/.gitconfig 文件,但不推荐。

代理设置

--unset 取消代理

1
2
3
$ git config --global [--unset] http.proxy 127.0.0.1:1080

$ git config --global [--unset] https.proxy 127.0.0.1:1080

分支

查看当前位于哪个分支

1
2
3
4
5
# 准确打印分支,可能在 shell 脚本中用的多

$ git rev-parse --abbrev-ref HEAD

# git branch

基本操作

1
2
3
4
5
6
7
$ git checkout -b NEW_BRANCH

# 等价于以下命令

$ git branch NEW_BRANCH ; git checkout NEW_BRANCH

$ git branch {-D | -d} BRANCH_NAME

恢复

本地仓库与远程仓库保持一致(强制覆盖)

1
2
3
4
5
6
7
8
9
# 拉取远程所有分支

$ git fetch [origin] [branch] [--all]

# 假设当前位于 master 分支,想要与远程的 master 分支保持一致

# 若是其他分支请将 master 换为其他分支名即可

$ git reset --hard origin/master

恢复某文件到上一次 commit 状态(未 add)

1
$ git checkout -- modify.md

将 add 的文件移出

1
$ git reset HEAD file.md

fork 与上游代码保持更新

1
2
3
4
5
6
7
8
9
10
$ git remote -v

$ git remote add source URL

$ git fetch source

# 假设当前位于 master 分支,想要与上游的 master 分支保持一致
# 若是其他分支请将 master 换为其他分支名即可

$ git rebase source/master

拉取远程仓库的分支(本地分支不存在)

1
$ git fetch remote_repo remote_branch_name:local_branch_name

本地分支推送到不同名远程分支

1
$ git push origin master:gh-pages

tag

1
2
3
4
5
6
7
8
9
$ git tag TAG_NAME commit号

$ git tag -a TAG_NAME -m "DESCRIPT" commit号

$ git show TAG_NAME

$ git tag -d TAG_NAME # delete

$ git push origin {TAG_NAME | --tags}

删除远程标签

1
$ git push origin --delete tag <tagName>

Stash

暂时储藏已修改未提交的文件,修复 bug 时会用到。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
$ git stash

# 返回到了上次 commit 的状态,开始修复问题

$ vi bug_file

$ git add . ; git commit -m "fix xxx"

$ git stash list

$ git stash apply [stash@{0}]

$ git stash pop # apply last stash and remove it from the list

$ git stash clear

commit

1
$ git commit --amend # 重新编辑上一次提交

相关链接

0%