I'm looking for a way of merging all commit on master to one single commit and eventually rewrite a new commit to start from a fresh start.
Asked
Active
Viewed 937 times
3 Answers
1
You could delete your .git folder and just use the current source as the "initial commit" in a new Git repo.

Jamie Bisotti
- 2,605
- 19
- 23
-
I need to keep this repo sadly. – Baldráni Nov 04 '16 at 18:51
-
As I understand it, you're talking about completely changing the history of _master_. How is that "keeping this repo"? – Jamie Bisotti Nov 04 '16 at 18:52
-
Well keeping the name of the repo, but maybe should i delete the .git, the repo and redo one. – Baldráni Nov 04 '16 at 18:53
1
If you want to keep your existing repository configuration you could do the following:
git checkout master
git branch backup
: Optionally create a backup branch in case you want to keep your history.git reset --soft $SHA_OF_INIT_COMMIT
: This will update whatHEAD
is pointing to but leave your index and working directory in their current state.git commit --amend
: Change your initial commit to point to the current state of your repo.git push --force
: Force push toorigin/master
to update the remote repository (if there is any).
I have just tried this and it worked like a charm. Credit for this goes to Kevin M Granger. After this you might also want to run git gc
on the local and remote repository (again, if there is any). git gc
runs a number of housekeeping tasks within the current repository, such as compressing file revisions (to reduce disk space and increase performance) and removing unreachable objects.

Community
- 1
- 1

Christoph Zauner
- 341
- 2
- 7
1
Use following Commands and Steps:
1) git rebase -i HEAD~2 origin/master #2 is no of commits you want to fix
Following window will open up
pick 03d633e refactor
pick d51b770 Updating Username comment
# Rebase 2868146..d51b770 onto 2868146 (2 commands)
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
# d, drop = remove commit
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out
"~/WebstormProjects/Working/.git/rebase-merge/git-rebase-todo" 21L, 700C
2) To fixup : change pick to "fixup" or "f" and Save file .
pick 03d633e refactor
fixup d51b770 Updating Username comment
3) Verify same using command:
git log --oneline
4)force push changes :(Be Careful)
git push origin HEAD:master --force

Rahul
- 181
- 1
- 8