Skip to content

Instantly share code, notes, and snippets.

@matiasherranz matiasherranz/
Last active Dec 30, 2016

What would you like to do?
# Create a branch to play safe in
--> (git: master m) $ git checkout -b rebase_example
Switched to a new branch 'rebase_example'
# Create a file
--> (git: rebase_example m) $ touch some_file.txt
# Add it to git
--> (git: rebase_example ? m) $ git add some_file.txt
# Commit the initial file
--> (git: rebase_example * m) $ git commit some_file.txt -m "Added some file"
[rebase_example a8f9092] Added some file
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 repo/some_file.txt
# Let's put some content in (some we want to keep, some we didn't really want to commit)
# Looking like this:
--> (git: rebase_example m) $ nano some_file.txt
# ... and commit it!
--> (git: rebase_example m) $ git commit some_file.txt -m "Added file content"
[rebase_example 5524b82] Added file content
1 file changed, 3 insertions(+)
# Oh, no! We committed some debug stuff! Let's remove it (just the first piece)
--> (git: rebase_example m) $ nano some_file.txt
# ...and commit
--> (git: rebase_example m) $ git commit some_file.txt -m "Removed unwanted content"
[rebase_example 011a198] Removed unwanted content
1 file changed, 1 insertion(+), 1 deletion(-)
# Oh, no! We forgot some of the unwanted debug stuff! Let's remove this too
--> (git: rebase_example m) $ nano some_file.txt
# ...and commit again
--> (git: rebase_example m) $ git commit some_file.txt -m "Removed unwanted content (now for real)"
[rebase_example d14be56] Removed unwanted content (now for real)
1 file changed, 2 deletions(-)
# We now have only what we want inside the file, let's check
--> (git: rebase_example m) $ cat some_file.txt
Added some content
# Looking nice! Let's see how our commit history looks like now
--> (git: rebase_example m) $ git lola
* d14be56 (HEAD -> rebase_example) Removed unwanted content (now for real)
* 011a198 Removed unwanted content
* 5524b82 Added file content
* a8f9092 Added some file
# Oh, darn, those commits shouldn't be there. Let's fix hour history:
# [Note] HEAD, in Git, points to the current status hash / commit of
# our local branch).
--> (git: rebase_example) $ git rebase -i HEAD~4
# This will open:
# Let's pick the commits to pick and the ones to squash, replacing "pick" with
# "squash" or simply "s" on the commits we don't want to keep and save the file
# Then another file will open, containing all the commit messages of the squashed commits
# and the first picked one they'll be squeashed into, like this:
# Now, simply leave the comment that expresses what you did (in my case, simply
# "Added file content", and remove the rest. You can also write something else.
# Whatever is more meaningful for you (and your team), like this:
# Save the file. We are almost there!
# If you pushed some of the commits, a "push -f" will be necessary (in order to re-write
# history in the remote), and is often used for rebasing / squashing.
# Push and you are good to go! :)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.