Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
General Git Workflow options. One way to keep your work up to date with the group project (upstream/master) by squashing, rebasing, and pushing up your latest work.

To checkout a Pull Request locally, add the following line to your .git/config file under the remote name for which you would like to checkout PRs.

    fetch = +refs/pull/*/head:refs/remotes/upstream/pr/*

No need to delete or alter any other lines in the file, just add it under the remote name.


  [remote "upstream"]
  	url =
  	fetch = +refs/heads/*:refs/remotes/upstream/*
  	fetch = +refs/pull/*/head:refs/remotes/upstream/pr/*

When you git fetch --all, pull requests will now be available to you. Check out the PRs as follows:

  git checkout pr/150     // or whichever PR number you would like to checkout

/******************************************************************************************************************************* This is just one way. If you don't have a flow yet, try this out til you find your own groove ;-) ********************************************************************************************************************************/


I am working on my branch...working on my branch...committed my last commit... Now I want to get everything ready to push up.

    git commit -m "last commit message needed"
    git log                                 // how many commits do I need to squash?
    git rebase -i HEAD~2                    // squashing 2 commits, after squashing is complete, follow next step
                                            // (see DETAILED REF for more details on squashing, etc)
    git checkout master
    git fetch --all                         // fetch all changes from all remotes
    git reset --hard upstream/master        // your master branch is now equivalent to the upstream/master
    git checkout your_branch                // go back to the branch you were working on
    git rebase master                       // this makes sure your branch is up to date with the group project.
                                            // resolve any conflicts, if any, by opening those files.
                                            // when all conflicts are resolved, follow next step
    git push origin your_branch             // do this if this branch does not have a PR raised yet


    git push origin your_branch -f          // use the -f force flag if you are updating a PR


Update my local master branch to the upstream/master

(NOTE: This assumes you already have a remote called upstream linked to your group project)

    git fetch --all 

    git branch                          // MAKE SURE YOU ARE ON THE MASTER BRANCH

    git reset --hard upstream/master    // now your local master branch is set to the upstream/master, yay!

    git checkout your_branch            // now check out your branch

do you need to squash?
if you have more than one commit on your branch then squash/fixup first

    git log                         // check how many commits you have on your branch

    git rebase -i HEAD~2            // this will include the last 2 commits in your rebase
                                    // choose a diff # if you need to

pick or s or f the commits
pick will keep the message, s will squash it, f will fixup
if you choose s then you will be given a second window after you save where you can choose or edit the message you want to keep
if you choose f then your commit message will be the same as the commit you pick

    git log                           // check that the commits squashed/fixed up the way that you anticipated

now time to rebase to make sure your branch doesn't have any conflicts with the main project branch (upstream/master)

    git rebase master               // remember when we set your master branch to the upstream/master?
                                    // now we are rebasing your branch with master (ie, upstream/master)

if the rebase shows any conflicts, look at those files and resolve the conflicts,
(ie, choose which version of the code should stay and delete the rest - make sure you don't delete other people's work though!)

if you aren't sure if you are still in a rebase, or sometimes it is just good to check, you can use these commands to help you out:

    git rebase --continue           // maybe you were given a message to resolve conflicts, use this after you're done.
                                    // this command will tell you what you need to do to continue successfully rebasing
                                    // or it will tell you that no rebase is in progress. peace of mind either way.
    git rebase --abort              // maybe you aren't sure what happened and you are worried ... you can use this and start again

are you all up to date? push it up!

    git push origin your_branch     // this is the first time you are pushing up your code


    git push origin your_branch -f  // if you already have a pull request up, you need to force push (-f)
                                    // to replace what is up in GitHub with your current work on your_branch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment