0

First of all on zsh I am using git-auto-fetch plugin. This may messes up if it is automatically take place right after squash is done and led me end up in the rebase.

I just want to update my previous commit with the new one. I believe squashing is solution for this, where commit and squash it with the previous commit; hence latest commit is updated with the new one.

If the latest commit is:

* b5d2f27 Update Contract

Basically, if my latest commit is b5d2f27, and I've modified some files locally. I just want to include those changes in commit b5d2f27.


I have followed: Squash my last X commits together using Git as but it usually ends up in rebase.

git reset --soft HEAD~1
git add -A .
git commit --quiet --no-verify --edit \
   -m "$(git log --format=%B --reverse HEAD..HEAD@{1})"
git push -f
alper
  • 2,919
  • 9
  • 53
  • 102
  • Unclear what the goal is. The phrase "update my previous commit with the new one" is meaningless, and the phrase "Basically I want to remove b5d2f27 and push my commit" leaves one asking: _what_ commit do you want to push, if you just removed it? Do you mean _replace_ the previous commit with the new one? – matt Nov 04 '20 at 15:18
  • Do you mean you made commit A and then commit B and you want to push commit B but _not_ commit A? If so, do you mean you want to push commit B with none of the _effects_ of commit A? Because if you do, that is not what a squash does; a squash just _combines_ the effects of A and B into a single commit C. – matt Nov 04 '20 at 15:19
  • Or do you mean you made commit A and have not made commit B but you want to erase commit A and now make commit B instead? What? Maybe you could draw us a diagram of what the goal is here. – matt Nov 04 '20 at 15:20
  • I want to combine my new commit (that is going to be pushed) with the previous commit as a single commit ; I want to commit B with the affects of commit A. @matt – alper Nov 04 '20 at 15:26
  • Okay! So that is [Regret Type 1](https://stackoverflow.com/a/59675191/341994). Reset soft back to before the previous commit, and now just commit. Done. – matt Nov 04 '20 at 15:27
  • I have done the `git reset --soft HEAD~1` like on my script code; but it keeps the previous commit too and sometimes it ends up in `rebase` – alper Nov 04 '20 at 15:29
  • Yep, because you did not reset back far enough. Look at the example in the link! If you have `A-B-C` and you want to combine B and C, you need to reset soft back to A. – matt Nov 04 '20 at 15:29
  • Ah I was doing if its `A-B-C` and my new commit should be `D` but I was doing the `git reset --soft HEAD~1` operation before commiting `D` – alper Nov 04 '20 at 15:34
  • What do you mean " sometimes it ends up in rebase "? – ignoring_gravity Nov 04 '20 at 15:35
  • Please see : https://gist.github.com/avatar-lavventura/b022cfdd6634dedf91a1986e0054e72e ; after the commit using `git reset --soft HEAD~1` it builds up on top of the previous commit – alper Nov 04 '20 at 15:46
  • have you tried `git rebase -i 1f457a4 `, and then changing `pick` to `fixup`? – ignoring_gravity Nov 04 '20 at 16:01
  • Sorry I did not understand what you mean by ` changing pick to fixup` // I was using `git log --graph --decorate --oneline \$(git rev-list -g --all)` I think its graph guide me wrongly – alper Nov 04 '20 at 17:18
  • I think I found it real problem: `zsh` was doing auto fetching if there is a change in the directory using ( `https://github.com/ohmyzsh/ohmyzsh/tree/master/plugins/git-auto-fetch`) which leads to mess up if its completed before `git push -f ` of the squashed commits completed – alper Nov 04 '20 at 23:48

2 Answers2

2

You could do

git rebase -i HEAD~2

and then, in the interactive window, change the second pick to fixup


Alternatively, if you haven't yet made a new commit but just want to include your latest changes in your previous commit, you can do

git commit --amend
ignoring_gravity
  • 6,677
  • 4
  • 32
  • 65
  • What do you mean by `second pick to fixup` – alper Nov 04 '20 at 23:20
  • When you do `git rebase -i HEAD~2`, you will get an interactive window. In that window, change the second `pick` to `fixup` – ignoring_gravity Nov 05 '20 at 10:38
  • It says: `error: cannot rebase: You have unstaged changes.` along with following github sitatus: `@de1245be rebase-i !4 ?2` – alper Nov 05 '20 at 10:51
  • Just to clarify - your latest commit is `1f457a4 `, and you've modified some files locally (but not staged them). You'd like to include those changes in commit `1f457a4 `. Right? If so, you can just stage them (`git add`) and the amend your last commit (`git commit --amend`) – ignoring_gravity Nov 05 '20 at 11:00
  • Yes exactly! Like I just want to push my new local changes into the latest (which is the previous commit) – alper Nov 05 '20 at 11:03
  • After `git commit --amend` should I do `git push -f ` ? because it remains as `master ⇡1 ⇣1 !1` right after the commit // even after the push my latest changes is not included into the previous commit – alper Nov 05 '20 at 11:10
  • If you had already pushed your previous commit and want to overwrite it, then yes, you will have to do `git push -f` – ignoring_gravity Nov 05 '20 at 11:17
0

You can try

git rebase --interactive [commit-hash]

[commit-hash] - is the hash of the commit just before the first one you want to rewrite from.You can read the whole thing as:

Merge all my commits on top of commit [commit-hash].

Source

Eduard Hosu
  • 11
  • 1
  • 2