git - How to checkout all changed files between two commits

375 Views Asked by At

I have a branch A and a branch B. Branch B is three commits ahead of A.

I can use git diff to list the changed files between A and B.

But my question is: How can I checkout all those changed files between A and B while I am on A, then commit them altogether into A as one commit?

4

There are 4 best solutions below

2
On BEST ANSWER

If B is strictly ahead of A (and not behind it), then you can simply run "merge --squash":

git merge --squash B
git commit
0
On

Simply pull the B branch into A then, merge last three commits as one.

$ git checkout A
$ git pull origin B
$ git log
# Now top 3 commits are B's commit

# now back to 3 commits but exists all changes of that 3 commits (soft reset)
$ git log
# copy the last commit-hash of A (before pulled)                    

$ git reset --soft <commit-hash>

# now do one commit with all changes
$ git add .
$ git commit -m 'add last 3 commits of B as one in A'
$ git push origin HEAD         # push the changes to remote
0
On

Rebase the commits onto A and then squash them togeter to one commit with

git rebase -i HEAD~3
0
On

Get the commits from the B through rebase

git rebase B

Now squash three commits in to one

git rebase -i HEAD~3