2

I have a local develop branch "dev" and a remote "master" branch. For the "dev" branch, I have a quite complicated commit history(nearly 20 successive commits). I tried to use

"git rebase -i origin/master" 

at "dev", however, I found this process is very error-prone(you have to apply each commit one by one and resolve conflicts..) and frustrating... What I need now is to compress the commit history at "dev" to a single commit and push this big commit to "master" for review and merge. Is there any safe and efficient way to do that? Thank you guys very much!

1
  • 1
    You might want to elaborate on why you're trying to do this. If you have merge conflicts doing this operation, you'll have the same conflicts when you squash the changes and apply them as one commit. Do you want to squash the history into a single commit? Do you want a linear history on the remote master, or do you mind having a merge?
    – djs
    Dec 6, 2011 at 5:22

2 Answers 2

1

I would do something like that:

$> git merge-base dev origin/master
<commit>

Whatever is returned would be the point of merge to be used for squeezing your commits...

Then, assuming you are on your dev branch, just do:

$> git reset <commit>

That would effectively put all the changes of your 20 successive commits back into the "modified files" section in git status...

Then just create a new commit like that (make sure git add --all is what you want here):

$> git add --all
$> git commit -m "my squeezed stuff"

Then fix your conflicts and push to origin...

Hope it helps,

3
  • it not work, since I already merge with "dev" and "master", running $> git merge-base dev origin/master <commit> would return the latest commit both "dev" and "master" have... What I want is just squeeze my commits to new commit and push it to "master" for review.
    – rayeaster
    Dec 6, 2011 at 5:32
  • Well in that case just do a git reset --hard HEAD^ on dev to remove the merge commit and then do what I suggested up there. Does that make sense? Dec 6, 2011 at 7:54
  • thanks billaud, I finally use the simplest way: copy from "dev" to a new local branch "dev2" which is already sync with "master" and commit from "dev2", this way, "dev2" only have a single clean commit instead of a complicated commit history....
    – rayeaster
    Dec 6, 2011 at 8:12
0

If resetting the current branch is problematic, making a new one is a good alternative (and you did that), but you don't need to copy everything.

Once you have created dev2 (based on master), you can checkout dev, and then checkout dev2 without modifying any files(!) of dev currently present in the working directory.
See "switch git branch without files checkout":

git symbolic-ref HEAD refs/heads/dev2
git reset

Then you can add everything and commit.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.