Skip to content

Instantly share code, notes, and snippets.

@matiasherranz
Last active December 30, 2016 15:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matiasherranz/83b91a460a20352e9f2b474151d8fbe7 to your computer and use it in GitHub Desktop.
Save matiasherranz/83b91a460a20352e9f2b474151d8fbe7 to your computer and use it in GitHub Desktop.
# 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: https://www.dropbox.com/s/51dwbsrh8bomwxu/Screenshot%202016-12-30%2011.37.51.png?dl=0
--> (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:
# https://www.dropbox.com/s/2brpzj6e3k132dh/Screenshot%202016-12-30%2011.40.50.png?dl=0
# 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:
# https://www.dropbox.com/s/f8uutrof4s36g7g/Screenshot%202016-12-30%2011.41.05.png?dl=0
# 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:
# https://www.dropbox.com/s/15z52dlcqig2v6j/Screenshot%202016-12-30%2011.41.16.png?dl=0
# 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