Showing posts with label clean history. Show all posts
Showing posts with label clean history. Show all posts

Thursday, September 27, 2018

When to amend/squash commits and how to maintain a clean PR

I always like to improve the development process with the git and a strong believer in the clean code.
Something which I always insist during my PR review is - maintain the clean git history by avoiding the unnecessary indivual commits. 

Broadly, we can follow these 2 rules:

  1. amend/squash commits if they don't add semantic value to the PR
  2. Always rebase the PR before you push to the server

Below article exactly reflects my thought on this.



Other nice articles on this:

https://www.atlassian.com/git/tutorials/rewriting-history
https://git-scm.com/book/en/v2/Git-Tools-Rewriting-History

Thursday, July 18, 2013

Maintaining clear git history tree

This is what I have learned from my colleague few days back.

My general workflow with git:
Git pull origin Dev-5.0
Then I would have worked for a day or two
Now, I want to push my changes for last 2 days. But, in mean time many people in my team also would have pushed the code.
Git commit  “last 2 days commits message”
Git pull origin Dev-5.0 -> get the latest code from server again
Git push origin Dev-5.0 -> push your committed changes to the server


But, this is creating a very bad hierarchy tree in git(gitk). Like below













The problem here is, while I am committing my code I don't have the latest code from central repository. This would create a different tree hierarchy in git from my last state, and only when I push my code it will merge your local branch with the central repository. Then it will connect your local branch tree node to the central tree node.

Below is the approach which has worked out for us.

Git pull origin Dev-5.0
Then you might be working for few days
Now you want to commit your changes.
Git stash   -> stash your current changes in local repository
Git pull origin Dev-5.0   -> Get the latest changes from the central repository
Now your local repository has latest changes
Apply your stashed changes on latest changes which we have pulled from central repository.
Git stash pop -> This will apply stashed changes to current changes.
If any merge conflicts, resolve them
Git commit  “my changes”
Git push origin Dev-5.0    => This would fail, if somebody else has pushed the code in mean time after your last pull, so we need to pull the code again and proceed with the push.

This is how it looks, if we maintain the above procedure.