简体   繁体   English

提交到GitHub

[英]Commits to GitHub

The following workflow is what I use when I want make changes and have them committed to GitHub: 当我想要进行更改并将它们提交到GitHub时,将使用以下工作流程:

$ git checkout -b NewBranch
# Creates a new branch from origin/master for working on a specific feature and immediately switches to that branch. At this point you can add the feature you want.

$ git commit -a -m 'added a new function'
    # Commits the changes to the NewBranch

$ git checkout master
$ git merge NewBranch
    # Merging the changes we did in NewBranch with origin/master
$ git branch -d NewBranch
    # Deletes the NewBranch as it’s no longer needed
Pushing changes to remote repository
$ git push origin
    # Pushes all the changes from the master branch to the origin repository

At this point I visit the GitHub website and go to my online repository, the fork of the original project. 此时,我访问GitHub网站并转到我的在线存储库,即原始项目的分支。 There I click on New Pull Request , add some text and all changes are proposed in a Pull Request to the original owner. 我单击“ 新建请求” ,添加一些文本,所有更改均在请求中向原始所有者提出。

Until here, all is working as expected. 到这里,一切都按预期进行。 However, at this point when I make other changes locally on my client and do another git push origin , these changes are also immediately merged/added to the same Pull Request previously made. 但是,在这一点上,当我在客户端上本地进行其他更改并执行另一个git push origin ,这些更改也将立即合并/添加到先前进行的同一Pull Request中。

How can I avoid having these new changes being added to the original Pull Request? 如何避免将这些新更改添加到原始“拉取请求”中? I just want to have then online in my personal fork, not in the original repository. 然后,我只想在我的个人分支中在线,而不是在原始存储库中。

You will have two solutions: 您将有两种解决方案:

  1. When you are giving pull request, then first you merge the pull request. 当您发出请求请求时,首先合并请求请求。 After merge only you can start the work locally. 合并后,您只能在本地开始工作。

  2. After pushing the code, you can create new branch & pull the code from your old branch, and start the working on it. 推送代码后,您可以创建新分支,并从旧分支中提取代码,然后开始进行处理。 So when you are going to push the code then it will push to your new branch, it will not merge to previous pull request. 因此,当您要推送代码时,它将推送到您的新分支,它将不会合并到先前的请求中。 To merge this, you need to create new pull request. 要合并此请求,您需要创建新的请求请求。

The final and best solution thanks to @oruckdeschel is to change my way of working into the following steps: 感谢@oruckdeschel的最终最佳解决方案是将我的工作方式更改为以下步骤:

Set 'git push' 设置'git push'

$ git config --global push.default simple

Simple pushes the current branch to its upstream branch, but refuses to push if the upstream branch's name is different from the local one (this will be the default for future versions of git) Simple将当前分支推送到其上游分支,但是如果上游分支的名称与本地名称不同,则拒绝推送(这是git未来版本的默认设置)

Adding a new feature 新增功能

$ git checkout -b NewBranch

Creates a new branch (for each feature) and immediately switches to that branch. (为每个功能)创建一个新分支,并立即切换到该分支。 At this point you can make feature specific changes to the files. 此时,您可以对文件进行特定于功能的更改。

$ git commit -a -m 'Added a new feature’

Commits the changes to the NewBranch 将更改提交到NewBranch

$ git push

Push the current branch and its changes to GitHub. 将当前分支及其更改推送到GitHub。

Go to the GitHub webpage of your fork and click ‘Compare & Pull Request’:
- Base fork: OriginalAuthor/Project  - Base: Master --- Head fork: Me/Project  - Compare: NewBranch 

When the Pull Request is made, all new changes to this branch will also be automatically added to the same pull request . 发出请求请求后,对该分支的所有新更改也将自动添加到同一请求请求中

$ git checkout master
$ git merge NewBranch

Merging the changes we did in NewBranch with master . 将我们在NewBranch中所做的更改与master合并。 master will always be private, not shared in the Pull Requests on GitHub master始终是私有的,不会在GitHub的Pull Request中共享

$ git push -d origin NewBranch
$ git branch -d NewBranch

Delete the branch only when the Pull Requests is accepted 仅当接受请求请求时删除分支

Hopefully this might help others too in working with GitHub. 希望这可能会帮助其他人使用GitHub。

Thanks for the great help guys! 感谢你们的大力帮助!

My comment as an answer: 我的评论为答案:

I normally work with the branches a little different. 我通常在分支机构工作有点不同。 I push after I committed to the branch. 我承诺进入分支机构后再推动。 So the branch will land within github. 因此,该分支将降落在github中。 There you can create the Pull Request. 在那里您可以创建请求请求。 Merge your changes to the master (without deleting the branch) and do your "private" changes on the master. 合并对主服务器的更改(不删除分支),然后在主服务器上执行“私有”更改。 Whenever you push to the Pull Requested branch the commit will automagically added to the pull request. 每当您推送到“拉出请求”分支时,提交都会自动添加到拉取请求中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM