Git Tips
安装卸载
官方教程,在 Linux/Unix 通过工具在系统中安装 git
,这种方法相对简单,便于升级卸载工具。
下面介绍在 CentOS 通过系统 yum 来安装 git
RHEL and derivatives typically ship older versions of git. You can download a tarball and build from source, or use a 3rd-party repository such as the IUS Community Project to obtain a more recent version of git.
官方文档说 git 在 RHEL
衍生产品通常发布旧版本 git
,我们需要编译和安装源代码或使用第三方存储库(如IUS社区项目)。
现在我们通过,IUS社区下载 ius-release.rpm 安装文件
# 注意下载不同版本的本机 CentOS 7 wget https://centos7.iuscommunity.org/ius-release.rpm # 安装rpm文件 rpm -ivh ius-release.rpm
检查可安装的git安装包
repoquery --whatprovides git # git-0:1.8.3.1-13.el7.x86_64 # git2u-0:2.16.5-1.ius.centos7.x86_64 # git2u-0:2.16.2-1.ius.centos7.x86_64 # git2u-0:2.16.4-1.ius.centos7.x86_64 # git-0:1.8.3.1-14.el7_5.x86_64
卸载 1.8.3
的 git
,安装 2.16.5
的 git
# 卸载旧版本 yum remove git # 安装新版本 yum install git2u
配置管理
首先是配置帐户信息 ssh -T git@github.com
测试。
git help config # 获取帮助信息,查看修改个人信息的参数 git config --list # 查看配置信息 git config --global user.name "小弟调调" # 修改全局名称 git config --global user.email "wowohoo@qq.com" # 修改全局邮箱 git config --global --unset <entry-name> # 删除全局设置
使用场景不常见
忽略文件权限的变化
不再将文件的权限变更视为变更
git config core.fileMode false
设置大小写敏感
git config --get core.ignorecase # 查看git 的设置 git config core.ignorecase false # 设置大小写敏感 git rm -r --cached <目录/文件> # 远程有两个相同的目录,以这种方式删除,然后提交记录
配置自动换行
自动转换坑太大,提交到git自动将换行符转换为lf
git config --global core.autocrlf input
创建SSH密钥
用这个密钥跟着 github 在本地终端中生成通信,然后上传到 github
ssh-keygen -t rsa -C 'wowohoo@qq.com' # 生成密钥 ssh-keygen -t rsa -C "wowohoo@qq.com" -f ~/.ssh/ww_rsa # 指定生成目录文件的名称 ssh -T git@github.com # 测试是否成功
多账号ssh配置
ssh-keygen -t rsa -C "邮箱地址" -f ~/.ssh/jslite_rsa
会生成 jslite_rsa
和 jslite_rsa.pub
这两个文件
vim ~/.ssh/jslite_rsa.pub
jslite_rsa.pub
,并把内容复制至代码托管平台上
vim ~/.ssh/config
#修改config文件,如果没有创建 config
Host jslite.github.com
HostName github.com
User git
IdentityFile ~/.ssh/jslite_rsa
Host work.github.com
HostName github.com
# Port 服务器open-ssh端口(默认:22,默认时一般不写此行)
# PreferredAuthentications 配置登录时用什么权限认证
# publickey|password publickey|keyboard-interactive等
User git
IdentityFile ~/.ssh/work_rsa
Host
这里是个别名可以随便命名HostName
一般是网站如:git@ss.github.com:username/repo.git
填写github.com
User
通常填写git
IdentityFile
使用的公钥文件地址
ssh -T git@jslite.github.com # `@`后面跟上定义的Host
ssh -T work.github.com # 通过别名测试
ssh -i ~/公钥文件地址 Host别名 # 如 ssh -i ~/.ssh/work_rsa work.github.com
# 原来的写法
git clone git@github.com:<jslite的用户名>/learngit.git
# 现在的写法
git clone git@jslite.github.com:<jslite的用户名>/learngit.git
git clone git@work.github.com:<work的用户名>/learngit.git
如果你修改了id_rsa的名字,你需要将ssh key添加到SSH agent中,如:
ssh-add ~/.ssh/jslite_rsa
ssh-add -l # 查看所有的key
ssh-add -D # 删除所有的key
ssh-add -d ~/.ssh/jslite_rsa # 删除指定的key
免密码登录远程服务器
$ ssh-keygen -t rsa -P '' -f ~/.ssh/aliyunserver.key
$ ssh-copy-id -i ~/.ssh/aliyunserver.key.pub root@192.168.182.112 # 这里需要输入密码一次
编辑 ~/.ssh/config
Host aliyun1
HostName 192.168.182.112
User root
PreferredAuthentications publickey
IdentityFile ~/.ssh/aliyunserver.key
上面配置完了,可以通过命令登录,不需要输入IP地址和密码 ssh aliyun1
https协议下提交代码免密码
git clone https://github.com/username/rep.git
通过上面方式克隆可能需要密码,解决办法:进入当前克隆的项目 vi rep/.git/config
编辑 config
, 按照下面方式修改,你就可以提交代码不用输入密码了。
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
ignorecase = true
precomposeunicode = true
[remote "origin"]
- url = https://github.com/username/rep.git
+ url = https://用户名:密码@github.com/username/rep.git
fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
remote = origin
merge = refs/heads/master
文件推向3个git库
git remote add origin https://github.com/JSLite/JSLite.git
git remote set-url --add origin https://gitlab.com/wang/JSLite.js.git
git remote set-url --add origin https://oschina.net/wang/JSLite.js.git
usage: git remote set-url [--push] <name> <newurl> [<oldurl>]
or: git remote set-url --add <name> <newurl>
or: git remote set-url --delete <name> <url>
git remote set-url --delete origin https://oschina.net/wang/JSLite.js.git
git push origin master
git push -f origin master # 强制推送
只能拉取 origin
里的一个url地址,这个fetch-url 默认为你添加的到 origin
的第一个地址
git pull origin master
git pull --all # 获取远程所有内容包括tag
git pull origin next:master # 取回origin主机的next分支,与本地的master分支合并
git pull origin next # 远程分支是与当前分支合并
# 上面一条命令等同于下面两条命令
git fetch origin
git merge origin/next
如果远程主机删除了某个分支,默认情况下,git pull 不会在拉取远程分支的时候,删除对应的本地分支。这是为了防止,由于其他人操作了远程主机,导致git pull不知不觉删除了本地分支。 但是,你可以改变这个行为,加上参数 -p 就会在本地删除远程已经删除的分支。
$ git pull -p
# 等同于下面的命令
$ git fetch --prune origin
$ git fetch -p
只需要更改config文件里,那三个url的顺序即可,fetch-url会直接对应排行第一的那个utl连接。
修改远程仓库地址
git remote remove origin # 删除该远程路径
git remote add origin git@jslite.github.com:JSLite/JSLite.git # 添加远程路径
撤销远程记录
git reset --hard HEAD~1 # 撤销一条记录
git push -f origin HEAD:master # 同步到远程仓库
放弃本地的文件修改
git reset --hard FETCH_HEAD # FETCH_HEAD表示上一次成功git pull之后形成的commit点。然后git pull
git reset --hard FETCH_HEAD
出现错误
git pull
You are not currently on a branch, so I cannot use any
'branch.<branchname>.merge' in your configuration file.
Please specify which remote branch you want to use on the command
line and try again (e.g. 'git pull <repository> <refspec>').
See git-pull(1) FOR details.
解决方法:
git checkout -b temp # 新建+切换到temp分支
git checkout master
最简单放弃本地修改内容
# 如果有的修改以及加入暂存区的话
git reset --hard
# 还原所有修改,不会删除新增的文件
git checkout .
# 下面命令会删除新增的文件
git clean -xdf
通过存储暂存区stash,在删除暂存区的方法放弃本地修改。
git stash && git stash drop
回退到某一个版本
git reset --hard <hash>
# 例如 git reset --hard a3hd73r
# --hard代表丢弃工作区的修改,让工作区与版本代码一模一样,与之对应,
# --soft参数代表保留工作区的修改。
搜索 commit 历史记录
git log --grep=224
# 这条命令是查看含有 "224" 关键字的 git commit
回滚到某个commit提交
git revert HEAD~1 # 撤销一条记录 会弹出 commit 编辑
git push # 提交回滚
去掉某个commit
# 实质是新建了一个与原来完全相反的commit,抵消了原来commit的效果
git revert <commit-hash>
把 A 分支的某一个 commit,放到 B 分支上
对两个分支,同时都拥有的文件,进行修改后,再同时 commit
到这两个分支,比如 master
分支和 branch1
分支,都拥有文件 test.js
,在 master
或者 branch1
分支下对 test.js
进行修改后,把修改的 test.js
同时提交到 master
分支和 branch1
分支。
git checkout <branch-name> && git cherry-pick <commit-id>
获取最近一次提交的 commit id
git rev-parse HEAD # e10721cb8859b2cd340d31a52ef4bf4b9629ddda
git rev-parse --short HEAD # e10721c
两个 git 仓库合并
现在有两个仓库 kktjs/kkt 和 kktjs/kkt-next 我们需要将 kkt-next
仓库合并到 kkt
并保留 kkt-next
的所有提交内容。
# 1. 克隆主仓库代码
git clone git@github.com:kktjs/kkt.git
# 2. 将 kkt-next 作为远程仓库,添加到 kkt 中,设置别名为 other
git remote add other git@github.com: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
在合并时有可能两个分支对同一个文件都做了修改,这时需要解决冲突,对文本文件来说很简单,根据需要对冲突的位置进行处理就可以。对于二进制文件,需要用到如下命令:
git checkout --theirs YOUR_BINARY_FILES # 保留需要合并进来的分支的修改
git checkout --ours YOUR_BINARY_FILES # 保留自己的修改
git add YOUR_BINARY_FILES
合并多个commit
# 这个命令,将最近4个commit合并为1个,HEAD代表当前版本。
# 将进入VIM界面,你可以修改提交信息。
git rebase -i HEAD~4
# 可以看到其中分为两个部分,上方未注释的部分是填写要执行的指令,
# 而下方注释的部分则是指令的提示说明。指令部分中由前方的命令名称、commit hash 和 commit message 组成
# 当前我们只要知道 pick 和 squash 这两个命令即可。
# --> pick 的意思是要会执行这个 commit
# --> squash 的意思是这个 commit 会被合并到前一个commit
# 我们将 需要保留的 这个 commit 前方的命令改成 squash 或 s,然后输入:wq以保存并退出
# 这是我们会看到 commit message 的编辑界面
# 其中, 非注释部分就是两次的 commit message, 你要做的就是将这两个修改成新的 commit message。
#
# 输入wq保存并推出, 再次输入git log查看 commit 历史信息,你会发现这两个 commit 已经合并了。
# 将修改强制推送到前端
git push -f origin master
修改远程Commit记录
git commit --amend
# amend只能修改没有提交到线上的,最后一次commit记录
git rebase -i HEAD~3
# 表示要修改当前版本的倒数第三次状态
# 将要更改的记录行首单词 pick 改为 edit
pick 96dc3f9 doc: Update quick-start.md
pick f1cce8a test(Transition):Add transition test (#47)
pick 6293516 feat(Divider): Add Divider component.
# Rebase eeb03a4..6293516 onto eeb03a4 (3 commands)
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
# d, drop = remove commit
保存并退出,会弹出下面提示
# You can amend the commit now, with
#
# git commit --amend
#
# Once you are satisfied with your changes, run
#
# git rebase --continue
# 通过这条命令进入编辑页面更改commit,保存退出
git commit --amend
# 保存退出确认修改,继续执行 rebase,
git rebase --continue
# 如果修改多条记录反复执行上面两条命令直到完成所有修改
# 最后,确保别人没有提交进行push,最好不要加 -f 强制推送
git push -f origin master
利用commit关闭一个issue
这个功能在Github上可以玩儿,Gitlab上特别老的版本不能玩儿哦,那么如何跟随着commit关闭一个issue呢? 在confirm merge的时候可以使用一下命令来关闭相关issue:
fixes #xxx
、 fixed #xxx
、 fix #xxx
、 closes #xxx
、 close #xxx
、 closed #xxx
、
新建一个空分支
# 这种方式新建的分支(gh-pages)是没有 commit 记录的
git checkout --orphan gh-pages
# 删除新建的gh-pages分支原本的内容,如果不删除,提交将作为当前分支的第一个commit
git rm -rf .
# 查看一下状态 有可能上面一条命令,没有删除还没有提交的的文件
git state
添加忽略文件
echo node_modules/ >> .gitignore
忽略某个文件的改动
git update-index --assume-unchanged path/to/file # 关闭 track 指定文件的改动,也就是 Git 将不会在记录这个文件的改动
git update-index --no-assume-unchanged path/to/file # 恢复 track 指定文件的改动
同步fork的上游仓库
Github教程同步fork教程,在Github上同步一个分支(fork)
在同步之前,需要创建一个远程点指向上游仓库(repo).如果你已经派生了一个原始仓库,可以按照如下方法做。
$ git remote -v
# List the current remotes (列出当前远程仓库)
# origin https://github.com/user/repo.git (fetch)
# origin https://github.com/user/repo.git (push)
$ git remote add upstream https://github.com/otheruser/repo.git
# Set a new remote (设置一个新的远程仓库)
$ git remote -v
# Verify new remote (验证新的原唱仓库)
# origin https://github.com/user/repo.git (fetch)
# origin https://github.com/user/repo.git (push)
# upstream https://github.com/otheruser/repo.git (fetch)
# upstream https://github.com/otheruser/repo.git (push)
同步上游仓库到你的仓库需要执行两步:首先你需要从远程拉去,之后你需要合并你希望的分支到你的本地副本分支。从上游的存储库中提取分支以及各自的提交内容。 master
将被存储在本地分支机构 upstream/master
git fetch upstream
# remote: Counting objects: 75, done.
# remote: Compressing objects: 100% (53/53), done.
# remote: Total 62 (delta 27), reused 44 (delta 9)
# Unpacking objects: 100% (62/62), done.
# From https://github.com/ORIGINAL_OWNER/ORIGINAL_REPOSITORY
# * [new branch] master -> upstream/master
检查你的 fork’s 本地 master
分支
git checkout master
# Switched to branch 'master'
合并来自 upstream/master
的更改到本地 master 分支上。 这使你的前 fork’s master
分支与上游资源库同步,而不会丢失你本地修改。
git merge upstream/master
# Updating a422352..5fdff0f
# Fast-forward
# README | 9 -------
# README.md | 7 ++++++
# 2 files changed, 7 insertions(+), 9 deletions(-)
# delete mode 100644 README
# create mode 100644 README.md
手动合并冲突的 Pull Request
以 tsbbjs/tsbb 为例,合并来自 jaywcjlove/tsbb master分支的 Pull Request
。
# 1. 克隆主仓库
git clone git@github.com:tsbbjs/tsbb.git
# 2. 在主仓库 master 分支切个 jaywcjlove-master 分支出来,并且切换到 jaywcjlove-master 分支
git checkout -b jaywcjlove-master master
# 3. 获取 jaywcjlove/tsbb 仓库 master 分支最新代码
git pull https://github.com/jaywcjlove/tsbb.git master
# ⚠️ 注意下面是输出内容:
# ----------------------
# Auto-merging src/babel/transform.ts
# CONFLICT (content): Merge conflict in src/babel/transform.ts
# ----------------------
# ⚠️ 注意上面 CONFLICT 标识是有冲突无法自动合并的代码,根据路径进入代码手动合并
# 4. 合并完成之后,进行 commit 说明合并内容
git commit -m "Merge branch 'master' of github.com:jaywcjlove/tsbb #3"
# 5. 切换到 master 分支,如果是 PR 其它分支,这里就切其它分支
git checkout master
# 6. 合并 jaywcjlove-master 分支的代码
git merge --no-ff jaywcjlove-master
# 7. 提交代码
git push origin master
修改作者名
git commit --amend --author='Author Name <email@address.com>'
批量修改历史commit中的名字和邮箱
这是 Github官方教程
注意参数,这个不是普通的clone,clone下来的仓库并不能参与开发
git clone --bare https://github.com/user/repo.git
cd repo.git
OLD_EMAIL原来的邮箱 CORRECT_NAME更正的名字 CORRECT_EMAIL更正的邮箱
将下面代码复制放到命令行中执行
git filter-branch -f --env-filter ' OLD_EMAIL="wowohoo@qq.com" CORRECT_NAME="小弟调调" CORRECT_EMAIL="更正的邮箱@qq.com" if [ "$GIT_COMMITTER_EMAIL" = "$OLD_EMAIL" ] then export GIT_COMMITTER_NAME="$CORRECT_NAME" export GIT_COMMITTER_EMAIL="$CORRECT_EMAIL" fi if [ "$GIT_AUTHOR_EMAIL" = "$OLD_EMAIL" ] then export GIT_AUTHOR_NAME="$CORRECT_NAME" export GIT_AUTHOR_EMAIL="$CORRECT_EMAIL" fi ' --tag-name-filter cat -- --branches --tags
执行过程
Rewrite 160d4df2689ff6df3820563bfd13b5f1fb9ba832 (479/508) (16 seconds passed, remaining 0 predicted)
Ref 'refs/heads/dev' was rewritten
Ref 'refs/heads/master' was rewritten
同步到push远程git仓库
git push --force --tags origin 'refs/heads/*'
我还遇到了如下面错误,lab默认给master分支加了保护,不允许强制覆盖。Project(项目)
->Setting
->Repository
菜单下面的Protected branches
把master的保护去掉就可以了。修改完之后,建议把master的保护再加回来,毕竟强推不是件好事。
remote: GitLab: You are not allowed to force push code to a protected branch on this project.
当上面的push 不上去的时候,先 git pull
确保最新代码
git pull --allow-unrelated-histories
# 或者指定分枝
git pull origin master --allow-unrelated-histories
cd ..
rm -rf repo.git
查看两个星期内的改动
git whatchanged --since='2 weeks ago'
查看某个文件历史
git log --pretty=oneline 文件名 # 列出文件的所有改动历史
git show c178bf49 # 某次的改动的修改记录
git log -p c178bf49 # 某次的改动的修改记录
git blame 文件名 # 显示文件的每一行是在那个版本最后修改。
git whatchanged 文件名 # 显示某个文件的每个版本提交信息:提交日期,提交人员,版本号,提交备注(没有修改细节)
查看git仓库中最近修改的分支
git for-each-ref --count=30 --sort=-committerdate refs/heads/ --format='%(refname:short)'
更新所有本地分支
git branch \
--format "%(if)%(upstream:short)%(then)git push . %(upstream:short):%(refname:short)%(end)" |
sh
打造自己的git命令
git config --global alias.st status
git config --global alias.br branch
git config --global alias.co checkout
git config --global alias.ci commit
配置好后再输入git命令的时候就不用再输入一大段了,例如我们要查看状态,只需:
git st
删除已经合并到 master 的分支
git branch --merged master | grep -v '^\*\| master' | xargs -n 1 git branch -d
中文乱码的解决方案
git config --global core.quotepath false
提交一个空文件夹
在空文件夹中建立一个文件 .gitkeep
, 你就可以提交这个空文件夹了。
新建仓库
init
git init
#初始化
status
git status
#获取状态
add
git add file
# .或*代表全部添加 git rm --cached <added_file_to_undo>
# 在commit之前撤销git add操作 git reset head
# 好像比上面git rm --cached
更方便
commit
git commit -m "message"
#此处注意乱码
remote
git remote add origin git@github.com:JSLite/test.git
#添加源
push
git push -u origin master # push同事设置默认跟踪分支
git push origin master
git push -f origin master # 强制推送文件,缩写 -f(全写--force)
clone
git clone git://github.com/JSLite/JSLite.js.git git clone git://github.com/JSLite/JSLite.js.git --depth=1 git clone git://github.com/JSLite/JSLite.js.git mypro # 克隆到
自定义文件夹 git clone [user@]example.com:path/to/repo.git/ # SSH协议还有另一种写法。
git clone支持多种协议,除了HTTP(s)以外,还支持SSH、Git、本地文件协议等,下面是一些例子。git clone <版本库的网址> <本地目录名>
$ git clone http[s]://example.com/path/to/repo.git/
$ git clone ssh://example.com/path/to/repo.git/
$ git clone ssh://example.com/path/to/repo.git/
$ git clone git://example.com/path/to/repo.git/
$ git clone /opt/git/project.git
$ git clone file:///opt/git/project.git
$ git clone ftp[s]://example.com/path/to/repo.git/
$ git clone rsync://example.com/path/to/repo.git/
本地
help
git help config # 获取帮助信息
add
git add * # 跟踪新文件
git add -u [path] # 添加[指定路径下]已跟踪文件
rm
rm *&git rm * # 移除文件
git rm -f * # 移除文件
git rm --cached * # 取消跟踪
git mv file_from file_to # 重命名跟踪文件
git log # 查看提交记录
commit
git commit #提交更新
git commit -m 'message' #提交说明
git commit -a #跳过使用暂存区域,把所有已经跟踪过的文件暂存起来一并提交
git commit --amend #修改最后一次提交
git commit log #查看所有提交,包括没有push的commit
git commit -m "#133" #关联issue 任意位置带上# 符号加上issue号码
git commit -m "fix #133" commit关闭issue
git commit -m '概要描述'$'\n\n''1.详细描述'$'\n''2.详细描述' #提交简要描述和详细描述
reset
git reset HEAD * # 取消已经暂存的文件
git reset --mixed HEAD * # 同上
git reset --soft HEAD * # 重置到指定状态,不会修改索引区和工作树
git reset --hard HEAD * # 重置到指定状态,会修改索引区和工作树
git reset -- files * # 重置index区文件
revert
git revert HEAD # 撤销前一次操作
git revert HEAD~ # 撤销前前一次操作
git revert commit # 撤销指定操作
checkout
git checkout -- file # 取消对文件的修改(从暂存区——覆盖worktree file)
git checkout branch|tag|commit -- file_name # 从仓库取出file覆盖当前分支
git checkout HEAD~1 [文件] # 将会更新 working directory 去匹配某次 commit
git checkout -- . # 从暂存区取出文件覆盖工作区
git checkout -b gh-pages 0c304c9 # 这个表示 从当前分支 commit 哈希值为 0c304c9 的节点,分一个新的分支gh-pages出来,并切换到 gh-pages
diff
git diff file # 查看指定文件的差异
git diff --stat # 查看简单的diff结果
git diff # 比较 Worktree 和 Index 之间的差异
git diff --cached # 比较Index和HEAD之间的差异
git diff HEAD # 比较Worktree和HEAD之间的差异
git diff branch # 比较Worktree和branch之间的差异
git diff branch1 branch2 # 比较两次分支之间的差异
git diff commit commit # 比较两次提交之间的差异
git diff master..test # 上面这条命令只显示两个分支间的差异
git diff master...test # 你想找出‘master’,‘test’的共有 父分支和'test'分支之间的差异,你用3个‘.'来取代前面的两个'.'
stash
存储当前的修改,但不用提交 commit
git stash # 将工作区现场(已跟踪文件)储藏起来,等以后恢复后继续工作。
git stash -u # 保存当前状态,包括 untracked 的文件
git stash list # 查看保存的工作现场
git stash apply # 恢复工作现场
git stash drop # 删除stash内容
git stash clear # 删除所有的 stash
git stash pop # 恢复的同时直接删除stash内容
git stash apply stash@{
0} # 恢复指定的工作现场,当你保存了不只一份工作现场时。
git checkout <stash@{
n}> -- <file-path> # 从 stash 中拿出某个文件的修改
merge
git merge --squash test # 合并压缩,将test上的commit压缩为一条
cherry-pick
git cherry-pick commit # 拣选合并,将commit合并到当前分支
git cherry-pick -n commit # 拣选多个提交,合并完后可以继续拣选下一个提交
rebase
git rebase master # 将master分之上超前的提交,变基到当前分支
git rebase --onto master 169a6 # 限制回滚范围,rebase当前分支从169a6以后的提交
git rebase --interactive # 交互模式,修改commit
git rebase --continue # 处理完冲突继续合并
git rebase --skip # 跳过
git rebase --abort # 取消合并
分支branch
删除
git push origin :branchName # 删除远程分支
git push origin --delete new # 删除远程分支new
git branch -d branchName # 删除本地分支,强制删除用-D
git branch -d test # 删除本地test分支
git branch -D test # 强制删除本地test分支
git remote prune origin # 远程删除了,本地还能看到远程存在,这条命令删除远程不存在的分支
提交
git push -u origin branchName # 提交分支到远程origin主机中
拉取
git fetch -p # 拉取远程分支时,自动清理 远程分支已删除,本地还存在的对应同名分支。
git fetch origin '+refs/heads/*:refs/heads/*' # 更新所有分支内容
分支合并
git merge branchName # 合并分支 - 将分支branchName和当前所在分支合并
git merge origin/master # 在本地分支上合并远程分支。
git rebase origin/master # 在本地分支上合并远程分支。
git merge test # 将test分支合并到当前分支
重命名
git branch -m old new # 重命名分支
查看
git branch # 列出本地分支
git branch -r # 列出远端分支
git branch -a # 列出所有分支
git branch -v # 查看各个分支最后一个提交对象的信息
git branch --merge # 查看已经合并到当前分支的分支
git branch --no-merge # 查看为合并到当前分支的分支
git remote show origin # 可以查看remote地址,远程分支
新建
git branch test # 新建test分支
git branch newBrach 3defc69 # 指定哈希3defc69,新建分支名字为newBrach
git checkout -b newBrach origin/master # 取回远程主机的更新以后,在它的基础上创建一个新的分支
git checkout -b newBrach 3defc69 # 以哈希值3defc69,新建 newBrach 分支,并切换到该分支
连接
git branch --set-upstream dev origin/dev # 将本地dev分支与远程dev分支之间建立链接
git branch --set-upstream master origin/next # 手动建立追踪关系
分支切换
git checkout - # 快速切换分支上一个分支
git checkout test # 切换到test分支
git checkout -b test # 新建+切换到test分支
git checkout -b test dev # 基于dev新建test分支,并切换
远端
git fetch <远程主机名> <分支名> # fetch取回所有分支(branch)的更新
git fetch origin remotebranch[:localbranch] # 从远端拉去分支[到本地指定分支]
git merge origin/branch # 合并远端上指定分支
git pull origin remotebranch:localbranch # 拉去远端分支到本地分支
git push origin branch # 将当前分支,推送到远端上指定分支
git push origin localbranch:remotebranch # 推送本地指定分支,到远端上指定分支
git push origin :remotebranch # 删除远端指定分支
git checkout -b [--track] test origin/dev # 基于远端dev分支,新建本地test分支[同时设置跟踪]
submodule
克隆项目同时克隆 submodule
git clone https://github.com/jaywcjlove/handbook.git --depth=1 --recurse-submodules
克隆项目,之后再手动克隆 submodule 子项目
git submodule add -b gh-pages --force '仓库地址' '路径'
git submodule a