0

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?

Jitesh Mohite
  • 31,138
  • 12
  • 157
  • 147

3 Answers3

6

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.

eftshift0
  • 26,375
  • 3
  • 36
  • 60
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.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
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 --

jmunsch
  • 22,771
  • 11
  • 93
  • 114