Git

Git 常用功能

Posted by liuxu379 on April 19, 2021

多个 Commit 合并为一个

1. 创建 Rebase 分支

在当前开发分支上创建 Rebase 分支

git pull
git checkout -b global/feature/xx_rebase

2. 合并 Master 分支

查看 Master 分支最新 commit id(例如40aad6c),合并至 Rebase 分支

git reset 427a756 --soft

3. 提交代码

git add . & git commit -m 'xx'

如果当前分支已更新到远程,需要强制推送

git push -f origin currentBranch

分支1 Commit 合并至分支2

假设分支2要合并分支1内的Commit;

首先记录分支1待合并的 Commit hash,并切换至分支2;

合并单个 Commit

git cherry-pick <commit hash>

合并多个 Commit

git cherry-pick <hash a> <hash b>

合并多个 Commit 区间(hashA 早于 hashB,不会将 hashA 合并过去)

git cherry-pick hashA..hashB

合并多个 Commit 区间(hashA 早于 hashB,会将 hashA 合并过去)

git cherry-pick hashA^..hashB

Clone 时卡住

Clone 项目时经常碰到卡住的情况,会报错:

fatal: the remote end hung up unexpectedly
fatal: early EOF
fatal: index-pack failed

这是因为有大文件存在导致的,我们在 Clone 时可以将代码打包 Clone

git config --global core.compression -1

如果还是卡住,可以在第一次克隆的时候,把克隆深度设置为1,然后再fetch

git clone  https://example.com/example/example.git --depth  1
cd example
git fetch --unshallow