Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to roll back Git repo to first commit and delete all history

I'm learning how to use git these days and I had to do many hit-and-misses. Thus I needed to delete and create anew my remote and local repos. Is there a way to roll back to the first commit of the repo and delete all history after that? Basically a clean slate to experiment on.

like image 345
Aleksandar Savkov Avatar asked May 11 '13 17:05

Aleksandar Savkov


People also ask

How do I revert back to a first commit?

The git revert command will undo a commit so you can return a repository to the previous commit. Instead of deleting the commit, revert will create a new commit that will reverse the changes of a published commit. This preserves the initial commit as a part of the project's history.

Can you delete git commit history?

If you commit sensitive data, such as a password or SSH key into a Git repository, you can remove it from the history.

How do I revert all changes to a specific commit?

To undo changes associated with a specific commit, developers should use the git revert command. To undo every change that has happened since a given commit occurred, use git reset.


4 Answers

I don't know of any way to do exactly what you're asking (one can roll back to first commit, but not delete all history, since the history will at least contain that initial commit.)

If I were you I'd just delete the remote repo and the .git directory of the local repo, and start over with git init.

The closest that I can get to what you're asking for would be to rollback all but the first commit. To do that, you'd first find the SHA1 of the first commit, for example:

% git rev-list --max-parents=0 --abbrev-commit HEAD
aa8119f

...and then run either

% git reset aa8119f

...or

% git reset --hard aa8119f

...depending on whether you want to preserve or discard all the changes made since that initial commit. (The above assumes that you have only one branch. If not, you'll also have to delete any other branches you have with git branch -d <BRANCHNAME>.)

Finally, you'd run

% git push -f

(I hope that you realize that git push -f is a no-no whenever you're pushing to a repo that is shared with others.)

Unfortunately, as already explained, this approach does not delete all the history.

If this is something you'll want to do often, I'd recommend that, immediately after git init, you run something like

% git commit --allow-empty --allow-empty-message -m ''
% git tag -a -m '' ROOT

This will put an empty commit at the root of your history, and tag it with a tag named ROOT. Then you can do something like

% git reset ROOT

or

% git reset --hard ROOT

to bring you back to that first empty commit.

To get a good handle on what git reset does, I recommend reading this.

like image 128
kjo Avatar answered Oct 13 '22 00:10

kjo


You could reset to the first commit:

"How to show first commit by 'git log'?" describes how to find the first commit:

git log --pretty=format:%H | tail -1

(works only if there is no multiple root branches)

git reset --hard yourFirstCommitSHA1

Note that after the reset, to really get a clean slate, you could simply git init a new repo and copy the content of your first commit you just reset to (and add and commit in that new repo)

like image 23
VonC Avatar answered Oct 13 '22 01:10

VonC


Just blow away the .git directory once you've got your first commit checked out. As such:

git checkout <first-commit-sha>
rm -rf .git
git init
git add -A
git commit -m 'Initial Commit'
like image 11
GoZoner Avatar answered Oct 13 '22 00:10

GoZoner


To do a clean reset you want to eliminate the logs and any config changes and everything, I'd do this by fetching just the master root into a new repo:

git tag master-root $(git rev-list --topo-order master|sed '$!d')
git init ../reset-my-repo
cd ../reset-my-repo
git fetch $OLDPWD master-root
git checkout -B master FETCH_HEAD

(i.e. pretty much what VonC said)

(added --topo-order to protect against bad timestamps)

like image 4
jthill Avatar answered Oct 13 '22 02:10

jthill