在我们的团队中,我们希望保持线性的 git 历史。
我们一直在运行这些命令:
用户更新了一些文件
git add .
git commit
git pull --ff-only
Run Code Online (Sandbox Code Playgroud)
但是我们经常看到:
fatal: Not possible to fast-forward, aborting.
Run Code Online (Sandbox Code Playgroud)
我想知道我使用的 git 命令是否可以改进?例如,通过使用 git pull --rebase 而不是 git pull --ff-only ?
重现步骤。
创建3个目录:
mkdir gitserver
mkdir gitclient1
mkdir gitcleint2
# server side
cd gitserver
git init --bare diverge.git
#client 1
cd gitclient1
git clone ../gitserver/diverge.git
cd diverge
<create index.html file with a few lines of text, save.>
git add .
git commit -m "first commit by user 1"
git push
#client 2
cd gitclient2
git clone ../gitserver/diverge.git
cd diverge
add a new line to index.html
git add .
git commit -m "changed line made by user 2"
git push
# back to client 1
$ git pull --ff-only
remote: Counting objects: 3, done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 1), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
From D:/projects/git/git_diverge_test/gitclient1/../gitserver/diverge
7208075..ce21832 master -> origin/master
fatal: Not possible to fast-forward, aborting.
Run Code Online (Sandbox Code Playgroud)
为什么我会收到致命消息?
git 不应该能够在现有提交之上拉下提交吗?
如果我运行 gitk --all,我会看到 master 与 remotes/origin/master 位于树的不同“分支”上。
一种在一定程度上有效的方法是使用:
git pull --rebase
<fixup any issues>
git add .
git rebase --continue
Run Code Online (Sandbox Code Playgroud)
git pull --rebase 是否更危险?
要将本地工作放在远程分支之上:使用git pull --rebase.
如果这是您所有团队的预期工作流程:让每个人都设置好
git config pull.rebase true
Run Code Online (Sandbox Code Playgroud)
而git pull现在将转化为git pull --rebase。
的预期用途git pull --ff-only是:
如果您确实在分支之上有本地工作,git pull --ff-only则永远不会执行任何操作:
如果要引入远程分支以检查它是否已更新:使用 git fetch
git fetch # will update all remote branches
git fetch origin branch # if you want to update one single branch from the remote
Run Code Online (Sandbox Code Playgroud)
运行后git fetch,您可以比较您的分支和origin/branch(使用 git 查看器,或git log --graph --oneline HEAD origin/branch),您可以在origin/branch(运行 : git rebase origin/branch)等基础上重新调整您的工作...