Easy rebase for forked git repo, pull changes from the origin main git repository

This is for a case when you have forked a git repository, created your local branch there (in your forked repo) and intend to merge a PR (pull request) into the master branch for the original git repository.

In a scenario where you had initially made a commit and raised a PR, and since then say the main, origin git repo has merged some new changes. And you need to push some code changes to be included in your PR (which is not yet merged), while having those new changes pulled. I have some easy steps below which can be followed to do a quick pull and push your new changes again :)

  1. If you already made those new code changes and already did a git add & git commit for those changes …Oops! you can be helped — Run git stash and those commits will go back a step.
  2. Set the main git repo as upstream ….….….….….….….….….….….……git remote add upstream {main_git_repo_url}
  3. Fetch the new changes from the master branch of the main git repo….. git fetch upstream
  4. Now that you’ll have the new changes pulled into your branch, let’s get those changes you had accidentally committed earlier…………………. git stash pop
  5. If you had not made any changes earlier (smart!), make them now.
  6. git add, git commit
  7. Push the changes to the origin repo : git push -f origin {forked_branch_name}

Software Engineer. Docker, K8s, Linux, cloud and new technologies enthusiast. Interested in making a difference. NCSU Grad M.S. 2016–18