Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git "force push" from new local repo to remote, but keeping remote commit history

I have a working, live website that I am certain has the latest, correct version of every file, that is not managed with Git. I also have a Bitbucket Git repo that's ostensibly for that site, but that has major problems and out-of-date stuff in it.

What I want to do git init the live files, then somehow push them to the remote so that the final remote HEAD state is exactly like the state of my live files, but also so that the remote commit history remains intact.

I want to do this so that the live working tree is never in a different state. I've thought of creating a new branch, pulling remote the master, and then force-merging them or whatever, but as far as I know, I would have to check out the master branch and mess up the live files for those brief moments before and during the merge, assuming that everything went correctly.

I also thought of just FTPing the files down, working out all the conflicts/merging on my local computer, and then pushing from there to Bitbucket and pulling down to the live repo. But that also feels unreliable and risky.

I know that I should never manipulate live code like this, but this is the situation I've been dropped into, and I just want the easiest/safest way to make it work.

(I'm sure this is a duplicate question, but my Google-fu is failing me; please be gentle when directing me to previous answers.)

like image 801
75th Trombone Avatar asked Nov 12 '13 22:11

75th Trombone


People also ask

Does force push remove commit history?

Force pushing rewrites the repository history, which removes sensitive data from the commit history. If you force push, it may overwrite commits that other people have based their work on.

Does git push force overwrite history?

Force PushingGit prevents you from overwriting the central repository's history by refusing push requests when they result in a non-fast-forward merge. So, if the remote history has diverged from your history, you need to pull the remote branch and merge it into your local one, then try pushing again.

How do I force a git push?

To force a push to only one branch, use a + in front of the refspec to push (e.g git push origin +master to force a push to the master branch). See the <refspec>... section above for details. Force an update only if the tip of the remote-tracking ref has been integrated locally.

How do I push committed changes to 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

Try git merge -s.

git merge -s ours origin/remote_branch

This resolves any number of heads, but the resulting tree of the merge is always that of the current branch head, effectively ignoring all changes from all other branches. It is meant to be used to supersede old development history of side branches.

like image 139
mrutyunjay Avatar answered Sep 28 '22 05:09

mrutyunjay