Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The way to "reinit" repository

Tags:

I've made kind of a big refactoring in my project: I renamed files, removed, added... Besides, I added some folders in .gitignore. However, I've already made a commit to a remote repository before refactoring.

Is there any to make git "reinit" for my repository? If there is not, what should I do?

Update:

I have deleted the folder .git and now I have an error of

hint: Updates were rejected because the tip of your current branch is behind hint: its remote counterpart. Merge the remote changes (e.g. 'git pull') hint: before pushing again. hint: See the 'Note about fast-forwards' in 'git push --help' for details.

I don't want to merge the local changes with the remote repo, I just want to push there, meaning completely replace the remote repository with the local one.

like image 945
Alan Coromano Avatar asked Jun 28 '13 04:06

Alan Coromano


People also ask

How do you remote a repository?

Adding a remote repository To add a new remote, use the git remote add command on the terminal, in the directory your repository is stored at. The git remote add command takes two arguments: A remote name, for example, origin.


1 Answers

UPDATE

If you have deleted the .git folder (not a great idea), you can create a new clone of the repo and move your stuff to that, and continue there. Something like this

cd .. git clone <remote-repo-url> new-repo rm -rf new-repo/*                          // this will not remove new-repo/.git cp -f <original-local-repo> new-repo cd new-repo 

Then continue as below.

Note that it is better if you can restore the .git folder. Creating a new repo will loose all local repo information you had in the original local repo, like local branches that were never pushed.

END UPDATE

You could

git reset --soft HEAD^ git add -A . git commit -m "rewriting history" git push --force origin master 

This will back up to the previous commit (while preserving the working tree and index), commit your changes, and then force push that rewritten history to the remote.

push --force is dangerous stuff. It will disturb others who have already pulled, and is considered very rude, but if no one else have started work on it, that is not a problem.

This is what is happening:

--- A -- B  master      ^     |          origin/master  git push  --- A -- B master, origin/master  git reset HEAD^ git commit -am "rewriting"  --- A -- B origin/master      \       \        B' master  git push --force  --- A -- B      \       \        B' master, origin/master 
like image 131
Klas Mellbourn Avatar answered Oct 13 '22 12:10

Klas Mellbourn