简体   繁体   中英

Commits to GitHub

The following workflow is what I use when I want make changes and have them committed to 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. 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.

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:

Set '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)

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

$ git push

Push the current branch and its changes to 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 . master will always be private, not shared in the Pull Requests on GitHub

$ 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.

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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