安装git
1 | sudo pacman -S git |
配置git
1 | 配置git的用户名 邮箱号 |
创建仓库
git init
1
2
3
4
5
6
7
8
9
10初始化一个仓库
git init
初始化newrepo目录为仓库
git init path/newrepo
添加跟踪的文件
git add *.c
git add README
git commit -m "初始化项目版本"git clone
1
2
3
4
5
6克隆远程仓库
git clone <repo>
git clone <repo> <directory>
git clone git://github.com/shcacon/grit.git
git clone git://github.com/shcacon/grit.git配置
1
2
3
4
5
6
7
8显示当前git配置信息
git config --list
针对当前仓库
git config -e
针对系统上的所有仓库
git config -e --global
git基本操作
提交与修改
git add
1
2
3
4
5
6添加文件到暂存区
git add [file1] [file2] ...
添加目录到暂存区
git add [dir]
git add .git status
1
2
3
4查看git仓库当前状况
git status
-s 显示简短的输出结果
git status -sgit diff
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24查看工作区和暂存区的差异
git diff [file]
显示暂存区和上一次提交的差异
git diff --cached [file]
git diff --staged [file]
显示两次提交的差异
git diff [first-branch]...[second-branch]
- git commit
```shell
提交暂存区到本地仓库中
git commit -m "message"
提交暂存区的指定文件到仓库区
git commit [file1] [file2] ... -m [message]
参数设置修改文件后不需要执行git add 命令,直接来提交
git commit -a
先设置提交代码时的用户信息
git config --global user.name 'yourname'
git config --global user.email 'test@youremail.com'
版本回退
git reset
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15指定退回到某一次提交的版本
git reset [--soft | --hard | --mixed] [HEAD]
#--mixed 重置暂存区文件与上一次的提交保持一致
#--soft 知识撤回上一次提交
#--hard 重置工作区,与上一次的提交保持一致
HEAD
HEAD 当前版本
HEAD^ 上一次版本
HEAD^^ 上上次版本
HEAD^^ 上上上次版本
HEAD-0 当前版本
HEAD-1 上一次版本
HEAD-2 上上一次版本
HEAD-3 上上上一次版本git rm
1
2
3
4
5
6
7
8
9
10
11将文件从工作区和暂存区中删除
git rm <file>
强制删除
git rm -f <file>
仅仅将文件从暂存区中移除
git rm --cached <file>
可以递归删除
git rm -r *git mv
1
2
3
4
5移动或重命名
git mv [file] [newfile]
新文件名存在,强制重命名
git mv -f [file] [newfile]
分支切换
git checkout
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17切换到指定分支
git checkout <branch-name>
创建新分支 并切换到它
git checkout -b <new-branch-name>
切换到前一个分支
git checkout -
将指定文件恢复到最新的提交状态,丢弃所有未提交的更改,这对于撤销不需要的更改非常有用
git checkout -- <file>
切换到特定提交,使用提交哈希值
git checkout <commit-hash>
切换到标签
git checkout tags/<tag-name>git switch
1
2
3
4
5
6
7
8
9
10
11切换到制定分支
git switch <branch-name>
创建一个新分支并切换到它
git switch -c <new-branch-name>
切换到前一个分支
git switch -
切换到标签指向的提交状态
git switch tags/<tag-name>git restore
1
2
3
4
5
6
7
8
9
10
11恢复文件到最新提交状态
git restore <file>
还原文件到暂存区状态
git restore --staged <file>
还原所有未提交的更改
git restore .
还原文件到特定提交的状态
git restore --source=<commit> <file>
提交日志
git log
1
2
3
4
5
6
7
8
9
10
11
12
13
14git log [options] [branch-name | commit-hash]
-p 显示提交的补丁 具体修改内容
--oneline 简洁的一行格式显示提交信息
--graph 以图形方式显示分支和合并历史
--decorate 显示分支和标签指向的提交
--author=<author> 显示特定作者的提交
--since=<time> 显示指定时间之后的提交
--until=<time> 显示指定时间之前的提交
--grep=<模式> 显示包含指定模式的提交消息
--no-merges 不显示合并提交
--stat 显示简略统计信息,包括修改的文件和行数
--abbrev-commit 使用短提交哈希值
--pretty=<格式> 使用自定义的提交信息显示格式
--reverse 逆向显示所有日志git blame
1
2
3
4
5
6逐行显示指定文件的每一行代码注释和相关信息
git blame [option] [path]
-L <起始行号>,<结束行号> 指定行号范围
-C 对重命名或拷贝的代码进行溯源
-M 对移动的代码进行溯源
--show-stats 显示行数统计信息
git 远程操作
1 | 累出当前仓库总已配置的远程仓库 |
git fetch
1
2
3
4
5提取远程仓库更新的数据
git fetch [alias]
从远程仓库提取数据,病尝试合并到当前分支
git merge [alias] [branch]git pull
1
2
3
4
5
6
7
8从远程获取代码并合并到本地版本
git pull <remote-host> <remote-branch-name>:<local-branch-name>
将远程主机origin的master分支拉取过来,与本地的brantest合并起来
git pull origin master:brantest
与当前分支合并
git pull origin mastergit push
1
2
3
4
5
6
7
8
9
10
11
12
13
14将本地仓库的分支版本上传到远程并合并
git push <remote-host> <local-branch>:<remote-branch>
将本地仓库当前分支master推送到远程origin主机的master分支上
git push origin master
git push origin master:master
强制执行
git push --force origin master
删除远程主机origin的master分支
git push origin --delete master
分支管理
1 | 列出分支 |
git 标签
1
2
3
4
5
6
7
8
9
10
11创建带注解的标签
git tag -a v1.0
查看所有标签
git tag
指定标签信息
git tag -a <tagname> -m "标签"
PGP签名标签命令
git tag -s <tagname> -m "标签"
github
1 | 有远程仓库 |
git 服务器搭建
1 | groupadd gi |