Skip to content

Quick git tips: fine-grain control over your commits

“Korbys Bouncy Balls” CC BY 2.0

Here is a second delivery of quick git tips, i.e. easy-to-incorporate tips that can have a significant impact on your workflow. Nothing hidden or convoluted here, these tips are extracted and summarized from the official Pro Git Book.

Choose what parts of a file will be committed

When you have made a lot of changes to a certain file and you wish to only commit a part of those changes. This is possible thanks to the git add --patch command. This allows you to select which chunks are moved to the staging area, leaving the rest of them unstaged.

$ git status
On branch master
Changes not staged for commit:
    modified:   someFile.txt
$ git diff
+I want to commit this
 random stuff random stuff random stuff random stuff random stuff 
 random stuff random stuff random stuff random stuff random stuff 
+I do not want to commit this yet
$ git add --patch
...
# We select yes (y) for the first chunk, no (n) for the second chunk. Output omitted for brevity
$ git status
On branch master
Changes to be committed:
    modified:   someFile.txt

Changes not staged for commit:
    modified:   someFile.txt

$ git diff --cached # --cached shows the diff for staged changes
 random stuff random stuff random stuff random stuff random stuff 
+I want to commit this
$ git diff  
 random stuff random stuff random stuff random stuff random stuff 
+I do not want to commit this yet

The reset operation can also benefit from this principle, by doing git reset --patch. As a result, only the selected chunks will be moved back to the unstaged area.

Reorder commits

The order of commits can be easily changed thanks to the almighty interactive rebase, git rebase -i.

If we have the following set of commits (from older to newer):

[commit1]->[commit2]->[commit3]->[commit4]->[commit5]

And we now that the logical order should be:

`\[commit1]->[commit3]->[commit4]->[commit5]->[commit2]

<br />It is possible to do:

```shell
$ git rebase -i commit1^ # Notice the ^ at the end, indicating the parent of commit1

Then, we get a list of commits and the action to apply. We simply want to pickthem (like if we were cherry-picking), so there is no need to modify the action. What we do want is to change the order, so we do exactly that.

Before

pick commit1
pick commit2
pick commit3
pick commit4
pick commit5
pick commit6

After reodering

pick commit1
pick commit3
pick commit4
pick commit5
pick commit2

Save and exit, and git will re-apply the commits in the order that you selected.

Move last N commits to another branch

If you are working on a branch that it is no longer relevant, and your relevant work is on the last commits that you have made on such branch, the easiest solutions is to move those commits to another branch. In this manner, you do not have to deal with rebasing/merging the deprecated branch.

[commit1]->[commit2]->[commit3] master
[stupidCommit1]->[stupidCommit2]->[goodCommit1]->[goodCommit2] deprecatedBranch

Once again, almighty git rebase comes to the rescue.

$ git checkout deprecatedBranch
$ git rebase -i goodCommit1^ --onto master

What we are doing here is saying that we want all commits from goodCommit1 to the most current one, goodCommit2, to be moved onto the master branch. We do not need the other commits. The new situation is:

[commit1]->[commit2]->[commit3] master
[commit1]->[commit2]->[commit3]->[goodCommit1]->[goodCommit2] deprecatedBranch

Now we can stay as we are, or safely merge into master, as any conflict would be solved in the rebase operation.

$ git checkout master
$ git merge deprecatedBranch

Published inGit

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *