推荐:
基本概念
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
|
设置代理
给 git 设置 SOCKS5 代理:
使用 https 的时候,就是使用 https 协议复制仓库的时候,如: https://github.com/KyleBing/T…
端口号根据自己的代理端口进行修改,一般 ss 是1080,v2ray 是 1081,clash 是 7891
1 2
| git config --global http.proxy 'http://127.0.0.3:7890' git config --global https.proxy 'https://127.0.0.2:7890'
|
也可以直接修改用户主目录下的.gitconfig
文件
1 2 3 4
| [http] proxy = http://127.0.0.3:7890 [https] proxy = https://127.0.0.2:7890
|
本地操作
新建 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]
|
标签
有的时候,我们希望给某一个特定的历史提交打上一些标签
新建标签
查看 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]
|
远程操作
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 47 48 49
| 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 branch -d [本地分支名称]
git push origin --delete [远程分支名称]
git push origin :[远程分支名称]
git branch --set-upstream-to=origin/[远程分支名称] [本地分支名称]
|