Git 常用命令 - rxliuli blog
source link: https://blog.rxliuli.com/p/009384f82d44496ba4de15eec4625156/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
G_
1.7k 字
20 分钟
本文最后更新于:2021年3月3日 中午
记录一些最常用的命令(从上往下使用频率依次降低),当然,这里只是吾辈个人的,其他功能可能暂时还没用到呢
详细的 git 相关知识强烈推荐 Pro Git
关键词
命令格式
命令解释
命令示例
命令解释
init
git init
为当前目录初始化 git 本地仓库
add
git add [要追踪的文件/路径]
添加到 git 跟踪
git add -A
追踪仓库目录下的所有文件
commit
git commit [路径] [选项] [提交说明]
提交当前的修改内容
git commit -a -m "提交全部的修改"
提交了全部的修改内容(仍在本机)
remote
git remote [选项] [远程仓库名]
操作 git 远程仓库
git remote
显示所有远程仓库的别名
git remote add origin https://github.com/rxliuli/rxliuli.github.io.git
为本地仓库关联一个远程仓库
git remote show origin
显示 origin 远程仓库的详细信息
git remote remove origin
删除 origin 远程仓库
push
git push [远程别名] [远程分支]
推送本地修改到远程
git push origin master
推送本地修改到远程 origin 的 master 分支上
pull
git pull [远程别名] [远程分支]
拉取远程修改到本地
git pull origin master
拉去远程 origin 上 master 分支的修改到本地
status
git status
查看本地仓库的状态,以此得知添加和修改的文件
clone
git clone [远程仓库地址]
克隆一个远程仓库到本地,这里和 pull 不同点在于本地不存在要克隆的仓库
git clone https://github.com/rxliuli/rxliuli.github.io.git
克隆吾辈的博客 github 仓库到本地
log
git log [选项]
查看 git 日志
git log
简单的查看 git commit 历史纪录
revert
git revert [提交记录 hash]
撤销掉指定提交
git revert ab1c2d2
撤销一次提交内容,然后将撤销的内容作为修改提交一次,保留了所有的记录
reset
git reset [提交记录 hash]
重置到某次提交上,和上面不一样的是不会添加新的提交记录,而是删除已有的提交记录
git reset ab1c2d2
不会在 log 中留下痕迹
git reset HEAD~[N 回退次数]
回退最近几次的提交, N 为几就回退几次
git reset HEAD~1
回退最近一次的提交
branch
git branch [分支]
git 分支(强大而又复杂的功能)
git branch dev
创建 dev 分支
git branch
列出所有分支
git branch dev -D
删除名为 dev 的分支
checkout
git checkout [分支名]
切换当前分支(分支之间不共享修改)
git checkout master
切换当前分支到 master 分支
git checkout origin/dev -b dev
拉取远程分支到本地并切换
merge
git merge [选项]
合并其他分支的修改到当前分支上
git merge dev
合并 dev 分支的修改到当前分支(一般是 master 分支)
git merge origin/master --allow-unrelated-histories
强行合并远程分支到本地
push
git push [远程仓库名] :[分支名]
删除掉指定的远程分支(仓库还在,只是删除分支)
git push origin :dev
删除远程仓库 origin 下的 dev 开发分支
stash
git stash
暂存本地更改
git stash list
查看所有暂存更改
git stash apply [index]
重新应用指定暂存更改
git stash apply
重新应用最新的暂存更改
git stash drop [index]
删除掉指定的暂存更改
git stash drop
删除掉最新的暂存更改
撤销掉本地所有的修改
1
git add -A && git stash && git stash drop
- 添加所有更改到 git 追踪中(如果没有被忽略的话)
- 添加所有本地更改到暂存区域中
- 删除掉刚添加的最新暂存更改
应用场景
修改了一些文件但又没有提交,突然发现有问题,想把它们全删除了重来,或者全部回到上次提交,先把这些修改暂存起来(不加最后一条命令)
区分文件名大小写
1
git config core.ignorecase false
解释
Windows 下默认不区分文件名大小写,所以需要特别设置一下。
git push 强制推送
1
git push -f
强制推送到远程分支,即便是远程包含本地不存在的提交
忽略已经跟踪的文件的提交
1
git update-index --assume-unchanged <files>
取消这种设定
1
git update-index --no-assume-unchanged <files>
从远端拉取分支
1
git checkout -b [本地分支名(不存在)] [远程分支名]
1
git checkout -b dev origin/dev
忽略已提交的文件修改
主要用于忽略一些本地修改的文件但又不希望提交,同时也不希望添加到 .gitignore 中将之从云端排除的方式。
1
git update-index --assume-unchanged [file-pattern]
git 删除远程 tag
显示本地 tag
1
git tag
删除本地 tag
1
git tag -d Remote_Systems_Operation ```
用 push, 删除远程 tag
1
git push origin :refs/tags/Remote_Systems_Operation```
删除远程分支
1
2
git branch -r -d origin/branch-name
git push origin :branch-name
两个 git 仓库合并
现在有两个仓库 kktjs/kkt 和 kktjs/kkt-next 我们需要将 kkt-next
仓库合并到 kkt
并保留 kkt-next
的所有提交内容。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# 1. 克隆主仓库代码
git clone [email protected]:kktjs/kkt.git
# 2. 将 kkt-next 作为远程仓库,添加到 kkt 中,设置别名为 other
git remote add other [email protected]:kktjs/kkt-next.git
# 3. 从 kkt-next 仓库中拉取数据到本仓库
git fetch other
# 4. 将 kkt-next 仓库拉取的 master 分支作为新分支 checkout 到本地,新分支名设定为 kkt-next
git checkout -b kkt-next other/master
# 5. 切换回 kkt 的 master 分支
git checkout master
# 6. 将 kkt-next 合并入 kkt 的 master 分支
git merge kkt-next
# 如果第 6 步报错 `fatal: refusing to merge unrelated histories`
# 请执行下面命令 ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
git merge kkt-next --allow-unrelated-histories
JetBrains IDE 提供了非常好用的冲突合并工具
在合并时有可能两个分支对同一个文件都做了修改,这时需要解决冲突,对文本文件来说很简单,根据需要对冲突的位置进行处理就可以。对于二进制文件,需要用到如下命令:
1
2
3
git checkout --theirs YOUR_BINARY_FILES # 保留需要合并进来的分支的修改
git checkout --ours YOUR_BINARY_FILES # 保留自己的修改
git add YOUR_BINARY_FILES
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK