1

I would like to push changes to a remote Git repository in such a way that all commits to the local repository that were effected since it was last pulled are not recorded remotly.

In effect, the multiple local change log messages should be forgotten (or summed up e.g. as "big change 2" as in the example below) once I push into the remote registry. Many "small" local commits should appear as one "big" remote one. So I want to read the log messages something like this:

git log remote branch
# big change 1
git log branch
# small change 1
# small change 2
# big change 2
git push remote branch
git log remote branch
# big change 1
# big change 2

Is this possible with the git command, and if so, how?

rookie09
  • 736
  • 1
  • 6
  • 18
  • 1
    I think you are looking for [this](https://stackoverflow.com/a/5201642/3727050) – urban Nov 24 '17 at 09:18
  • 1
    Possible duplicate of [Squash my last X commits together using Git](https://stackoverflow.com/questions/5189560/squash-my-last-x-commits-together-using-git) – Liam Nov 24 '17 at 09:37
  • @urban I'd recommend you read [How should duplicate questions be handled?](https://meta.stackexchange.com/questions/10841/how-should-duplicate-questions-be-handled) – Liam Nov 24 '17 at 09:38
  • 1
    @Liam Will do, however, I avoid flagging questions before my 2nd coffee in the morning... Health and Safety regulations :) – urban Nov 24 '17 at 10:26

1 Answers1

2

Yes, it's called squashing. I usually use the interactive rebase feature for that.

First, find the commit before the oldest commit you want to be part of the resulting squashed commit:

$ git log --oneline
701c7f8 (HEAD -> master) commit c
4c0270a commit b
8ca1f6e commit a
bb0a92c (origin/master) previous commit

Then do git rebase -i <commit>:

$ git rebase -i bb0a92c

Your configured editor will open up presenting you a list of the commits that are part of the rebase operation, starting with the oldest:

pick 8ca1f6e commit a
pick 4c0270a commit b
pick 701c7f8 commit c

# Rebase bb0a92c..701c7f8 onto bb0a92c (3 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

Change the first three lines to look like this:

pick 8ca1f6e commit a
squash 4c0270a commit b
squash 701c7f8 commit c

Or, actually, the first letter suffices:

pick 8ca1f6e commit a
s 4c0270a commit b
s 701c7f8 commit c

Save and exit the editor. Another editor instance will give you the opportunity to edit the commit message for the resulting squashed commit and the end result will be one commit encompassing the changes of commits 8ca1f6e, 4c0270a, 701c7f8.

anothernode
  • 5,100
  • 13
  • 43
  • 62
  • So you squash on the local repository and them push it to remote without further special consideration, right? – rookie09 Nov 27 '17 at 08:13
  • Yes, exactly! Just be careful not to squash or otherwise change any commits that were already pushed earlier unless you know exactly what you are doing. – anothernode Nov 27 '17 at 10:19