Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git: Merge in only one commit

Tags:

git

branch

Usually, I work with branches in Git, but I don't like to see hundreds of branches in my working tree (Git history). I'm wondering if there is a method in Git to "join" all commits in a branch in only one commit (ideally with a clear commit message).

Something like this:

git checkout -b branch <some work> git commit -a -m "commit 1" <some work> git commit -a -m "commit 2" <some work> git commit -a -m "commit 3" git checkout master git SUPER-JOIN branch -m "super commit" 

After this, only "super commit" will exist in the git log.

like image 633
Ivan Avatar asked Jan 05 '11 10:01

Ivan


People also ask

Can you merge one commit to another branch?

Git Cherry-Pick — Selecting Specific Commits to Merge As you know, when you are working with Git and need to do a merge between two branches, every commit in the source branch will be merged in the target branch without exception.

Can I merge a commit?

Git merging combines sequences of commits into one unified history of commits. There are two main ways Git will merge: Fast Forward and Three way. Git can automatically merge commits unless there are changes that conflict in both commit sequences.


2 Answers

It sounds like you're looking for the --squash option of git-merge:

git checkout master git merge --squash branch -m "super commit" 
like image 180
Greg Hewgill Avatar answered Sep 24 '22 17:09

Greg Hewgill


This can be done using git rebase and squash, or using git merge --squash, see

Git merge flattening

and

git: squash/fixup earlier commit

like image 24
tonio Avatar answered Sep 23 '22 17:09

tonio