Skip to content

Instantly share code, notes, and snippets.

@nbhasin2
Last active August 29, 2015 14:10
Show Gist options
  • Save nbhasin2/25cd67169e8add7b6e22 to your computer and use it in GitHub Desktop.
Save nbhasin2/25cd67169e8add7b6e22 to your computer and use it in GitHub Desktop.
Git General Tips (Rebase & Squash)

##Git Rebase and Squash - Tips

Git Rebase

Say you are on your branch.

  1. Commit your changes (git add .) and (git commit -m "your message")
  2. Checkout master (git checkout master)
  3. Update master (git pull master)
  4. Checkout your branch (git checkout "your branch name")
  5. Rebase changes from master (git rebase master)
  6. Resolve conflicts and your branch should be updated with master.
  7. Push your changes to remote (git push origin yourbranchname)
  • You can also do (git push origin yourbranchname --force) but do it only on your branch and only if you have no other option left.
  • Never do force push on master / develop
  • Similarly if you want to push any branch to remote (git push origin branchname)
  • If you want to push master to remote (git push origin master)

(Not suggested but do it if you don't have any option left)

If you simply want to put changes to master and are really behind then you can also do merge.

  1. Commit your changes.
  2. Checkout master.
  3. Merge your branch with master (git merge yourbranchname)
  4. Checkout yourbranch.
  5. Merge master to your branch to get latest changes. (git merge master)
  6. To push master to

Good websites that you can look at for rebase:-

Git Squash

Say you are on your branch and made 4 commits but now you decide to clean up those commits and combine it in one. Here's how you can do it with interactive rebase (git rebase -i)

  1. git rebase -i HEAD~2

Here HEAD2 means you want to squash last 2 commits for last 3 you will do HEAD3 and so on. Below is an example of output that happens when you enter above command and press enter. I am trying to edit my Readme.md file here.

output -

pick 108ff6f Testing out git sq
pick b83950e Edited Readme and checking git squashing.

# Rebase ba7b4bb..b83950e onto ba7b4bb
#
# Commands:
#  p, pick = use commit
#  r, reword = use commit, but edit the commit message
#  e, edit = use commit, but stop for amending
#  s, squash = use commit, but meld into previous commit
#  f, fixup = like "squash", but discard this commit's log message
#  x, exec = run command (the rest of the line) using shell
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out
  1. Now what you need to do is change all the pick after first one to s or squash like the output below and write the file (:wq) if you are using vim.
pick 108ff6f Testing out git sq
s b83950e Edited Readme and checking git squashing.

# Rebase ba7b4bb..b83950e onto ba7b4bb
#
# Commands:
#  p, pick = use commit
#  r, reword = use commit, but edit the commit message
#  e, edit = use commit, but stop for amending
#  s, squash = use commit, but meld into previous commit
#  f, fixup = like "squash", but discard this commit's log message
#  x, exec = run command (the rest of the line) using shell
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out
  1. Once file is written next screen you will seen is the one below. This simply tells that you are trying to combine commits and here you can change the commit message according to your need.
# This is a combination of 2 commits.
# The first commit's message is:

Testing out git sq

# This is the 2nd commit message:

Edited Readme and checking git squashing.

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Date:      Sat Nov 29 03:25:36 2014 -0500
# Committer: Nishant Bhasin <nishantbhasin@Nishants-MacBook-Pro.local>
#
# rebase in progress; onto ba7b4bb
# You are currently editing a commit while rebasing branch 'master' on 'ba7b4bb'.
#
# Changes to be committed:
#       modified:   README.md
#
  1. Below is my edited message. You can put whatever message you like. Whatever you put after # won't be added in the message. After editing do (:wq) and you are done.
Edited Readme and git squash practice.

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Date:      Sat Nov 29 03:25:36 2014 -0500
# Committer: Nishant Bhasin <nishantbhasin@Nishants-MacBook-Pro.local>
#
# rebase in progress; onto ba7b4bb
# You are currently editing a commit while rebasing branch 'master' on 'ba7b4bb'.
#
# Changes to be committed:
#       modified:   README.md
  1. All done. Now if you check log (git log) you should see only one commit instead of 2.

Good websites that you can look at for git squashing:-

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