Force “git push” to overwrite remote files

后端 未结 6 1126
情书的邮戳
情书的邮戳 2020-11-22 06:12

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

6条回答
  •  迷失自我
    2020-11-22 07:12

    You want to force push

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

    If you want a more detailed explanation of each of the following commands, then see my details section below. You basically have 4 different options for force pushing with Git:

    git push   -f
    git push origin master -f # Example
    
    git push  -f
    git push origin -f # Example
    
    git push -f
    
    git push   --force-with-lease
    

    If you want a more detailed explanation of each command, then see my long answers section below.

    Warning: force pushing will overwrite the remote branch with the state of the branch that you're pushing. 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.

    Force pushing details

    Specifying the remote and branch

    You can completely specify specific branches and a remote. The -f flag is the short version of --force

    git push   --force
    git push   -f
    

    Omitting the branch

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

    git push  --force
    

    while prior to 2.0, new repos will have default settings to push multiple local branches. The settings in question are the remote..push and push.default settings (see below).

    Omitting the remote and the branch

    When both the remote and the branch are omitted, the behavior of just git push --force is determined by your push.default Git config settings:

    git push --force
    
    • As of Git 2.0, the default setting, simple, will basically just push your current branch to its upstream remote counter-part. The remote is determined by the branch's branch..remote setting, and defaults to the origin repo otherwise.

    • 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).

    You can read more push.default settings by reading git help config or an online version of the git-config(1) Manual Page.

    Force pushing more safely with --force-with-lease

    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:

    git push   --force-with-lease
    

    You can learn more details about how to use --force-with-lease by reading any of the following:

    • git push documentation
    • Git: How to ignore fast forward and revert origin [branch] to earlier commit?

提交回复
热议问题