0%

git 简明手册 | 学习笔记

迭代记录

  • 190702 首次提交

文件

  • 全局配置文件:~/.gitconfig

  • 项目配置文件:<project>/.git/config

命令

git add

1
2
3
4
5
6
7
8
# 添加文件到仓库
git add <FILE>

# 把当前所有位置的所有文件都添加到仓库中
git add .

# 允许指定add具体哪些内容,使用?查看选项的解释
git add -p

git branch

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# 查看本地分支
git branch

# 查看远程分支
git branch -r

# 查看本地分支+远程分支
git branch -a

# 创建分支
git branch <name>

# 删除分支
git branch -d <name>

# 删除远程分支
git branch -dr [remote/branch]

# 新建一个分支,与指定的分支建立追踪关系,一般是对远程分支建立追踪关系
git branch --track [newBranch] [remote/branch]
# eg
git branch --track mage origin/mage

# 建立追踪关系,在现有分支与指定的远程分支之间 | 已经弃用
git branch --set-upstream [branch] [remote/branch]

git checkout

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 切换到上一个分支
git checkout -

# 撤销对当前工作区文件的修改。其中--的作用是表明后面的参数是文件名,而不是分支名。
git checkout -- FILE

# 切换分支
git checkout <name>

# 创建+切换分支
git checkout -b <name>

# 在origin/master的基础上,创建一个新分支。
git checkout -b <name> origin/master

git cherry-pick

1
2
# 选择一个commit,合并进当前分支
git cherry-pick [commit]

git clone

1
2
3
4
5
# 只克隆仓库的最新一个版本,对于多次提交的仓库可明显提高clone速度
git clone --depth=1 仓库地址

# 使用-o指定远程分支名,假如不指定,默认是origin
git clone -o jQuery https://github.com/jquery/jquery.git

git commit

1
2
3
4
5
6
7
8
9
10
11
12
# 把文件提交到仓库
git commit

# 可以选择部分文件commit
git commit <file1> <file2>...

# 使用一次新的commit,替代上一次提交
# 它一般适用于还未提交到远程仓库的提交,也适用于gerrit,不适用于Github
git commit --amend

# 不更改评论信息
git commit --amend --no-edit

git config

1
2
3
4
5
6
7
8
9
# 设置提交代码时的用户信息。
git config [--global] user.name "Username"
git config [--global] user.email "email@example.com"

# 显示当前的git配置。
git config --list

# 编辑Git配置文件。
git config -e [—global]

git diff

1
2
# 可以将FILE现在的状态和最近一次add到仓库的状态进行一次diff。
git diff FILE

git fetch

1
2
3
4
5
6
7
8
9
10
11
12
# 取回分支,但是并不合并。
git fetch <远程主机名> <分支名>

# 指定取哪个分支,默认情况下会取所有分支。
git fetch <分支名>

# 取回远程分支到本地某个分支,假如本地分支不存在,则创建。不会切换到该分支。
git fetch <远程主机名> <远程分支名>:<本地分支名>

git fetch origin :branch2
# 等价于
git fetch origin master:branch2

git init

1
2
# 初始化仓库
git init

git log

1
2
# 查看commit日志
git log

git merge

1
2
# 合并指定分支到当前分支
git merge [branch]

git mv

1
2
# 改名文件,并且将这个改名放入暂存区
git mv [file-original] [file-renamed]

git pull

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 某些部分可省略。
git pull <远程主机名> <远程分支名>:<本地分支名>

# 上面命令表示,取回origin/next分支,再与当前分支合并。
git pull origin next

# 上面命令指定master分支追踪origin/next分支。
git branch --set-upstream master origin/next

# 上面命令表示,本地的当前分支自动与对应的origin主机"追踪分支"进行合并。
git pull origin

# 上面命令表示,当前分支自动与唯一一个追踪分支进行合并。
git pull

git push

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 标准格式
git push <远程主机名> <本地分支名>:<远程分支名>

# 上面命令将本地的master分支推送到origin主机,同时指定origin为默认主机,后面就可以不加任何参数使用git push了。
git push -u origin master

# 将本地分支推送到远程主机。
gut push origin <本地分支名>

# 删除远程分支。
git push origin --delete <name>

# 提交所有tag
git push [remote] --tags

git rebase

1

git reflog

1
2
# 查看引用日志,可以回到“未来”。
git reflog

git remote

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 列出所有远程主机。
git remote

# 可以参看远程主机的网址。
git remote -v

# 查看主机的详细信息。
git remote show <主机名>

# 用于删除远程主机。
git remote rm <主机名>

# 用于远程主机的改名。
git remote rename <原主机名> <新主机名>

# 将仓库连接到远程服务器。一般是在git init后使用,绑定远程主机。
git remote add origin <server>

git reset

1
2
3
4
5
6
7
8
# 撤销对当前缓存区的修改,相当于`git add`的反操作。
git reset -- <file>

# 撤销对当前缓存区的修改。
git reset HEAD <file>

# 让HEAD指针指到commit_id代表的状态。顺便一提,HEAD^表示上一个状态,HEAD^^表示上上个状态。
git reset --hard commit_id

git revert

1
2
# 远程分支
git revert

git rm

1
2
3
4
5
# 删除工作区文件,并且将这次删除放入暂存区。即删除远程文件和本地文件。
git rm [file1] [file2] ...

# 停止追踪指定文件,但该文件会保留在工作区。即删除远程文件,但不删除本地文件。
git rm --cached [file]

git status

1
2
# 查看仓库当前的状态
git status

git tag

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 添加了tag的commit就会将源码添加到打包,一般用版本号为tag命名
# 在Github上看到的release不是git提供的,而是代码托管网站出于满足开发者发布二进制文件的需求而开发的

# 列出所有tag
git tag

# 新建一个tag在当前commit
git tag [tag]

# 新建一个tag在指定commit
git tag [tag] [commit]

# 删除本地tag
git tag -d [tag]