Git推送上游分支跟踪

时间:2020-06-07 05:36:03

标签: git git-push

我一直在向自己介绍git,并且如果我要与项目中的其他人一起工作,那么我将很难理解如何组装我的工作流程。

假设有一种情况,我要负责为现有代码库创建新的更新。我首先要从远程存储库中拉出,创建一个新分支,进行更改,提交,合并到我自己的主服务器,最后推送到远程存储库。但是我希望我的队友在看完代码并认为很好之后,将所做的更改合并到远程仓库中。因此,在这种情况下,理想情况下,我可以在主版本的远程存储库上创建另一个分支,并将我在本地所做的更改推送到该分支,以便我的团队成员可以查看它。一旦他们认为还可以,我希望能够将远程仓库中的分支合并到主版本中。

所以从我这边的命令来看,它看起来像:

git pull 
git checkout -b new_update_branch
//make changes to code, etc.
git add *
git commit -m "update finished"
git checkout master
git merge new_update_branch
//somehow push the changes to a new branch on the remote repository
//i.e. create a branch on the remote repository too
//teammates look at this branch on the remote repository and ok it
git push origin master 

我该如何在远程存储库上创建分支,这样也不会影响master?

1 个答案:

答案 0 :(得分:2)

理想情况下,您不应该将您的featuredevelopment分支中的更改合并到local master分支中,并直接更新remote master

在本地的master上创建新的featuredevelopment分支之前,请先用local master来更新remote master

#If you are not already in master then run the next command otherwise you can skip
git checkout master 

git pull origin master

现在local masterremote master是最新的,请创建一个分支,您将在其中进行新的更改以实现新功能

git checkout -b feature

现在,您将切换到feature分支。在这里进行所有需要的更改,完成更改后,您可以将更改本地提交到此feature分支。

git add .
git commit -m "Commit message"

提交成功后,您可以将更改推送到remote feature分支,而不是直接合并到您的local master

git push origin feature

所有更改现在都推送到remote feature分支。现在是时候提出一个Pull Request来掌握分支了,以便其他队友或其他协作者可以看到正在进行的所有更改。 他们将根据自己的评论采取适当的措施。

一旦审核者对所做的更改感到满意,那么您所做的代码更改将被合并到远程master中。 如果他们对更改不满意,并且需要进行一些改进,则可能必须在local feature分支中进行相应的更改,然后推送到remote feature分支。然后将进行审核。

这样,只有批准的代码将被合并到远程存储库中的master中,并且不会受到污染。

还请确保每当您要使用新功能时,都要创建一个具有不同名称的新分支,以使即使您的功能分支也不会受到污染。另外,请尝试使每个功能分支对于每个功能都独立,这样可以很容易地进一步跟踪和解决任何问题。

希望这有助于解决您面临的问题。