1

I have the following structure of my pushes/commits.

UnpushedLocalCommit1  <--- most recent local commit, but not yet pushed
 ^
 |
UnpushedLocalCommit0  <--- committed locally, but not yet pushed
 ^
 |
Commit1 <---origin/master, most recent pushed commit
 ^
 |
Commit0 <---first ever pushed commit

UnpushedLocalCommit0 had the following files

File1 with 2 lines
File2

UnpushedLocalCommit1 has the following files

File1 with 3 lines

Specifically, the most recent local commit changed File1 and did not change File2. I would now like to push a single commit remotely which contains:

File1 with 3 lines
File2

Can this be done?

I have looked at rebasing and git merge. The use cases of these seem to involve multiple branches into one. However, in my case, there is just a single linear branch. So, it is not clear to me if I should be using rebase or git merge.

Squashing commits seems like what would help in my case but is there a guarantee that File1 with 3 lines (and not File1 with 2 lines) and File2 would be the resultant squashed commit? Also, it is not clear to me what happens to UnpushedLocalCommit0 and UnpushedLocalCommit1 after squashing them. Ideally, I want no record of them locally or remotely.

That is, I would like the following structure eventually:

UnpushedLocalCommit2  <--- new commit with File1 with 3 lines and File2
 ^
 |
Commit1 <---origin/master, most recent pushed commit
 ^
 |
Commit0 <---first ever pushed commit
Tryer
  • 3,580
  • 1
  • 26
  • 49
  • 1
    Read matt's Most Excellent response (and the thread he linked to). Basically: Step 1) Square away the "commits" to your local branch (reset --soft, and/or rebase/squash as needed), then Step 2) once you're satisfied, "push" to your remote. – paulsm4 Mar 16 '22 at 17:57
  • Is there a simple rebasing / squash command that does the same as `git reset --soft ` ? My understanding is: `git rebase -i HEAD~2` since I want the previous two commits rolled into one. I have based this understanding from https://www.git-tower.com/learn/git/faq/git-squash – Tryer Mar 16 '22 at 18:24

1 Answers1

3

Your doubts are due to not understanding what Git is or what a commit is. Stop thinking in terms of what each commit "changed". That's just something you made up; Git doesn't traffic in changes. It traffics in commits. A commit is a snapshot of the current state of the entire project. Therefore if you like the current state of the project and you squash any sequence of commits in the parental history of the current state, you will still get that state.

For your task, the simplest way to squash into one commit is a soft reset. So here, you would say

git reset --soft <commit1>
git commit -m 'new message for squashed commits`
matt
  • 515,959
  • 87
  • 875
  • 1,141
  • 1
    See my https://stackoverflow.com/a/59675191/341994. You are having Regret Type 1. – matt Mar 16 '22 at 17:54
  • 2
    And to learn what Git is and what a commit is, see my https://www.biteinteractive.com/picturing-git-conceptions-and-misconceptions/ – matt Mar 16 '22 at 17:56
  • I will surely read your linked to posts (blog + answer). In this case, I am also curious what would happen if I said git reset --soft instead of git reset --soft ? From your line `if you like the current..., you will get that state`, I understand that both commands will essentially provide the same final state. Is this correct? – Tryer Mar 16 '22 at 18:05
  • You specifically said you already pushed commit1. So I don't know what exactly you pushed to, but if you were to rewrite history starting after commit0 you might be rewriting public pushed history. That is a no-no. So I gave you instructions just for rewriting the _local_ part of the history. — So yeah, you could totally do that if you were operating in a vacuum, and in fact I _always_ reset soft back to _before_ commit0 and squash down to a single commit _before_ I first push to a public branch (i.e. make a PR). But the morality of things changes once you've pushed a commit. – matt Mar 16 '22 at 20:19
  • 2
    I am reading your biteinteractive post and it is both informative and humorous. Thank you. – Tryer Mar 17 '22 at 02:05
  • 1
    Sounds like you're the intended audience! Thanks for reading so sympathetically. – matt Mar 17 '22 at 02:08