1

I have 10 commits on my branch with having 15 files changed. Now I want to keep only file changes but not commits.

Means Now I want 15 file changes in one commit.

git reset --hard commit_id
git push --force

The above command will not help us.

Edit:

I want to move my branch to one specific commit, with maintaining all file changes which happened in current commits. Then Want to add all unstash changes in one commit.

Can it be possible?

9
  • Check the duplicate link for a number of ways to squash your 10 commits into a single commit. Commented Jun 20, 2019 at 13:48
  • @TimBiegeleisen: I don't think the above link will help me, can you share the working code for it? Commented Jun 20, 2019 at 14:07
  • git reset --soft HEAD~10; git commit -m "there, 10 squashed commits" Commented Jun 20, 2019 at 14:08
  • Either do an interactive rebase with a squash, or try doing a soft reset. Commented Jun 20, 2019 at 14:10
  • @TimBiegeleisen: I am not getting my question answer dere, also I don't want to mention specific commit numbers. How it will be possible then? Commented Jun 20, 2019 at 14:15

3 Answers 3

9

Allow me to provide the answer that I posted as a comment when the question was marked as a duplicate (and before Tim's answer):

git reset --soft HEAD~10 git commit -m "there, my 10 commits squashed" 

Force-pushing (which is fine) as added by Tim is required if you will place this branch on other remote branches if you had already pushed the removed revisions there.

Sign up to request clarification or add additional context in comments.

4 Comments

Right. And in general, it's also useful to get to know the "git rebase -i HEAD~10" (interactive) command, which lets you squash, reorder, drop, etc., revisions. It can come in very handy.
I won't go as far as to say that rebase -i is not needed.... I would use it depending on the situation.... in this case, I would just use my recipe.
Can I mention commit id, from where I want to squash.?
On the reset --soft? Sure, you can use the ID.
2

Perhaps the easiest approach here would be to just do a soft reset, going back 10 commits, and then recommitting:

git reset --soft HEAD~10 git commit -m 'squash 10 commits into just one commit' git push --force origin your_branch 

The idea behind this soft reset trick is that we move the HEAD pointer back 10 commits. This places all the changes from your 10 commits into the stage, while leaving the working directory the same. Then, when you recommit, you effectively create a commit containing the work from all 10 commits.

Note that a force push would generally be required, since this option rewrites the history of your branch.

Another approach would be to use an interactive rebase, and choose squash for the commits you want to squash. This option would also rewrite history.

3 Comments

Can I mention commit id, from where I want to squash. ?
If you want to squash a certain range of commits which ends before the HEAD commit, then a soft reset won't be an option and you'll have to use an interactive rebase.
That's what I want actually, can you provide the answer for it, I will accept then
1

What this does is creates a new branch "your-branch-3" and then checks out each file from the other branch "your-branch-2":

git checkout master git checkout -b your-branch-3 git diff your-branch-2 --name-only | xargs -I{} git checkout your-branch-2 -- {} 

--name-only

  • output only the file name paths

xargs -I

  • Replace occurrences of {} in the initial-arguments with names read from standard input.

git-checkout branch --

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.