Advanced Git Commands: Rewriting History
Once you've learned the basics of the Git command line, you have some powerful commands at your fingertips... and it only gets better from there.
Join the DZone community and get the full member experience.Join For Free
when developers use git, they often use a combination of a graphical user interface and the command line. a gui has a low barrier to entry, but many of us want to leverage the power that git cli. once you've learned the basics of the git command line, you have some powerful commands at your fingertips. and it only gets better from there. let's take a look at some advanced git commands that will really leverage the power of git.
this article assumes you know how to branch, commit, and merge. but then again, it assumes only that. the rest will be explained.
the command "git commit --amend" will allow you to add staged changes to the previous commit. this is useful if you made some new changes that logically belong to the last commit you made. instead of adding a new commit to the log, you can add the changes to the last commit.
let's say you edit some file and run these git commands:
git add readme.md git commit -m "add installation instructions to the readme"
now, if you forgot something and edit the readme again, you could create a new commit, making your log look something like this:
acb6767e3 add more installation instructions to the readme 554dd2e66 add installation instructions to the readme
you could write a better commit message for the last commit, but in some cases, having a single commit is better. this is actually quite easy. after creating your first commit and editing the file again, just run this command:
git add readme.md git commit --amend --no-edit
the --amend flag will add the staged files (readme.md, in our case) to the last commit. the --no-edit flag will avoid having your editor opened. if you know you want to edit the commit message, omit this flag.
amending a commit is a basic way of rewriting history: you added an entry to the git log, but with the --amend flag, you're changing that entry. rebasing is an even more powerful way of rewriting history. what a rebase will do is reapply a set of commits on a given branch.
let's say you have a master branch with three commits (each circle represents a commit):
when you branch off this and add two commits to your branch, you get this:
but in the meantime, development continues, and new commits have been added to master:
before you merge, you might want to add the latest commits on master to your branch, just so you're sure everything keeps working. you could merge master into your branch, but this results in complicated git logs, especially in larger teams. the convention in the world of git is to do a rebase.
now, because git is a distributed version control system, your local repository still contains the master branch without the new commits. to clarify, let's compare the local and the remote repositories:
so first we need to get the latest version of master:
git checkout master git pull
now we're in sync with the remote master. let's compare them again:
next, we rebase by running:
git checkout feature/my-feature-branch
git rebase master
this will result in the following situation:
so what happened here? well, to understand this, you need to know something about the inner workings of git. each git commit points to a parent commit (i.e., the previous commit). what this means is that prior to the rebase, your branch looks like this to git:
a rebase is done in several steps. first, git rewinds your commits, leaving you with this situation:
next, git adds the missing commits on master:
finally, your branch commits are applied:
now it's like you've only just recently branched and added the commits to the most up-to-date version of the master branch. this will make a subsequent merge easier and your git log cleaner.
if you always merge with this command,
git checkout master git merge feature/my-feature-branch
then you'll get a log that looks like this:
this gives a very readable git log.
if you prefer, you can merge using fast-forward merges, by using this command:
git checkout master git merge feature/my-feature-branch --ff-only
this will give you a git log without separate merge commits:
so, just remember to rebase before merging:
git checkout master git pull git checkout feature/my-feature-branch git rebase master git checkout master git merge feature/my-feature-branch
but let's take it a step further. let's look at interactive rebasing to really rewrite history.
remember how i mentioned git will reapply your commits when you rebase? with interactive rebasing, you can change the way git applies your commits. let's look at an interactive rebase in action.
once again, we'll start from this situation in our local repository:
now let's rebase interactively:
git rebase -i master
this will open an editor-in my case, vim:
here you see the list of commits that git will reapply on the master branch. the second column gives you the sha of the commit. the third column shows the commit message you entered. but it's the first column that's most interesting. beneath these lines is a list of commands that you can use in this first column. these commands allow you to interact with the rebase.
in the above example, you can see we've added a twitter connector, had some problems, made the layout look good, and finally removed an image. a better log could exist of only two commits: adding the connector and providing the layout. let's assume the last commit was an error and the image shouldn't have been removed.
to do this, we can edit the lines to look like this:
let's run over the changes.
we haven't changed the first line, which means git will just apply the commit as is.
we've edited the second and third line to "squash" into the previous commit. this means git will do the same, like our "git commit --amend" command, and the first three commits will be squashed into one commit.
the fourth line is preceded by an "r," meaning we want to reword the commit message. you can see there's a spelling error in the commit message. with interactive rebase, we can tell git we want to change the commit message.
in the last line, we tell git that we want to drop that commit. when rebasing, git will not apply that commit.
when we save this file and close our editor, git will start the rebase. a new editor will open because we need to tell git what the commit message is for the squashed commit and for the reworded commit.
after git finishes, we can check the logs (git log --oneline) and see our new history:
you can see that our master is at commit 10b8889 and our branch now contains two commits.
a word of caution
we've seen how git allows you to change history after the fact. once you start applying these techniques, you'll realize how powerful git is. be warned that this comes with some responsibility. rewriting history can severely hinder your team members, as they'll find their repository and its branches out of sync. getting that synchronized again is another topic but requires some effort and knowledge. this is why git won't allow you to push an already-pushed branch whose history you have changed unless you specifically specify the "--force" flag.
but even with that flag, you should take care not to mess up your colleagues' work. one rule of thumb that goes a long way is to never rewrite the history of the master branch. that way, everyone can always get the latest version of the master branch and rebase their feature branch on that.
Published at DZone with permission of Peter Morlion, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.