Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git - push to new remote with a single consolidated commit

I have been working on a private repo for a while, and am going to release to GitHub.

The problem is i've got many many commits, and for hygiene's sake I'd like only a single commit to appear in GitHub.

How do I push without pushing history? i.e consolidate the entire MASTER into a single commit

many thanks in advance.

Doug

like image 560
Doug Avatar asked Jun 03 '12 08:06

Doug


People also ask

How do I push a new git repository remote?

To push the commit from the local repo to your remote repositories, run git push -u remote-name branch-name where remote-name is the nickname the local repo uses for the remote repositories and branch-name is the name of the branch to push to the repository. You only have to use the -u option the first time you push.


1 Answers

Usually one would use git rebase --interactive HEAD~n where n is the no. of commits you want to merge into one. However, in your case you want to squash the entire history into a single commit and since rebasing without a parent (upto the initial commit) isn't possible we have to use git reset and git commit --amend.

Here's what you do:

  1. git log --oneline. Find the SHA of your initial commit.
  2. Suppose our SHA is fe7d5d1. Run git reset fe7d5d1. This will reset the branch to the inital commit.
  3. Now use git add . to stage all your changes.
  4. Next amend your inital commit, git commit --amend -m "Initial commit".
  5. Confirm that you only have a single commit now with git log --oneline.

CAUTION: Never rewrite/squash/rebase commits you have already pushed to a remote repo.

like image 108
Sahil Muthoo Avatar answered Sep 28 '22 08:09

Sahil Muthoo