Sometimes all you want is to reset your commit history and start afresh with your current project state.
Why or when would you do this
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
1. You will loose your commit history.
2. Your team members will be required to reset remote branch tracking to continue working on your project.
git fetch origin
git reset --hard origin/master
Once you have understood the impact of these consequences, it will probably be in your best interests to:
a) Create a backup of your repository.
b) Notify your team members of the intended action.
c) Plan for the action when at a time when it is less disruptive to teammates.
d) 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: Performing a Quick Reset
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 branch
git checkout --orphan new_master
# add all files to git staging area
git add .
# create you intended first commit for the fresh branch
git commit -m "As fresh as a cucumber"
# Delete the parent branch
git branch -D master
# Rename the current orphan branch to original parent branch name
git branch -m master
Once this is done, you can push your changes to the remote repository.
Note: you 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 process.
This is why it is very important to communicate with other collaborators working on your project.
# push changes to remote repository
git push -f origin master
Once 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.
See important considerations section.