Squashing commits: Difference between revisions

From OPeNDAP Documentation
⧼opendap2-jumptonavigation⧽
mNo edit summary
Line 1: Line 1:
= Squashing Commits of a branch to make things tidy =
= Squashing Commits of a branch to make things tidy =
== Why ==
Lots of commit messages are about fixing spelling errors, etc., and often it's better to combine a branch's commit messages into a smaller set of commits. This is how to use 'git rebase' for that, even when you have already pushed the branch to a repo like GitHub. This should work with a merged code, too, but fiddling with the master branch should not be done unless absolutely necessary.
== How ==
First, while you're on your branch, use 'git log' to find the commit hash for the commit right before the first commit of you branch. In the git log output below (which has some stuff elided) it has the info **(origin/master, master)**. Copy the first 8 or so characters of that commit hash.


  <nowiki>[nori:hello_world jimg$] git log
  <nowiki>[nori:hello_world jimg$] git log
Line 9: Line 15:
     Merge branch 'hack' of https://github.com/jgallagher59701/hello into hack
     Merge branch 'hack' of https://github.com/jgallagher59701/hello into hack


commit de1b5fee05159d4a0ba35a189f12e94ba71dfe70
...
Author: James Gallagher <jgallagher@opendap.org>
Date:  Wed Sep 30 12:39:13 2020 -0600
 
    Namespace finagling
 
commit 1037dfa76b3bd8785b79f6f7a973209692f125a3
Author: James Gallagher <jgallagher@opendap.org>
Date:  Wed Sep 30 12:39:13 2020 -0600
 
    Namespace finaggling
 
commit 19c0052fea1162145d4ae483fc7cf07e61a244ab
Author: James Gallagher <jgallagher@opendap.org>
Date:  Wed Sep 30 12:38:08 2020 -0600
 
    Comments


commit e6239975aaa6bf18d44a113873100df48d46087e
commit e6239975aaa6bf18d44a113873100df48d46087e
Line 39: Line 29:
     Initial commit</nowiki>
     Initial commit</nowiki>


Use 'git rebase --interactive f1302d5f3'
Now, use 'git rebase --interactive f1302d5f3'


Which will show (in the Git EDITOR):
Which will show (in the Git EDITOR):
Line 89: Line 79:
[nori:hello_world jimg$]</nowiki>
[nori:hello_world jimg$]</nowiki>


But... This won't work to push to GitHub because git will want us to pull first. To for the push, don't use `--force` but instead use this magic: 'git push origin +hack'. Yes, the magic is to include the word 'origin' and to use a plus sign, '+hack'.
But... This won't work to push to GitHub because git will want us to pull first. To for the push, don't use `--force` but instead use this magic: 'git push origin +hack'. Yes, the magic is to include the word 'origin' and to use a plus sign, '+hack'. That will look like:
 
<nowiki>[nori:hello_world jimg$] git push origin +hack
Counting objects: 3, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 463 bytes | 463.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://github.com/jgallagher59701/hello.git
+ 48a898d...94eba83 hack -> hack (forced update)</nowiki>
 
== What can go wrong ==
If you get scared and bail out of the editor with the 'pick' etc., lines, you'll be stuck in 'rebase mode.' You can use 'git rebase --continue' or 'git rebase --abort' to get out of that state and continue or abort.

Revision as of 19:17, 30 September 2020

Squashing Commits of a branch to make things tidy

Why

Lots of commit messages are about fixing spelling errors, etc., and often it's better to combine a branch's commit messages into a smaller set of commits. This is how to use 'git rebase' for that, even when you have already pushed the branch to a repo like GitHub. This should work with a merged code, too, but fiddling with the master branch should not be done unless absolutely necessary.

How

First, while you're on your branch, use 'git log' to find the commit hash for the commit right before the first commit of you branch. In the git log output below (which has some stuff elided) it has the info **(origin/master, master)**. Copy the first 8 or so characters of that commit hash.

[nori:hello_world jimg$] git log
commit 48a898d0830fbf8a795204993bbd572b83e6557c (HEAD -> hack, origin/hack)
Merge: de1b5fe 1037dfa
Author: James Gallagher <jgallagher@opendap.org>
Date:   Wed Sep 30 12:40:34 2020 -0600

    Merge branch 'hack' of https://github.com/jgallagher59701/hello into hack

 ...

commit e6239975aaa6bf18d44a113873100df48d46087e
Author: James Gallagher <jgallagher@opendap.org>
Date:   Wed Sep 30 12:37:40 2020 -0600

    Added an error.

commit f1302d5f3549937ddc770b06e7316fc11e5079ae (origin/master, master)
Author: James Gallagher <jgallagher@opendap.org>
Date:   Wed Sep 30 12:32:19 2020 -0600

    Initial commit

Now, use 'git rebase --interactive f1302d5f3'

Which will show (in the Git EDITOR):

pick e623997 Added an error.
pick 19c0052 Comments
pick de1b5fe Namespace finagling
pick 1037dfa Namespace finaggling

# Rebase f1302d5..48a898d onto f1302d5 (4 commands)
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
# d, drop = remove commit
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out

Change the 'pick' word to 'squash'

pick e623997 Added an error.
squash 19c0052 Comments
squash de1b5fe Namespace finagling

Now we have:

[nori:hello_world jimg$] git log
commit 94eba834437908e738f80c80403d2e95a6f11fe2 (HEAD -> hack)
Author: James Gallagher <jgallagher@opendap.org>
Date:   Wed Sep 30 12:37:40 2020 -0600

    Added an error.

commit f1302d5f3549937ddc770b06e7316fc11e5079ae (origin/master, master)
Author: James Gallagher <jgallagher@opendap.org>
Date:   Wed Sep 30 12:32:19 2020 -0600

    Initial commit
[nori:hello_world jimg$]

But... This won't work to push to GitHub because git will want us to pull first. To for the push, don't use `--force` but instead use this magic: 'git push origin +hack'. Yes, the magic is to include the word 'origin' and to use a plus sign, '+hack'. That will look like:

[nori:hello_world jimg$] git push origin +hack
Counting objects: 3, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 463 bytes | 463.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://github.com/jgallagher59701/hello.git
 + 48a898d...94eba83 hack -> hack (forced update)

What can go wrong

If you get scared and bail out of the editor with the 'pick' etc., lines, you'll be stuck in 'rebase mode.' You can use 'git rebase --continue' or 'git rebase --abort' to get out of that state and continue or abort.