Git Squash and Delete Previous Commits

Perhaps I misunderstood how GIT works.

I started git rebase -i HEAD~10and I was able to put 10 commits into one. The problem is that all crushed commits still exist, and I thought they would be deleted after merging them all into one.

Is this the expected result? If so, can I rewrite history to remove useless commits (since these changes are already in the commit, all previous commits were crushed)?

+5
source share
3 answers

When you started the interactive relocation session, you should have been offered a list of the last 10 commits from the current branch:

git rebase -i HEAD~10

pick as2i8dw first commit
pick ee361eb second  commit
...
pick b2762sx most recent commit

You need to modify this file as follows:

pick as2i8dw first commit
squash ee361eb second commit
...
squash b2762sx most recent commit

git commit, . , git log, as2i8dw .

, , , ?

+4

,

- ( ), , , .

git reset --soft.
HEAD 10 :

git reset --soft HEAD~10
git commit -m "squashed 10 commits"
+1

: :

git rebase -i HEAD~10
# Retain the first commit from below( as pick) and change the rest of the `pick` to `squash`
# After your rebase is successful
git log
# You can see all your commits squashes to one commit

, git pull , , ( , , ) , , .

Better to use git push -ffor your remote branch if you are sure that you have no new changes.

PS: If you have new changes in the remote branch, it is better:

git rebase origin remote_branch 

and then crush your commits.

0
source

Source: https://habr.com/ru/post/1599714/


All Articles