Skip to content

Instantly share code, notes, and snippets.

@svagionitis
Last active December 24, 2015 14:19
Show Gist options
  • Save svagionitis/6811196 to your computer and use it in GitHub Desktop.
Save svagionitis/6811196 to your computer and use it in GitHub Desktop.
Git interactive rebase a branch to the master

Git interactive rebase a branch to the master

  1. Check out the lastest changes for the master with one of the following commands

    git checkout origin master or git fetch origin master
    
  2. Create a new branch from the master

    git branch -b TEMP
    
  3. Check out the branch that you want to rebase. For example, if the branch is BRANCH_REBASE

    git checkout BRANCH_REBASE
    
  4. Start the interactive rebase using the following command

    git rebase -i TEMP
    

    This command will select all the commits of this branch and will be presented in an editor in the following format

    pick 5cf316e Add empty page in about section
    pick 964e013 Add contents to about page
    pick 89db9ab Add HTML page for personal bio
    pick 2bda8e5 Add empty HTML page for Mary's bio
    pick 915466f Add link to about section in home page
    

    You want to create only one commit, so you will have only one pick and all the other commit will be squashed. The above example becomes

    pick 5cf316e Add empty page in about section
    squash 964e013 Add contents to about page
    squash 89db9ab Add HTML page for personal bio
    squash 2bda8e5 Add empty HTML page for Mary's bio
    squash 915466f Add link to about section in home page
    
  5. You save the above screen and close the editor. If there are no conflicts, a new editor will appear, which will be the message for the commit. You edit the message as you wish. Save and close the editor.

  6. Your local branch of BRANCH_REBASE will have only one commit, instead of 5 (from the above example). Don't try to push your local branch BRANCH_REBASE to the remote, you will get an error.

  7. You merge the BRANCH_REBASE to the TEMP branch. Use the following commands

    git checkout TEMP		# Checkout the TEMP branch
    git merge BRANCH_REBASE		# Merge the BRANCH_REBASE to TEMP
    

    You can push the local TEMP branch to the remote with the following command

    git push origin TEMP
    
  8. Now remove the local and remote branch of BRANCH_REBASE with the following commands

    git branch -d BRANCH_REBASE	# Remove local branch
    git push origin :BRANCH_REBASE	# Remove remote branch
    
  9. In order to merge the TEMP to master do the following

    git checkout master
    git merge TEMP
    

    After you checked that everything is ok on master you can delete the TEMP branch

    git branch -d TEMP	# Remove local branch
    git push origin :TEMP	# Remove remote branch
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment