本文共 11991 字,大约阅读时间需要 39 分钟。
git pul出错
I am not able to commit to the remote repository, let me do a force push.
我无法提交到远程存储库,请强制执行。
Let me run rebase on the remote repository, to make the commit history neater.
让我在远程存储库上运行rebase,以使提交历史记录更整洁。
Let me amend my previous commit which is in the remote repository.
让我修改远程存储库中的上一次提交。
The points mentioned above are some of the things to avoid doing in Git.
上面提到的几点是在Git中要避免的一些事情。
In my previous posts I covered and . Click on the links to know more about them.
在我以前的文章中,我介绍了以及 。 单击链接以了解更多信息。
Git has amazing features and is very helpful for developers. But mistakes still happen while using Git. Here I will be mentioning some of the things to avoid while using Git and also explain why you should avoid them.
Git具有惊人的功能,对开发人员非常有帮助。 但是使用Git时仍然会发生错误。 在这里,我将提到使用Git时要避免的一些事情,并说明为什么要避免使用它们。
Let’s say two developers are working on a single branch. Developer 2 is a beginner to Git.
假设有两个开发人员在一个分支上工作。 开发人员2是Git的初学者。
Developer 2 does a quick google search and finds out about force push command and uses it. The command is git push -f
开发人员2进行了快速的Google搜索,并找到了有关强制推送命令的信息并使用了它。 命令是git push -f
This is because force push overrides the code in the remote repository and, hence, the existing code in remote repository gets lost.
这是因为强制推送会覆盖远程存储库中的代码,因此,远程存储库中的现有代码会丢失。
Developer 2 needs to pull the latest code changes from the remote repository and rebase the code changes into the local repository. Once the rebase is done successfully, Developer 2 can push the code into the Remote repository. Here we are talking about rebase from remote to local repo in the same branch.
开发人员2需要从远程存储库中提取最新的代码更改,并将代码更改重新存储到本地存储库中。 一旦成功完成了基础,开发人员2即可将代码推送到远程存储库中。 在这里,我们谈论的是从同一分支中的远程仓库到本地仓库的重新部署。
Avoid force push unless absolutely necessary. Use it only as a last resort if there is no other way to handle a situation. But remember that force push will override the code in the remote repository.
除非绝对必要,否则避免强行推动 。 如果没有其他方法可以处理这种情况,请仅将其用作最后的选择。 但是请记住,强制推送将覆盖远程存储库中的代码。
In fact if you are using a user interface like source tree, by default force push is disabled. You will have to manually enable it to use it.
实际上,如果您正在使用诸如源代码树之类的用户界面,则默认情况下禁用强制推送。 您必须手动启用它才能使用它。
Also if the right are used, each developer will have their own feature branches, and such a scenario would not even occur.
同样,如果使用正确的 ,每个开发人员都将拥有自己的功能分支,甚至不会发生这种情况。
Let’s say two developers are working on a feature branch.
假设有两个开发人员正在开发功能分支。
But Developer 2 also wants to ensure that the latest changes from the release branch are rebased into the feature repository. So Developer 2 rebases the release branch onto the local feature branch. This is the rebase done from remote to local repo of different branches.
但是开发人员2还希望确保将release分支中的最新更改重新构建到功能部件存储库中。 因此,Developer 2将发布分支重新定位到本地功能分支。 这是从不同分支的远程仓库到本地仓库的基础 。
Rebasing the remote repository will alter the commit history and will create issues when other developers try to pull the latest code from the remote repository.
重新建立远程存储库的基础将更改提交历史记录,并且当其他开发人员尝试从远程存储库中获取最新代码时会产生问题。
The ideal way of handling this situation is to always rebase only the local repository. None of the commits in the local repo should have already been pushed to the remote repo.
处理这种情况的理想方法是始终仅对本地存储库进行基础调整。 本地存储库中的所有提交都不应该已经被推送到远程存储库。
If any of the commits have already been pushed to the remote feature branch, then its better to do a merge with the release branch rather than a rebase since merge would not alter the commit history.
如果任何提交都已经被推送到远程功能分支,那么最好与release分支进行合并而不是重新设置,因为合并不会更改提交历史记录。
Also if the right Git workflows are used, only one person would work on one feature branch, and this issue would not even occur.
同样,如果使用正确的Git工作流程,则只有一个人可以在一个功能分支上工作,并且甚至不会发生此问题。
If only one person works on the feature branch and a rebase is done on the remote feature branch, then there is no issue — no other developers are pulling code from the same remote feature branch. But it is best to avoid rebasing a remote repository.
如果只有一个人在功能部件分支上工作,并且在远程功能部件分支上完成了重新建立基础,那么就没有问题-没有其他开发人员从同一远程功能部件分支中提取代码。 但是最好避免重新建立远程存储库。
Rebase is a very powerful feature, but use it carefully.
Rebase是一项非常强大的功能,但请谨慎使用。
Let’s say two developers are working on a feature branch.
假设有两个开发人员正在开发功能分支。
Amending a commit changes the commit history. So amending a commit in the remote repository will create confusion when other developers try to pull the latest code from the remote repository
修改提交会更改提交历史记录。 因此,当其他开发人员尝试从远程存储库中获取最新代码时,修改远程存储库中的提交会造成混乱。
Best practise is to amend commits only in the local repository. Once the commit is there in the remote repository, it is best not to do any amends.
最佳实践是仅在本地存储库中修改提交。 一旦提交位于远程存储库中,最好不要进行任何修改。
Also, if the right Git workflows are used, only one person would work on one feature branch and this issue would not even occur. In this case, amending a remote repository would not create any issues, since no other developers are pulling code from the same remote feature branch.
另外,如果使用正确的Git工作流程,则只有一个人可以在一个功能分支上工作,甚至不会发生此问题。 在这种情况下,修改远程存储库不会造成任何问题,因为没有其他开发人员从同一远程功能分支中提取代码。
Hard reset is done by using git reset --hard
硬重置通过使用git reset --hard
There are other types of reset as well like--soft
and --mixed
which are not as dangerous as hard reset
还有其他类型的重置,例如--soft
和--mixed
,不像硬重置那样危险
Let’s say Developer 1 is working on a feature branch and has done five commits in the local repo.
假设开发人员1正在功能分支上,并在本地存储库中完成了5次提交。
If Developer 1 runs git reset --hard <commit4hash>
the following things will happen.
如果开发人员1运行git reset --hard <commit4hash>
,将发生以下情况。
Commit5 is still there internally in Git but the reference to it is lost. We can get the commit5 back using git reflog
. But, that being said, it is still very risky to use hard reset.
Commit5在Git内部仍然存在,但是对它的引用丢失了。 我们可以使用git reflog
返回commit5。 但是,话虽如此,使用硬重置仍然非常冒险。
Be very careful when you are using reset commands in Git. You may have to use reset in some scenarios, but evaluate the situation completely before going ahead with hard reset.
在Git中使用reset命令时要非常小心。 在某些情况下,您可能必须使用reset,但是在继续进行硬重置之前,请先全面评估情况。
The list I have mentioned above does not cover everything. It just lists out some of the things that can go wrong while using Git.
我上面提到的列表没有涵盖所有内容。 它只是列出了使用Git时可能出错的一些事情。
So how do you know in general what to avoid while using Git?
那么,您通常如何知道在使用Git时应避免什么?
Hopefully this post gave some ideas about what can go wrong in Git and how to avoid it. ?
希望这篇文章对Git中可能出现的问题以及如何避免出现问题提供了一些想法。 ?
I love technology and follow the advancements in the field. I also like helping others with my technology knowledge.
我热爱技术,并关注该领域的进步。 我也喜欢用我的技术知识来帮助他人。
Feel free to connect with me on my LinkedIn account
随时使用我的LinkedIn帐户与我联系
You can also follow me on twitter
您也可以在Twitter上关注我
My website:
我的网站: :
Originally published at
最初发布在
翻译自:
git pul出错
转载地址:http://efkzd.baihongyu.com/