How to keep your Git history “clean”

Matej Minárik
1 min readJan 15, 2016

--

I am not going to write about how great Git is for code versioning, sharing, maintaining, etc. If you are a programmer and you do not know, what I am talking about, you should stop reading right now and check out that link.

I think, that every programmer who uses Git came across an odd situation, when he/she committed changes but the “git push” command failed with an error message that said, that you needed to pull first. The most straightforward solution to this problem is to pull and merge your changes with remote changes, however then the history will look something like this:

6. c
5. | \
4. c |
3. | c
2. |/
1. c

Is there something you can do about it? Let’s try rebasing:

$ git stash      # Optional, if you have some working changes
$ git pull --rebase origin current_working_branch
$ git push origin current_working_branch
$ git stash pop # If you called git stash

What about our history?

c
|
c
|
c

Not only, that it is more appealing, but also it is much clear for other programmers, who work with the same repository. The “rebase” option simply tells git, to first get remote changes and apply your committed changes on top of them. If rebasing fails due to some conflicts, conflicts need to be resolved and then you can continue rebasing like this:

$ git rebase --continue

In order to get more information, you should totally check official documentation page.

--

--

Matej Minárik

I work at Agile-ICT (CZ) as a web developer. Currently interested in Ruby on Rails, AngularJS, JavaScript, and CSS3. I am also CXO at Stewardo.