user1692333
user1692333

Reputation: 2597

How to "clone" branch as one commit?

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?

Upvotes: 1

Views: 290

Answers (4)

Deepak Mahakale
Deepak Mahakale

Reputation: 23661

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

Upvotes: 1

user4398985
user4398985

Reputation:

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

Upvotes: 0

Michael Mairegger
Michael Mairegger

Reputation: 7301

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

Upvotes: 2

Ren&#233; H&#246;hle
Ren&#233; H&#246;hle

Reputation: 27285

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.

Upvotes: 0

Related Questions