推荐:
基本概念
Git
是一个开源的分布式版本控制系统,用于敏捷高效地处理任何或小或大的项目工作中,在项目的开发进程中起着至关重要的作用。
- 工作区(workspace):就是你在电脑里能看到的目录。
- 暂存区(index):一般存放在
.git
目录下的index文件
(.git/index)中,所以我们把暂存区有时也叫作索引(index)。
- 版本库(.git):工作区有一个隐藏目录
.git
,这个不算工作区,而是 Git 的版本库。
HEAD
是个指针,默认指向最新的一次commit
,使用git log
查看提交版本ID
下面这个图展示了工作区、版本库中的暂存区和版本库之间的关系:

Git 配置
Git 的配置文件为.git/config
,当前仓库配置文件在当前目录下,全局配置文件在用户主目录下
配置用户名和邮箱是记录用户提交身份信息的,不是用来进行身份验证的,身份验证一般有 HTTPS 和 SSH 两种方式,参考Github登录认证的两种方法。
在当前仓库设置提交身份信息
1 2
| git config user.name '你的名字' git config user.email '你的邮箱'
|
全局设置提交身份信息
1 2
| git config --global user.name '你的名字' git config --global user.email '你的邮箱'
|
修改 Git 配置
以文本编辑器方式编辑
1 2 3 4 5
| # 当前仓库配置 git config --edit
# 全局配置 git config --global --edit
|
设置代理
端口号根据自己的代理端口进行修改,一般 ss 是1080,v2ray 是 1081,clash 是 7890
1 2
| git config --global http.proxy 'http://127.0.0.1:7890' git config --global https.proxy 'https://127.0.0.1:7890'
|
也可以直接修改用户主目录下的.gitconfig
文件
1 2 3 4
| [http] proxy = http://127.0.0.1:7890 [https] proxy = https://127.0.0.1:7890
|
.gitignore
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| # 忽略 index.css 这个文件 index.css
# 忽略所有 .a 文件 *.a
# 但跟踪所有的 lib.a,即使前面已经忽略 !lib.a
# 只忽略当前目录下的 TODO 文件 /TODO
# 忽略任何目录下名为 build 的文件夹 build/
# 忽略 doc 及其子目录下所有 pdf 文件 doc/**/*.pdf
|
托管平台登录方法
- HTTPS (https://github.com/pbloods/pbloods.git)
执行推送拉取命令后弹窗输入密码,很可能要求多次输入,体验极差
1
| git push https://github.com/<usename>/<repo>.git master
|
2021 年 8 月 13 日以后,Github 用个人访问令牌(personal access token)替代了密码,无需重复输入了。
1 2 3 4 5 6 7 8
| # 添加远程仓库 git remote add origin https://<your_token>@github.com/<usename>/<repo>.git # 修改现有远程仓库的url git remote set-url origin https://<your_token>@github.com/<usename>/<repo>.git # 克隆项目 git clone https://<your_token>@github.com/<usename>/<repo>.git # 推送项目 git push https://<your_token>@github.com/<usename>/<repo>.git master
|
- SSH (git@github.com:pbloods/pbloods.git)
通用方法,不仅仅适用于Github
1 2 3 4 5 6 7 8 9
| # 生成公钥和私钥 (按回车 3 下) ssh-keygen -t rsa -C '用户邮箱' # 查看公匙,在 github 个人设置页面添加 cat ~/.ssh/id_rsa.pub # 将公式保存到缓存中 ssh-add ~/.ssh/id_rsa # 验证是否成功 ssh -T git@github.com # Hi pbloods! You've successfully authenticated, but GitHub does not provide shell access.
|
如遇到此错误: Could not open a connection to your authentication agent.
,执行以下命令修复
本地操作
新建 Git 版本库
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| git init -b master
git init [dir]
git clone [url] [dir]
git clone -b [branch] [url] [dir]
git clone --depth=1 [url] [dir]
|
查看信息
版本库、暂存区、工作区内容全部一致将不会返回任何信息
查看文件状态
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| # 查看文件状态 $ git status (红色) # 新添加,未跟踪 modified:(红色) # 已修改,但未暂存 modified:(绿色) # 已修改,已暂存
# 指定目录或者文件 git status [dir]/[file]
# -s 精简输出 $ git status -s M README # 已修改,但未暂存 (M的位置靠右,红色) MM Rakefile # 已修改,暂存后又作了修改(有暂存和未暂存) A lib/git.rb # 新添加到暂存区,未提交 M lib/simplegit.rb # 已修改,已暂存 (M的位置靠左,绿色) ?? LICENSE.txt # 新添加,未跟踪
|
查看版本历史
1 2 3 4 5 6 7
| # 查看当前分支的HEAD之前的commitID和版本历史 git log
# 显示当前分支的最近几次commitID # --stat 每次commit发生变更的文件 # --follow 显示某个文件的版本历史,包括文件改名 git reflog
|
比较
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| git diff
git diff commitId [file]
git diff --cached [commit] [file]
git diff HEAD
git diff [commitId1] [commitId2]
|
添加工作区文件到暂存区
1 2 3 4 5 6 7 8
| git add [file1] [file2] ...
git add ./
git add -A
|
提交暂存区文件到版本库
1 2 3 4 5 6 7 8
| git commit -m [message]
git commit [file1] [file2] ... -m [message]
git commit --amend -m [message]
|
删除
1 2 3 4 5 6 7 8
| git rm [file1] [file2] ...
git rm --cache [dir]/[file]
git rm --cached [file]
|
恢复
git reset(将HEAD向后移动,用来恢复指定commit)
基本格式
1
| git reset --hard [HEAD]/[commitID] [file]
|
参数说明
1 2 3 4
| --soft --mixed --hard --before="1 days"
|
例:(将暂存区、工作区都恢复到上次commit的版本)
1 2 3
| git reset --hard HEAD . # 若不起作用尝试下面这个 git checkout HEAD --
|
git revert(新建n个commit,用来抵消指定commit的变化,HEAD向前移动到到最新分支)
和git revert
的区别是git reset
是把HEAD
向后移动了一下,而git revert
是HEAD
继续前进,新的commit内容
和revert内容
正好相反,实现一样的恢复效果,正因为这样,使用git revent
后用git log
可查看所有历史commit版本
。
git checkout
1 2 3 4 5 6 7 8
| git checkout [file]
git checkout .
git checkout [commit] [file]
|
分支
查看分支
1 2 3 4 5 6 7 8
| git branch
git branch -r
git branch -a
|
创建分支
1 2 3 4 5 6 7 8
| git branch [branch-name]
git checkout -b [branch]
git branch --track [branch] [remote-branch]
|
切换分支
1 2 3 4 5
| git checkout [branch-name]
git checkout -
|
合并分支
1 2 3 4 5 6 7 8
| git merge [branch]
git branch --merged
git branch --no-merged
|
删除分支
1 2 3 4 5 6 7 8
| git branch -d [branch-name]
git branch -D [branch-name]
git remote prune [shortname]
|
远程操作
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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
| git remote add [shortname] [url]
git push --set-upstream [remote] [branch]
git push [remote] [branch]
git pull [remote] [branch]
git push origin [本地分支名称]:[远程分支名称]
git checkout -b [本地分支名称] origin/[远程分支名称]
git pull origin [远程分支名称]:[本地分支名称]
git subtree push --prefix=[dir] [remote] [branch]
git remote show origin
git branch
git branch -r
git branch -a
git merge [remote] [branch]
git push origin --delete [远程分支名称]
git push origin :[远程分支名称]
git branch --set-upstream-to=origin/[远程分支名称] [本地分支名称]
|
标签
有的时候,我们希望给某一个特定的历史提交打上一些标签
新建标签
查看 tag
1 2 3 4 5
| git tag
git show [tag]
|
删除 tag
1 2 3 4 5
| git tag -d [tag]
git push origin :refs/tags/[tagName]
|
提交 tag
1 2 3 4 5 6 7 8
| git push [remote] [tag]
git push [remote] --tags
git checkout -b [branch] [tag]
|