Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Push changes from git subtree to a branch for a pull request

How can I create local changes to a git subtree and then push those changes to a branch in the subtree's repo so that I can then create a pull request to merge the changes from that branch into the subtree's master?

Assume I have two repos, project and protocols, both of which are under my control.

Step 1: add protocols as a subtree in the project repo

$ git remote add protocols [email protected]:corp/protocols.git

$ git remote
origin
protocols

$ git subtree add --prefix=protocols protocols master --squash
...
From bitbucket.org:corp/protocols
 * branch            master     -> FETCH_HEAD
 * [new branch]      master     -> protocols/master
Added dir 'protocols'

Step 2: make some changes in the project repo to files that are in the protocols subtree

$ cd protocols
$ echo "foo" > some_file
$ git commit -a -m "added foo"

Step 3: create a branch in the protocols repo and push my local changes from project/protocols subtree to that branch

??

I'm unsure as to how best to achieve this...

Once my subtree changes have been successfully pushed to a branch in the remote protocols repo, I can then create a pull-request to merge those changes back into the protocols master.

Questions:

  • I have a local copy of protocols. Should I change to that repository, create a branch, and then change back to project and push the subtree changes to my local protocols repo?

  • Can I push the subtree changes directly to a new branch (as of yet uncreated) in my local protocols repo?

  • Can I push the subtree changes directly to a new branch (as of yet uncreated) in the remote protocols repo?

  • Is this a recommended workflow?

like image 633
Steve Lorimer Avatar asked Oct 12 '16 16:10

Steve Lorimer


People also ask

Can I push to a branch with a pull request?

Once you've created a pull request, you can push commits from your topic branch to add them to your existing pull request. These commits will appear in chronological order within your pull request and the changes will be visible in the "Files changed" tab.

How do you push changes to a pull request?

To make changes to an existing pull request, make the changes to your local branch, add a new commit with those changes, and push those to your fork. GitHub will automatically update the pull request.

What does git subtree push do?

git subtree split lets you specify a rev other than HEAD. ' git push '(man) lets you specify a mapping between a local thing and a remot ref. So smash those together, and have git subtree push let you specify which local thing to run split on and push the result of that split to the remote ref.

How do you push changes to someone else's branch?

Push Branch to Another Branch In some cases, you may want to push your changes to another branch on the remote repository. In order to push your branch to another remote branch, use the “git push” command and specify the remote name, the name of your local branch as the name of the remote branch.


1 Answers

create a branch in the protocols repo and push my local changes from project/protocols subtree to that branch

You can achieve that with a single command, subtree push, specifying a new remote branch:

$ git subtree push --prefix=protocols protocols feature

This will create a new branch, feature in the remote repository protocols

you can now create a pull request to merge the feature branch back into the master branch of protocols.

Once the branch has been merged back, you can update your subtree using a pull with --squash

$ git subtree pull --prefix=protocols protocols master --squash

This will create another commit with all the changes in the protocols repo squashed into one, and then a merge commit to merge into your project repo

Note there is a slight quirk to this method, and that is there will now be two commits in your project repo which contain the changes to the protocols repo.

  • The original commit made in the project repo's protocols subtree
  • The commit created by a subsequent subtree pull (typically along with other protocol changes, assuming --squash was used)
like image 169
Steve Lorimer Avatar answered Oct 23 '22 05:10

Steve Lorimer