0

I want to save all the local changes. But there is a remote version on my GitHub. GitHub force me to pull before I can push. But when I pull the remote version, my local version become corrupted. I want to push my local version and overwrite the remote version. I dont need the remote version. How could I do that? I don't want to mix those version I only want to preserve my local.

My origin/master is on the remote version and my master is on my local version.

I working on VisualStudio.

Thank you.

David Zomada
  • 167
  • 1
  • 5
  • 22
  • 1
    Try to `git stash` your changes. Then `git pull` to get the latest version of master. Then `pop latest stash` to overwrite the changes with your local changes. Finally, `git commit` and `git push` – Shubham Periwal May 10 '21 at 08:58

3 Answers3

1

I want to push my local version and overwrite the remote version. I dont need the remote version. How could I do that ?

That's exactly what

git push -f

… ("force") is for. However, before you do, I strongly encourage you to set a tag on top of both your current local and remote branches, so you'll be able to retrieve them if something goes wrong.

Rewriting our own branches to make them clean is something somewhat frequent, but try to avoid as much as possible having to use push -f until you really know how it works, as it can be a cause for work loss, both yours and the one of your collaborators if any.

Obsidian
  • 3,719
  • 8
  • 17
  • 30
  • For the good of everyone and especially the beginners that have never eared of it (like @david-zarzoso-moreno) and could do real mistakes with this command, it's surely better to advise the use of `--force-with-lease` instead, no!?! Now that `--force-with-lease` has been introduced, there is now, no good reason to use `--force` anymore... – Philippe May 10 '21 at 09:45
1

Adding my answer so that not only ones proposing to use the --force exists....

You changed the git history so git prevent you by default to loose the history (i.e some commits) already pushed.

You have to force it by tell him to loose some commits already pushed.

You could do it using either the --force or --force-with-lease flags.

But --force-with-lease is highly recommended because it could prevent you to loose the history pushed by someone else that you was not aware of and so, must be preferred.

See here for a good explanation of why use of --force-with-lease is better and brings you some security. https://stackoverflow.com/a/52823955/717372

Philippe
  • 28,207
  • 6
  • 54
  • 78
0

We can do a force push so that it will overwrite the remote changes.

git push origin <your_branch_name> --force
SwissCodeMen
  • 4,222
  • 8
  • 24
  • 34
Vinayagam R
  • 101
  • 4