Push local repo to a new remote with all branches tags and all the history
Since both me and my boss / admin are kind of newbies to Git and since our both vision on how application structure should look is a way different, we quickly did a quite pretty mess in our main repository and got lost a little bit! :] After some short talk, we agreed on one thing (at least). We need to make a new, clean Git repository and push everything from my local repository to it.
This brought us to a question, how to do this? After a short googling, I found these Stack Overflow questions:
- Cannot push into git repository,
- Push local Git repo to new remote including all branches and tags
- Git push error ‘[remote rejected] master -> master (branch is currently checked out)’,
but none of them gave me a quick-and-dirty answer (or: checklist) on how to do this, without too much read.
So, I decided to write my own. Once again, Git turned out to be much better and much easier than SVN.
We assume, that you have a console access to your local repository, which content (including branches, tags and logs) you want to push to a remote, and that you have console access to your server (remote), where you’ll create a brand new empty Git repository (replace clean-repo
with your actual repository name).
First, we need to perform some actions on server-side:
We need to create a new repository:
mkdir clean-repo
cd clean-repo
git init
And then to convert it to a bare (without a local working copy — without files, just the hidden .git
folder):
git config --bool core.bare true
On client side you must navigate to your local repository (which you want to push) and push everything to your new remote repository, using --all
switch:
git push /url/or/path/to/remote/clean-repo --all
That is all, if you want to remain on bare, file-less repository on server side.
If you, however, would like to turn it back to non-bare, with local working copy, then you have to additionally execute on server side these two commands:
git config --bool core.bare false
git reset --hard
This will receate all files and turn your repository to non-bare one.
You can verify, that everything is as expected, by going back to your client (local computer) and typing:
cd ..
mkdir test
cd test
git clone /url/or/path/to/remote/clean-repo
And you should get an exact copy of your local repository, you’ve just pushed to your remote.