Git is an awesome story teller that tells your project’s story through every commit. However, at times, all you want is for it to forget your project’s past and start the story afresh.
Sometimes all you want is to reset your commit history and start afresh with your current project state.
This is very important especially in the following scenarios:
- you want to remove sensitive data from the repository.
- you want to clean up a messy history or experiemental commits.
- you want to start afresh with the current project state.
- you want to reduce the size of the repository.
Important considerations
Before proceeding with this action, it is important to understand:
-
You will loose your commit history.
-
Your team members will be required to reset remote branch tracking to continue working on your project.
git fetch origingit reset --hard origin/master
Once you have understood the consequences of this action, it will probably be in your best interests to:
- Create a backup of your repository.
- Notify your team members of the intended action.
- Plan for the action when at a time when it is less disruptive to teammates.
- Record any important information such as important commit messages, versions or branches.
Once you have gone through these points, and are comfortable with the decision you can proceed.
Method 1: Using an orphan branch
The easiest approach, is to use an orphaned branch. An orphan branch does not keep parent branch commit history, Once the orphaned branch has been created, the original branch can be deleted. The orphaned branch can then be renamed to match the parent branch name.
Easy right ?
# create a new orphaned branchgit checkout --orphan new_master
# add all files to git staging areagit add .
# create you intended first commit for the fresh branchgit commit -m "As fresh as a cucumber"
# Delete the parent branchgit branch -D master
# Rename the current orphan branch to original parent branch namegit branch -m masterOnce this is done, you can push your changes to the remote repository.
NOTEYou will have to force push the local changes to the remote repository. This will overwrite any changes made on the remote repository prior to this action. This is why it is very important to communicate with other collaborators working on your project.
# push changes to remote repositorygit push -f origin masterOnce you have pushed the changes to the remote repository, all teammates that were working on your project before the action will be required to reset their working directories and staging area. important considerations
Method 2: Deleting .git and remote repository
Another alternative is to clone the remote repository to your local environment.
Once cloned, delete the .git folder in your local copy. This will delete project tracking in your local copy.
You can then delete the project in your git server and create a new fresh project.
You can then go ahead and initialize a new repository in your working copy, and then add the new remote project. Go ahead to make your first commit and push to the new remote project.
# initialiaze a new repository after deleting .git foldergit init
# add the remote project to the repository
# make your first commit to be pushed to the remote repositorygit add .git commit -m "Initial commit"
# push the new fresh changesgit push -u origin master