Force "git push" to overwrite remote files

This article was translated from: Force “git push” to overwrite remote files

I want to push my local files, and have them on a remote repo, without having to deal with merge conflicts. I want to push my local files and put them in a remote repository without having to deal with merge conflicts . I just want my local version to have priority over the remote one. I just want my local version to have priority over the remote version.

How can I do this with Git? How can I do this with Git ?


#1st Floor

Reference: https://stackoom.com/question/i6Fa/force-git-push-overwrite remote files


#2nd Floor

You should be able to force your local revision to the remote repo by using you should be able to force your local revision to the remote repo by using

git push -f <remote> <branch>

(eg git push -f origin master). (For example git push -f origin master). OFF Leaving <remote>and <branch>by Will Force All local branches that have have the Push the SET --set-upstream. To leave <remote>and <branch>will be forced to push all set up --set-upstreamlocal branches.

Just be warned, if other people are sharing this repository their revision history will conflict with the new one. Please note that if other people share this repository, their revision history will conflict with the new repository. And if they have any local commits after the point of change they will become invalid. And if they have any local commits after the point of change they will become invalid .

Update : Thought I would add a side-note. Update : I want to add a side note. If you are creating changes that others will review, then it's not uncommon to create a branch with those changes and rebase periodically to keep them up-to-date with the main development branch. If you want to create changes that others can view, then It is not uncommon to create branches with these changes and periodically reset the foundation to keep them up to date with the main development branch. Just let other developers know this will happen periodically so they'll know what to expect. Just let other developers know that this will happen regularly so that they know what will happen.

2 Update : Because of at The Increasing Number The Viewers of the I'd like some to the Add Additional Information ON the What to do the when your upstreamdoes the Push Force A preference Experience. Update 2 due to the increase in the number of viewers, I want to add something about: upstreamwhen forced to push encounter Additional information on what to do.

Say I've cloned your repo and have added a few commits like so: Suppose I have cloned your repository and added some commits as follows:

D----E  topic
           /
A----B----C         development

But later the developmentbranch is hit with a rebase, which will cause me to receive an error like so when I run git pull: But later the developmentbranch was rebaseattacked, which would cause me git pullto receive a similar error while running :

Unpacking objects: 100% (3/3), done.
From <repo-location>
 * branch            development     -> FETCH_HEAD
Auto-merging <files>
CONFLICT (content): Merge conflict in <locations>
Automatic merge failed; fix conflicts and then commit the result.

Here I could fix the conflicts and commit, but that would leave me with a really ugly commit history: Here, I can fix the conflicts and , but this will leave me with a very ugly commit history:commit

C----D----E----F    topic
      /              /
A----B--------------C'  development

It might look Enticing to use IT git pull --forceBut Because that'll BE careful with the Leave you stranded commits: Use git pull --forcemay look tempting, but be careful, because this will make you into a state of submission stranded:

D----E   topic

A----B----C'         development

So probably the best option is to do a git pull --rebase. Therefore, the best option may be implementation git pull --rebase. This will require me to resolve any conflicts like before, but for each step instead of committing I'll use git rebase --continue. This will require me to resolve any conflicts as before, but for each step, instead of committing , I will use git rebase --continue. In the end the commit history will look much better: Finally, the commit history will look better:

D'---E'  topic
           /
A----B----C'         development

Update 3: by You CAN Also use at The --force-with-leasethe Option AS A "SAFER" Force the Push, AS Mentioned by Cupcake in His-answer : Update 3: You can also use the --force-with-leaseoption as a "safer" forced push, such as Cupcake mention in his answer Arrived :

Force pushing with a "lease" allows the force push to fail if there are new commits on the remote that you didn't expect (technically, if you haven't fetched them into your remote-tracking branch yet), which is useful if you don't want to accidentally overwrite someone else's commits that you didn't even know about yet, and you just want to overwrite your own: if you use "rent" to force push, if there are new commits on the remote that you don't expect (Technically speaking, if it has not been extracted into the remote tracking branch), the forced push fails. You do n’t want to accidentally overwrite the submissions of others you do n’t even know, but just overwrite your own submissions:

 git push <remote> <branch> --force-with-lease 

The About the Details CAN Learn More by You How to use --force-with-leaseby at The Reading the any of following: You can learn more about how to use by reading any of the following --force-with-leasefor more details:


#3rd floor

Another option (to avoid any forced push which can be problematic for other contributors) is to: another option (to avoid any forced push that may cause problems to other contributors) is:

  • put your new commits in a dedicated branch will submit into your new special branch
  • your the RESET masterON origin/master reset masteronorigin/master
  • merge your dedicated branch to master, always keeping commits from the dedicated branch (meaning creating new revisions on top of masterwhich will mirror your dedicated branch). Merge your dedicated branch to master, always keep the commit of the dedicated branch (this means mastercreate on New revised version to reflect your dedicated branch).
    See " git command for making one branch like another " for strategies to simulate a git merge --strategy=theirs. For simulation strategies, see " Git command for making one branch like another branch ".git merge --strategy=theirs

That way, you can push master to remote without having to force anything. This way, you can push master to remote without having to force anything .


#4th floor

You want to force push you want to forcibly push

What you basically want to do is to force push your local branch, in order to overwrite the remote one. What you basically want to do is force push the local branch to overwrite the remote branch.

If you want a more detailed explanation of each of the following commands, then see my details section below. If you want a more detailed explanation of each of the following commands , please see the "My Details" section below. You basically have 4 different options for force pushing with Git: Basically, you can use 4 different ways to force Git to push:

git push <remote> <branch> -f
git push origin master -f # Example

git push <remote> -f
git push origin -f # Example

git push -f

git push <remote> <branch> --force-with-lease

If you want a more detailed explanation of each command, then see my long answers section below. If you want a more detailed explanation of each command , please refer to the "My Long Answers" section below.

Warning: Force Pushing by Will Overwrite at The Remote Branch with at The State of that you're at The Branch Pushing. Warning: forced by the state to push the branch you are pushing the cover remote branch. Make sure that this is what you really want to do before you use it, otherwise you may overwrite commits that you actually want to keep. Before using it, make sure this is what you really want to do, otherwise you may overwrite you The commit you actually want to keep.

Force pushing details forcibly push details

Specifying the remote and branch specified remote and branch

You can completely specify specific branches and a remote. You can completely specify specific branches and a remote . At The -fFlag of IS at The Short Version --force -flogo is --forcea short version

git push <remote> <branch> --force
git push <remote> <branch> -f

Omitting the branch will be omitted branch

When the branch to push branch is omitted, Git will figure it out based on your config settings. If the branch to push branch is omitted, Git will resolve it according to your configuration settings. In Git versions after 2.0, a new repo will have default settings to push the currently checked-out branch: In Git versions after 2.0, the new repo will have default settings to push the currently checked out branch :

git push <remote> --force

While prior to 2.0, new repos will have default settings to push multiple local branches. In versions prior to 2.0, the new repository will have default settings to push multiple local branches. Settings in are at The Question at The remote.<remote>.pushand push.defaultSettings (See below). There is a question of setting remote.<remote>.pushand push.defaultsettings (see below).

Omitting the remote and the branch will be omitted, and the remote control branches

The The remote and both the when branch are Omitted, Just The behavior of git push --forceIS Determined by your push.defaultGit config Settings: when the remote and branch omitted, and only git push --forcethe behavior of the push.defaultconfiguration settings determined Git:

git push --force
  • As of Git 2.0, the default setting, simplewill basically just push your current branch to its upstream remote counter-part. Starting from Git 2.0, the default setting simplewill basically only push the current branch to its upstream remote peer part. Determined by IS at The Remote at The Branch's branch.<remote>.remoteSetting, and Defaults. At The Origin repo to otherwise remote from the branch of branch.<remote>.remotedetermining the setting, otherwise it defaults to the source branch.<remote>.remote.

  • Before Git version 2.0, the default setting matching,, basically just pushes all of your local branches to branches with the same name on the remote (which defaults to origin). Before Git version 2.0, the default settings were matchingbasically just all your local The branch is pushed to the branch with the same name on the remote (default is the origin).

CAN the Read More by You push.defaultSettings by Reading git help configor AN Online Version of at The git-config (1) Manual Page . You can read git help configor git-config (1) man page for the online version to read more push.defaultsettings.

Force pushing more safely with --force-with-lease the use of --force-with-leasecompulsory promote safer

Force pushing with a "lease" allows the force push to fail if there are new commits on the remote that you didn't expect (technically, if you haven't fetched them into your remote-tracking branch yet), which is useful if you don't want to accidentally overwrite someone else's commits that you didn't even know about yet, and you just want to overwrite your own: if you use "rent" to force push, if there are new commits on the remote that you don't expect (Technically speaking, if it has not been extracted into the remote tracking branch), the forced push fails. You do n’t want to accidentally overwrite the submissions of others you do n’t even know, but just overwrite your own submissions:

git push <remote> <branch> --force-with-lease

The About the Details CAN Learn More by You How to use --force-with-leaseby at The Reading the any of following: You can learn more about how to use by reading any of the following --force-with-leasefor more details:


#5th Floor

git push -f is a bit destructive because it resets any remote changes that had been made by anyone else on the team. git push -f is a bit destructive because it resets any remote changes made by anyone else on the team . A safer option is {git push --force-with-lease}. A safer option is {git push --force-with-lease}.

{---Force-with what Lease} refuse to Update does IS IT IS A Branch The unless that State WE The Expect; {} --force-Lease-made with a rejection branch update, unless it is our desired state . ie nobody has updated the branch upstream . In practice this works by checking that the upstream ref is what we expect, because refs are hashes, and implicitly encode the chain of parents into their value. In practice, this is achieved by checking whether the upstream ref meets our expectations, Because ref is a hash, and the parent chain is implicitly encoded as its value. You can tell {--force-with- lease} exactly what to check for, but. By default will check the current remote ref you can tell exactly {--force-with-lease} To check the contents, but by default Will check the current remote reference. What this means in practice is that when Alice updates her branch and pushes it up to the remote repository, the ref pointing head of the branch will be updated. In practice, this means that when Alice updates her branch and pushes it to the remote repository, the branch's ref pointer will be updated. Now, unless Bob does a pull from the remote, his local reference to the remote will be out of date. Now, unless Bob pulls from the remote, his local reference to the remote will be outdated. When he goes to push using {--force-with-lease}, git will check the local ref against the new remote and refuse to force the push. When he uses {--force-with-lease} for push, git The local reference will be checked against the new remote server and refused to force the push. {--force-with-lease} effectively only allows you to force-push if no-one else has pushed changes up to the remote in the interim. {--force-with-lease} only when no one else is in transition Only when the change is pushed to the remote is it forced to be pushed effectively. It's {--force} with the seatbelt on. It is possible to wear a seat belt {--force}.


#6th floor

Works for me git push --set-upstream origin master -f

Published 0 original articles · praised 75 · 560,000 views +

Guess you like

Origin blog.csdn.net/w36680130/article/details/105484808