Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redoing Commit History in GIT Without Rebase

Tags:

Since asking my last question which turned out to be about rebasing with GIT, I have decided that I don't want to rebase at all. Instead I want to:

  1. Branch
  2. Work work work, checking in and pushing at all times
  3. Throw out all of those commits and pretend they never happened (so one clean commit at the end of work)

I do this currently by copying the files to a new directory and then copying them back in to a new branch (branched at the same point as my working branch), and then merging that into master or wherever.

Is this just plain bad and why? More important: Is there a better/GIT way to do this? git rebase -i forces me to merge (and pick, and squash).

like image 428
Dan Rosenstark Avatar asked Feb 23 '10 18:02

Dan Rosenstark


People also ask

How do I clean up commit history?

If you have been lazily writing multiple vague commits, you can use git reset --soft <old-commit> to make your branch point to that old commit. And as we learned, Git will start by moving the branch pointer to it and stops right there. It won't modify the index or working directory.

How do I rewrite a previous commit?

You can modify the most recent commit in the same branch by running git commit –amend. This command is convenient for adding new or updated files to the previous commit. It is also a simple way to edit or add comments to the previous commit. Use git commit –amend to modify the most recent commit.

Can you go back to previous commit in git?

The Git commit process provides a point-in-time snapshot (PIT snapshot) of the version-controlled files at the time of every change. An administrator can roll back the code repository to a previous commit -- that point-in-time copy -- in several ways, depending on the end goal. One approach is the git reset command.


2 Answers

The easiest thing to do is a soft reset.

So checkout your topic branch:

git checkout -b topic master

work, work, work.

git commit
git commit
git commit
git commit

Happy with this, you can make a new single commit on top of master

git reset --soft master
git commit

Now merge to master (it will be a fast-forward) and tidy up the topic branch. (Note that you don't need to do this if you are prepared to remember or tag where master was and just work on master without branching, you could have just done git reset --soft old-master and git commit and you wouldn't need these last clean-up steps.)

git checkout master
git merge topic
git branch -d topic
like image 135
CB Bailey Avatar answered Sep 25 '22 02:09

CB Bailey


You can also use git merge with the --squash option.

like image 24
Ionuț G. Stan Avatar answered Sep 25 '22 02:09

Ionuț G. Stan