1

So I have the dev branch which currently has a lot of temp commits. Is it possible somehow to merge all those commits in to one? Or recreate branch with code from last commit as a new one?

user1692333
  • 2,461
  • 5
  • 32
  • 64

4 Answers4

2

Yes it is possible. If you have following situation:

a--b--c (dev)
\
 d--e (temp)

the command

git merge --squash branchname

will result in

a--b--c--f

where f contains the changes done in d and e

Michael Mairegger
  • 6,833
  • 28
  • 41
1

Two ways

1. This will create a new branch -

Step 1: create a new branch from where you have created dev branch

git checkout master
git branch -d new_branch

Step 2: checkout to that branch

git checkout new_branch

step 3: merge dev with --squash flag

git merge --squash dev

step 4: add commit and push

git add .
git commit -m'commit message'
git push origin new_branch

2. One commit in same branch (tricky way)

n is number of commits you want to edit

$ git rebase -i HEAD~n

here you can edit the commits and squish them in one commit

Deepak Mahakale
  • 22,834
  • 10
  • 68
  • 88
0

You can do that with a rebase in git.

https://git-scm.com/book/en/v2/Git-Tools-Rewriting-History

Here is a git tutorial how to rebase a branch to remove unused commit messages.

René Höhle
  • 26,716
  • 22
  • 73
  • 82
0

Simple solution:

git rebase -i <after-this-commit>

Squash your commits

Recently, I faced the same problem, I had 41 commits on my pull request, see here. I will only touch something what I have implemented.

For instance, if you have 3 commits, you can squash them and make into one using interactive rebase. See below command:

$ git rebase -i HEAD~n

For example, you want to squash 3 commits:

$ git rebase -i HEAD~3

Now, you see an interactive rebase interface, where you can write reword/pick on first commit and squash on rest of them. Please see this video for better understanding.

Squashing commits video

This is easy, will not take longer. I worked on it.

See this post Squash my last X commits together using Git

Community
  • 1
  • 1