Using Rebase this
will allow you to remove one or more consecutive commits
Example git log
Number
|
Hash
|
Commit Message
|
Author
|
1
|
2c6a45b
|
(HEAD) Adding public method to
access protected method
|
Tom
|
2
|
ae45fab
|
Updates to database interface
|
Contractor 1
|
3
|
77b9b82
|
Improving database interface
|
Contractor 2
|
4
|
3c9093c
|
Merged develop branch into master
|
Tom
|
5
|
b3d92c5
|
Adding new Event CMS Module
|
Paul
|
6
|
7feddbb
|
Adding CMS class and files
|
Tom
|
7
|
a809379
|
Adding project to Git
|
Tom
|
Using Rebase
Using the git log above we want to remove the following commits; 2 & 3 (ae45fab & 77b9b82). As they are consecutive commits we can use rebase.git rebase --onto <branch
name>~<first commit number to remove> <branch name>~<first
commit to be kept> <branch name>
e.g to remove commits 2 & 3 above
git rebase --onto repair~3 repair~1
repair
Delete Commit History in Git Repository
Follow the below steps to complete this task.
Warning: This will remove your old commit history completely, You can’t recover it again.
- Create Orphan Branch – Create a new orphan branch in git repository. The newly created branch will not show in ‘git branch’ command.
$ git checkout --orphan temp_branch
- Add Files to Branch – Now add all files to newly created branch and commit them using following commands.
$ git add -A $ git commit -am "the first commit"
- Delete master Branch – Now you can delete the master branch from your git repository.
$ git branch -D master
- Rename Current Branch – After deleting the master branch, let’s rename newly created branch name to master.
$ git branch -m master
- Push Changes – You have completed the
changes to your local git repository. Finally, push your changes to
remote (Github) repository forcefully.
$ git push -f origin master
Short Form of commands:
cd Repo
rm -rf .git
git init
git remote add origin git@github.com:JohnDoe/foobar.git
git remote -v
git add --all
git commit -am "Initial commit"
git push -f origin master
First Method
Deleting the.git
folder may cause problems in our git
repository. If we want to delete all of our commits history, but keep
the code in its current state, try this:# Check out to a temporary branch:
git checkout --orphan TEMP_BRANCH
# Add all the files:
git add -A
# Commit the changes:
git commit -am "Initial commit"
# Delete the old branch:
git branch -D master
# Rename the temporary branch to master:
git branch -m master
# Finally, force update to our repository:
git push -f origin master
This will not keep our old commits history around. But if this doesn't work, try the next method below.
Second Method
# Clone the project, e.g. `myproject` is my project repository:
git clone https://github/heiswayi/myproject.git
# Since all of the commits history are in the `.git` folder, we have to remove it:
cd myproject
# And delete the `.git` folder:
git rm -rf .git
# Now, re-initialize the repository:
git init
git remote add origin https://github.com/heiswayi/myproject.git
git remote -v
# Add all the files and commit the changes:
git add --all
git commit -am "Initial commit"
# Force push update to the master branch of our project repository:
git push -f origin master
NOTE: You might need to provide the credentials for your GitHub account.
No comments:
Post a Comment