简体   繁体   中英

Git Push Upstream Branch Tracking

I have been introducing myself to git and am having trouble understanding how to assemble my workflow if I were to work with other individuals on a project.

Suppose a situation where I have been tasked with creating a new update to the existing code base. I would first pull from the remote repository, make a new branch, make my changes, commit, merge to my own master, and eventually push to the remote repository. But I want the changes I've made to be incorporated into the remote repo once my teammates have looked at the code and deemed it good. So in this situation ideally I would be able to make another branch but on the remote repository from the master version and push the changes that I made locally to that branch so that my teammates can look at it. Once they deem it ok, I want to be able to merge that branch on the remote repo into master version.

So in terms of commands on my end it would look like:

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 

How would I go about the creating a branch on the remote repository so it doesn't affect master too?

Ideally you should not be merging your changes from your feature or development branch to your local master branch and directly updating remote master .

Before creating a new feature or development branch from master in your local update the local master with 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

Now the local master is up to date with remote master , create a branch where you'll be working on the new changes for implementing new features

git checkout -b feature

Now you'll be switched to feature branch. Here make all the changes needed and once you are done with the changes then you can commit the changes locally to this feature branch.

git add .
git commit -m "Commit message"

Once the commit is successful, you can push the changes to the remote feature branch instead of merging directly to your local master

git push origin feature

All the changes are now pushed to remote feature branch. Here is the time to raise a Pull Request to master branch so that other teammates or other collaborators will be able to see what are all the changes which are being made. They'll make appropriate actions based on their review.

Once the reviewers are happy with the changes that are being made then you code changes will be merged to the remote master . If they are not happy with the changes and there needs to be some improvements required then you might have to do the corresponding changes in your local feature branch and push to remote feature branch. And the review process will take place.

This way only the approved code will be merged to your master in remote repository and will not get polluted.

Also please make sure that whenever you are going to work on a new feature create a new branch with different name so that even your feature branches will not get polluted. Also try to keep your each feature branches are independent for each feature so that it'll be easy for tracking and resolving any issues further.

Hope this helps in resolving the issue that you are facing.

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