Skip to content

Instantly share code, notes, and snippets.

@anjohnson
Last active December 12, 2024 22:37
Show Gist options
  • Save anjohnson/8994c95ab2a06f7d2339 to your computer and use it in GitHub Desktop.
Save anjohnson/8994c95ab2a06f7d2339 to your computer and use it in GitHub Desktop.
Triangle workflows

Triangle Workflows

A triangle workflow involves an upstream project and a personal fork containing a development branch of the project. This configuration makes git pull merge changes from the upstream but git push send local commits to the personal fork. However those config settings only work on relatively recent versions of git; 1.7.9 doesn't support the required remote.pushdefault config setting so you will have to explicitly tell git push which remote to push to.

This gist does not attempt to explain exactly what these commands do, it's intended as a cheat-sheet/reminder.

To set up a project area

  • Fork the project on https://github.com/<project>

If you already cloned the upstream repository

  • Rename 'origin' to 'upstream' and add a new remote 'github':
$ cd <project>
$ git remote rename origin upstream
$ git remote add github [email protected]:<user>/<project>.git

Else this is a new project area

  • Clone the newly forked branch:
$ git clone -o github [email protected]:<user>/<project>.git
  • Add a remote 'upstream' to point to the official repository:
$ cd project
$ git remote add upstream [email protected]:<organization>/<project>.git
$ git fetch upstream

In both cases

  • Set the default push remote to be the new fork:
$ git config remote.pushdefault github
$ git config push.default current

The above commands are optional and won't work on older versions of git such as 1.7.9, they save having to type the names of the remote and branch when pushing changes to github.

To start a new development

  • Create and checkout a local branch where the work will be done, based off the upstream/master branch:
$ cd <project>
$ git checkout -b <development> upstream/master
  • Develop on this branch, committing freely to the local repository.
$ git add <files>
$ git commit -m '<message>'
  • Push commits to the remote github/<development> branch using:
$ git push

These commands are quite simple because of the options used in the earlier steps. On older git versions you have to use git push github instead.

Merge or Rebase

There are two ways to import changes committed to the master branch before this branch gets merged. If there are no conflicts between the two sets of changes though it may not be necessary to do either.

  • To merge changes from the upstream/master branch:
$ git pull
  • To rebase this branch onto the latest upstream/master commit:
$ git pull --rebase

Parallel work on the master branch

It is relatively easy to commit changes to the upstream/master branch while you're still developing in your private development branch. To switch your working directory to the master, use

$ git stash save <message>  # Optional, saves uncommitted changes
$ git checkout master
$ git pull

Now you can make any changes needed. To commit and push them to the upstream repository

$ git add <files>
$ git commit -m '<message>'
$ git push upstream

To get back to where you were on your development branch again

$ git checkout <development>
$ git stash pop  # Optional, restores saved uncommitted changes

To close a development

  • When you're ready for review, issue a pull request for this branch through github.

  • After the branch has been merged, delete it using

$ git checkout master
$ git pull
$ git branch -d <development>
@anjohnson
Copy link
Author

@geoffrey-eisenbarth The text of that hint comes from the server, and it doesn't know what name you used locally for your remote pointing to the project, so it uses 'origin' which is the default name for a remote if you don't specify one (it mentions that in the first line of the hint). If you've been following my gist instructions then that remote is named 'upstream' in your local clone, so yes you would need to translate the suggestion to git checkout --track upstream/dev in that case.

@geoffrey-eisenbarth
Copy link

Thanks for the sanity check! I also had to use -D (instead of -d) to delete my <development> branch, I think because the dev branch of my fork might not have been updated to include the <development> local branch?

Very much appreciate your help and having this resource to reference.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment