8512

ローカルファイルを強制的に上書きするにはどうすればよいgit pullですか?

シナリオは次のとおりです。

  • チームメンバーが、作業中のWebサイトのテンプレートを変更しています
  • 彼らはいくつかの画像を画像ディレクトリに追加しています(ただし、ソース管理下でそれらを追加するのを忘れています)
  • 彼らは後で私にメールで画像を送っています
  • ソース管理下で画像を追加し、他の変更と一緒にGitHubにプッシュしています
  • Gitはファイルを上書きしたくないため、GitHubから更新をプルすることはできません。

これは私が得ているエラーです:

エラー:追跡されていない作業ツリーファイル'public / images/icon.gif'はマージによって上書きされます

Gitにそれらを上書きさせるにはどうすればよいですか?その人は設計者です-通常、私はすべての競合を手作業で解決するので、サーバーには、コンピューターで更新する必要がある最新バージョンがあります。

4

48 に答える 48

11919

⚠重要:ローカルで変更を加えると、それらは失われます。オプションの有無にかかわらず--hard、プッシュされていないローカルコミットは失われます。[*]

Gitによって追跡されていないファイル(アップロードされたユーザーコンテンツなど)がある場合、これらのファイルは影響を受けません。


まず、フェッチを実行して、すべてのorigin/<branch>参照を最新に更新します。

git fetch --all

現在のブランチをバックアップします。

git branch backup-master

次に、2つのオプションがあります。

git reset --hard origin/master

または他のブランチにいる場合:

git reset --hard origin/<branch_name>

説明:

git fetch何もマージまたはリベースしようとせずに、リモートから最新のものをダウンロードします。

次に、git resetマスターブランチをフェッチしたものにリセットします。この--hardオプションは、作業ツリー内のすべてのファイルを変更して、origin/master


現在のローカルコミットを維持する

[*]master :リセットする前からブランチを作成することで、現在のローカルコミットを維持できることに注意してください。

git checkout master
git branch new-branch-to-save-current-commits
git fetch --all
git reset --hard origin/master

この後、古いコミットはすべてに保持されnew-branch-to-save-current-commitsます。

コミットされていない変更

ただし、コミットされていない変更は(ステージングされていても)失われます。必要なものはすべて隠してコミットしてください。そのためには、次を実行できます。

git stash

そして、これらのコミットされていない変更を再適用するには:

git stash pop
于 2012-01-17T00:02:58.813 に答える
1159

これを試して:

git reset --hard HEAD
git pull

それはあなたが望むことをするはずです。

于 2010-05-09T19:45:21.437 に答える
540

警告:git clean追跡されていないファイル/ディレクトリをすべて削除し、元に戻すことはできません。


時々単にclean -f役に立たない。追跡されていないディレクトリがある場合は、-dオプションも必要です。

# WARNING: this can't be undone!

git reset --hard HEAD
git clean -f -d
git pull

警告:git clean追跡されていないファイル/ディレクトリをすべて削除し、元に戻すことはできません。

-n最初に(--dry-run)フラグを使用することを検討してください。これにより、実際には何も削除せずに何が削除されるかが表示されます。

git clean -n -f -d

出力例:

Would remove untracked-file-1.txt
Would remove untracked-file-2.txt
Would remove untracked/folder
...
于 2011-03-19T09:10:18.727 に答える
453

ハリネズミのように、答えはひどいものだと思います。しかし、ハリネズミの答えはもっと良いかもしれませんが、私はそれができるほどエレガントではないと思います。私がこれを行うために見つけた方法は、定義された戦略を使用fetchして使用することです。mergeこれにより、上書きを強制しようとしているファイルの1つでない限り、ローカルの変更が保持されるようになります。

まず、変更をコミットします

 git add *
 git commit -a -m "local file server commit message"

次に、変更をフェッチし、競合がある場合は上書きします

 git fetch origin master
 git merge -s recursive -X theirs origin/master

-Xはオプション名であり、theirsはそのオプションの値です。競合がある場合は、変更を使用することを選択していtheirます(他のオプションは変更です)。ours

于 2012-04-11T20:13:09.083 に答える
350

行う代わりに:

git fetch --all
git reset --hard origin/master

次のことをお勧めします。

git fetch origin master
git reset --hard origin/master

オリジン/マスターブランチにリセットする場合は、すべてのリモートとブランチをフェッチする必要はありませんか?

于 2013-04-26T13:48:12.070 に答える
147

最初に行うのが最善の方法のようです。

git clean

追跡されていないすべてのファイルを削除してから、通常の方法で続行するにはgit pull...

于 2009-07-14T15:16:35.800 に答える
124

警告、これを行うと、gitignoreファイルにdirectory / *エントリがある場合、ファイルが完全に削除されます。

いくつかの答えはひどいようです。David Avsajanishviliの提案に従うことで、@Lauriに何が起こったのかという意味でひどい。

むしろ(git> v1.7.6):

git stash --include-untracked
git pull

後で、隠し場所の履歴をクリーンアップできます。

手動で、1つずつ:

$ git stash list
stash@{0}: WIP on <branch>: ...
stash@{1}: WIP on <branch>: ...

$ git stash drop stash@{0}
$ git stash drop stash@{1}

残酷なことに、一度に:

$ git stash clear

もちろん、隠しておいたものに戻りたい場合は、次のようにします。

$ git stash list
...
$ git stash apply stash@{5}
于 2012-02-11T23:00:26.167 に答える
107

このコマンドは、ローカルの変更を破棄するのに役立つ場合があります。

git checkout <your-branch> -f

次に、クリーンアップを実行します(追跡されていないファイルを作業ツリーから削除します)。

git clean -f

追跡されていないファイルに加えて追跡されていないディレクトリを削除する場合:

git clean -fd
于 2010-08-05T18:06:55.123 に答える
98

とマージする代わりにgit pull、これを試してください:

git fetch --all

に続く:

git reset --hard origin/master

于 2012-11-22T10:56:22.947 に答える
71

私のために働いた唯一のものは:

git reset --hard HEAD~5

これにより、5つのコミットが返され、

git pull

Gitマージを元に戻す方法を調べることでそれがわかりました。

于 2011-05-05T21:53:32.973 に答える
69

これらすべてのソリューションの問題は、それらがすべて複雑すぎるか、さらに大きな問題として、追跡されていないすべてのファイルをWebサーバーから削除することです。これは、サーバー上に常に必要な構成ファイルがあるため、不要です。 Gitリポジトリにはありません。

これが私たちが使用している最もクリーンなソリューションです:

# Fetch the newest code
git fetch

# Delete all files which are being added, so there
# are no conflicts with untracked files
for file in `git diff HEAD..origin/master --name-status | awk '/^A/ {print $2}'`
do
    rm -f -- "$file"
done

# Checkout all files which were locally modified
for file in `git diff --name-status | awk '/^[CDMRTUX]/ {print $2}'`
do
    git checkout -- "$file"
done

# Finally pull all the changes
# (you could merge as well e.g. 'merge origin/master')
git pull
  • 最初のコマンドは、最新のデータをフェッチします。

  • 2番目のコマンドは、リポジトリに追加されているファイルがあるかどうかを確認し、競合の原因となる追跡されていないファイルをローカルリポジトリから削除します。

  • 3番目のコマンドは、ローカルで変更されたすべてのファイルをチェックアウトします。

  • 最後に、プルを実行して最新バージョンに更新しますが、リポジトリ内にある追跡されていないファイルはもう存在せず、ローカルで変更されたすべてのファイルはすでにリポジトリ内と同じであるため、今回は競合は発生しません。

于 2012-11-05T23:32:52.923 に答える
64

まず、標準的な方法を試してください。

git reset HEAD --hard # To remove all not committed changes!
git clean -fd         # To remove all untracked (non-git) files and folders!

警告:上記のコマンドは、コミットしていない場合にのみデータ/ファイルの損失を引き起こす可能性があります!よくわからない場合は、リポジトリフォルダ全体の最初にバックアップを作成してください。

その後、もう一度引っ張ります。

上記が役に立たず、追跡されていないファイル/ディレクトリを気にしない場合(念のため、最初にバックアップを作成してください)、次の簡単な手順を試してください。

cd your_git_repo  # where 'your_git_repo' is your git repository folder
rm -rfv *         # WARNING: only run inside your git repository!
git pull          # pull the sources again

これにより、すべてのgitファイル(.git/すべてのコミットがある場合はdirを除く)が削除され、再度プルされます。


git reset HEAD --hard場合によっては失敗する可能性がありますか?

  1. のカスタムルール.gitattributes file

    .gitattributesにeol=lfルールがあると、一部のテキストファイルでCRLF行末をLFに変換することにより、gitが一部のファイル変更を変更する可能性があります。

    その場合は、これらのCRLF / LFの変更をコミットするか(で確認してgit status)、:を試しgit config core.autcrlf falseて一時的に無視してください。

  2. ファイルシステムの非互換性

    権限属性をサポートしていないファイルシステムを使用している場合。たとえば、2つのリポジトリがあります。1つはLinux / Mac(ext3/ hfs+)にあり、もう1つはFAT32/NTFSベースのファイルシステムにあります。

    お気づきのように、ファイルシステムには2種類あります。したがって、Unixのアクセス許可をサポートしていないシステムでは、基本的に、その種類のアクセス許可をサポートしていないシステムのファイルアクセス許可をリセットできません。したがって、どのよう--hardに試しても、git常にいくつかの「変更」を検出します。

于 2012-10-26T09:17:40.410 に答える
55

私も同じ問題を抱えていました。誰も私にこの解決策を与えませんでしたが、それは私のために働きました。

私はそれを解決しました:

  1. すべてのファイルを削除します。.gitディレクトリだけを残します。
  2. git reset --hard HEAD
  3. git pull
  4. git push

今では動作します。

于 2011-01-12T23:58:39.267 に答える
53

Bonus:

In speaking of pull/fetch/merge in the previous answers, I would like to share an interesting and productive trick,

git pull --rebase

This above command is the most useful command in my Git life which saved a lot of time.

Before pushing your newly commit to server, try this command and it will automatically synchronise the latest server changes (with a fetch + merge) and will place your commit at the top in the Git log. There isn't any need to worry about manual pull/merge.

Find details in What does "git pull --rebase" do?.

于 2015-12-23T15:41:52.630 に答える
36

Here is a generic solution if you do not always want to paste the branch name or you want to automate this within a script

git fetch
git reset --keep origin/$(git rev-parse --abbrev-ref HEAD)

If you want to reset your local changes too:

git fetch
git reset --hard origin/$(git rev-parse --abbrev-ref HEAD)

You also could add a bash alias using this command:

alias gplf='git fetch && echo "HEAD was at $(git rev-parse --short HEAD)" && git reset --hard origin/$(git rev-parse --abbrev-ref HEAD)'
于 2018-05-25T06:31:39.287 に答える
33

私も同様の問題を抱えていました。私はこれをしなければなりませんでした:

git reset --hard HEAD
git clean -f
git pull
于 2011-01-14T15:18:49.410 に答える
33

私は他の答えを要約しました。git pullエラーなしで実行できます。

git fetch --all
git reset --hard origin/master
git reset --hard HEAD
git clean -f -d
git pull

警告:このスクリプトは非常に強力なので、変更を失う可能性があります。

于 2015-08-07T03:03:10.463 に答える
30

私自身の同様の経験に基づいて、上記のStrahinjaKustudicによって提供されたソリューションは断然最高です。他の人が指摘しているように、ハードリセットを実行するだけで、追跡されていないファイルがすべて削除されます。これには、構成ファイルなど、削除したくないものがたくさん含まれている可能性があります。より安全なのは、追加されようとしているファイルのみを削除することです。さらに言えば、更新されようとしているローカルで変更されたファイルもチェックアウトすることをお勧めします。

そのことを念頭に置いて、Kustudicのスクリプトを更新してまさにそれを実行しました。また、タイプミス(元の「欠落」)も修正しました。

#/bin/sh

# Fetch the newest code
git fetch

# Delete all files which are being added,
# so there are no conflicts with untracked files
for file in `git diff HEAD..origin/master --name-status | awk '/^A/ {print $2}'`
do
    echo "Deleting untracked file $file..."
    rm -vf "$file"
done

# Checkout all files which have been locally modified
for file in `git diff HEAD..origin/master --name-status | awk '/^M/ {print $2}'`
do
    echo "Checking out modified file $file..."
    git checkout $file
done

# Finally merge all the changes (you could use merge here as well)
git pull
于 2013-02-27T14:43:00.717 に答える
24

私は同じ問題を抱えていましたが、何らかの理由で、それをしgit clean -f -dませんでした。理由は次のとおりです。何らかの理由で、ファイルがGitによって無視された場合(.gitignoreエントリを介して)、後でプルしてこれを上書きするのは面倒ですが、.を追加しない限り、cleanはファイルを削除しません-x

于 2011-08-03T09:23:43.177 に答える
24

対立には2つの原因が考えられますが、それらは別々に解決する必要があります。私が知る限り、上記の答えのいずれも両方を扱っていません。

  • 追跡されていないローカルファイルは、手動で(より安全に)、または他の回答で提案されているように、次の方法で削除する必要があります。git clean -f -d

  • リモートブランチにないローカルコミットも削除する必要があります。IMOでこれを実現する最も簡単な方法は、次のとおりです。git reset --hard origin/master(「master」を作業中のブランチに置き換え、git fetch origin最初に実行します)

于 2011-12-12T19:54:43.983 に答える
23

ここでのほとんどの回答はmasterブランチに焦点を当てているようです。ただし、2つの異なる場所で同じ機能ブランチに取り組んでいて、フープをあまり飛び越えずに、一方のリベースをもう一方に反映させたい場合があります。

同様の質問に対するRNAの答えtorekの答えの組み合わせに基づいて、私は見事に機能するこれを思いつきました:

git fetch
git reset --hard @{u}

これをブランチから実行すると、ローカルブランチのみがアップストリームバージョンにリセットされます。

これは、gitエイリアス()にもうまく入れることができますgit forcepull

git config alias.forcepull "!git fetch ; git reset --hard @{u}"

または、.gitconfigファイル内:

[alias]
  forcepull = "!git fetch ; git reset --hard @{u}"

楽しみ!

于 2014-02-25T17:19:29.147 に答える
22

より簡単な方法は次のとおりです。

git checkout --theirs /path/to/file.extension
git pull origin master

これにより、ローカルファイルがgit上のファイルで上書きされます

于 2015-05-05T08:03:29.883 に答える
20

git cleanどちらも機能しないという奇妙な状況がありgit resetます。git index追跡されていないすべてのファイルで次のスクリプトを使用して、競合するファイルを削除する必要があります。

git rm [file]

その後、私はうまく引っ張ることができます。

于 2011-09-19T14:18:59.813 に答える
20

私はこれを自分で解決しました:

git checkout -b tmp # "tmp" or pick a better name for your local changes branch
git add -A
git commit -m 'tmp'
git pull
git checkout master # Or whatever branch you were on originally
git pull
git diff tmp

ここで、最後のコマンドは、ローカルの変更が何であったかのリストを提供します。許容できるまで「tmp」ブランチを変更し続けてから、次のコマンドでマスターにマージし直します。

git checkout master && git merge tmp

次回は、「git stash branch」を検索することで、おそらくこれをよりクリーンな方法で処理できますが、stashは最初の数回の試行で問題を引き起こす可能性が高いため、重要ではないプロジェクトで最初の実験を行ってください...

于 2010-12-03T15:00:41.050 に答える
20

私ははるかに簡単で痛みの少ない方法を知っています:

$ git branch -m [branch_to_force_pull] tmp
$ git fetch
$ git checkout [branch_to_force_pull]
$ git branch -D tmp

それでおしまい!

于 2015-09-05T18:23:16.437 に答える
16

Just do

git fetch origin branchname
git checkout -f origin/branchname // This will overwrite ONLY new included files
git checkout branchname
git merge origin/branchname

So you avoid all unwanted side effects, like deleting files or directories you wanted to keep, etc.

于 2015-10-19T09:54:39.250 に答える
15

元の質問にもかかわらず、上位の回答は、同様の問題を抱えているが、ローカルファイルを失いたくない人々に問題を引き起こす可能性があります。たとえば、Al-PunkとcrizCraigのコメントを参照してください。

次のバージョンは、ローカルの変更を一時的なブランチ(tmp)にコミットし、元のブランチ(私が想定しているmaster)をチェックアウトして、更新をマージします。これはで行うことができますがstash、通常はブランチ/マージアプローチを使用する方が簡単であることがわかりました。

git checkout -b tmp
git add *; git commit -am "my temporary files"
git checkout master

git fetch origin master
git merge -s recursive -X theirs origin master

ここで、他のリポジトリorigin masterです。

于 2014-10-22T17:31:53.413 に答える
14

インデックスとヘッドをorigin/masterにリセットしますが、作業ツリーはリセットしないでください。

git reset origin/master
于 2013-02-15T13:41:43.573 に答える
14

これらの4つのコマンドは私のために働きます。

git reset --hard HEAD
git checkout origin/master
git branch -D master
git checkout -b master

これらのコマンドを実行した後にチェック/プルするには

git pull origin master

私はたくさん試しましたが、最終的にこれらのコマンドで成功しました。

于 2014-03-20T04:24:02.043 に答える
14

要件:

  1. ローカルの変更を追跡して、ここで誰もそれらを失うことがないようにします。
  2. ローカルリポジトリをリモートオリジンリポジトリと一致させます。

解決:

  1. ローカルの変更を隠します。
  2. .gitignoreを無視してファイルディレクトリクリーンアップし、 originにハードリセットしてフェッチします。

    git stash --include-untracked
    git fetch --all
    git clean -fdx
    git reset --hard origin/master
    
于 2015-09-01T23:00:21.270 に答える
14

I read through all the answers but I was looking for a single command to do this. Here is what I did. Added a git alias to .gitconfig

[alias]
      fp = "!f(){ git fetch ${1} ${2} && git reset --hard ${1}/${2};};f"

Run your command as

git fp origin master

equivalent to

git fetch origin master
git reset --hard origin/master
于 2016-07-08T13:11:14.203 に答える
9

Don't use git reset --hard. That will wipe their changes which may well be completely undesirable. Instead:

git pull
git reset origin/master
git checkout <file1> <file2> ...

You can of course use git fetch instead of git pull since it clearly isn't going to merge, but if you usually pull it makes sense to continue to pull here.

So what happens here is that git pull updates your origin/master reference; git reset updates your local branch reference on to be the same as origin/master without updating any files, so your checked-out state is unchanged; then git checkout reverts files to your local branch index state as needed. In cases where exactly the same file has been added on live and on upstream master, the index already matches the file following the reset, so in the common case you don't need to do git checkout at all.

If the upstream branch also contains commits which you want to apply automatically, you can follow a subtle variation on the process:

git pull
git merge <commit before problem commit>
git reset <problem commit>
git checkout <file1> <file2> ...
git pull
于 2017-11-28T11:19:56.787 に答える
8

This is the best practice for reverting changes:

  • git commit Commit your staged changes so they will be saved in the reflog ( see below )
  • git fetch Fetch the latest upstream changes
  • git reset --hard origin/master Hard reset to the origin master branch

The reflog records branches and other references being updated in the local repository. Or simply put - the reflog is the history of your changes.

So it's always a great practice to commit. Commits are appended to the reflog which ensures you will always have a way to retrieve the deleted code.

于 2016-08-12T15:56:12.160 に答える
5
于 2015-10-07T09:03:18.587 に答える
5

I was trying to use the Material2 branch on the Angular2-Webpack-Starter and had a heck of a time. This was the only way I could download and use that branch.

git clone --depth 1 https://github.com/angularclass/angular2-webpack-starter.git

cd angular2-webpack-starter/

git checkout -b material2

Open the project folder and delete all non-hidden files and folders. Leave all the hidden ones.

git add .

git commit -m "pokemon go"

git reset --hard

git pull origin material2

(When the editor pops up, hit ':wq', and then press Enter)

Now you are ready.

于 2016-07-27T16:17:54.863 に答える
5

On Windows, do this single command:

git fetch --all & git reset --hard origin/master
于 2018-04-10T07:22:30.020 に答える
4

プロジェクトベースフォルダ内のファイルでは、そのファイルを無視できます。

.gitignore

public/images/*

次に、変更をプルして、その行をgitignoreファイルから削除します。

于 2010-11-09T16:57:30.783 に答える
3

1: Reset to a previous commit

git reset --hard HEAD

2: Delete Untracked Files

git clean -f

3: Pull the commits

git pull

Sources:

于 2018-11-10T02:27:04.933 に答える
3

Another way of solving this is to first stash any uncommitted changes using git stash and then run

git pull --rebase=interactive -s recursive -X theirs

In the interactive rebase you can change all your local undesired commits to drop, which will get rid of them and leave you at the head of the remote branch without introducing a merge commit.

Now you can run git stash apply if you had local stashed changed that you want to bring back.

于 2021-08-10T19:03:40.247 に答える
2

Despite the fact that this question has already many answers, the original question is to solve this question

error: Untracked working tree file 'public/images/icon.gif' would be overwritten by merge

As binary files can't be merged a simple answer is

git checkout public/images/icon.gif

With that the file will recover the previous state it had in this branch.

I usually do git stash if I don't want to lose my changes or something like git checkout . if I don't care about locally modified files. IMO much more simple than reset --hard, clean... and all this stuff more suited to leave the branch as in remote, including commits, untracked files, rather than just solving a locally modified file.

于 2020-07-24T11:23:48.890 に答える
1

Once you do git pull, you will get list of files that are not matching. If the number of files is not very large then you can checkout those files, this action will overwrite those files.

git checkout -- <filename>

I usually do it if for quick check I modify local files on server (no recommended and probabaly reason behind you getting this issue :D ), I checkout the changed files after finding solution.

于 2020-07-12T14:25:26.897 に答える
1

If you are working on your code and find the new changes a huge mistake or unwanted you can simply use an alternative like:

git restore .

where . means all the files present in the directory.

于 2022-02-03T20:42:41.067 に答える
0

I did this to get it to work:

On the computer where I created the new branch:

git push --all

On the computer where I wanted the new branch to show:

git fetch --all
于 2019-08-30T08:53:31.807 に答える
0

As I often need a fast way to reset the current branch on Windows through the command prompt, here's a fast way:

for /f "tokens=1* delims= " %a in ('git branch^|findstr /b "*"') do @git reset --hard origin/%b
于 2020-04-14T11:06:22.447 に答える
0

Step 1. (optional)
From the root of your local repository, save a backup and empty the current folder:

mkdir -p ../<branch>-bkp && mv --backup=t * ../<branch>-bkp

Step 2. Download all files and folders of the branch from the remote repository:

git checkout <branch> && git add -A . && git reset --hard origin/<branch> && git pull

where you should replace <branch> with the name of the branch you want to overwrite.

Comments:

  • You may prefer to manually backup and remove the current files and folders instead of step 1.
    In fact, I recommend doing this with a file-handler in the GUI of your operating system.
  • If you skip step 1, beware: you will lose all work in the local repository!
  • Important: if you leave out git pull in step 2,
    you risk not getting the latest version from the remote repository!
    According to the second reference below, git reset --hard will
    reset the staging area and the working directory to match the most recent commit.
    My experience contradicts this claim!
  • If you run git reset --hard origin/<branch_to_overwrite> without first deleting all files and folders from your local repository, beware that any junk files still laying around may sneak into the remote repository at a later git push even if that was not your intention.
    The extra git add -A . in step 2 prevents this from happening if you choose to leave out step 1.

References:
https://gitforwindows.org/
https://www.atlassian.com/git/tutorials/undoing-changes/git-reset
https://www.atlassian.com/git/tutorials/rewriting-history/git-reflog

于 2020-12-10T17:24:39.350 に答える
-1

I’ve tried most of what I could find here, none worked in my case.

What worked was git merge -X theirs

于 2020-12-01T18:20:31.140 に答える
-4

You could try git pull --force or maybe stash your commits by using git stash and then running git pull.

于 2019-08-22T16:34:32.737 に答える
-9

I think you can also force by git push -f / git push --force

于 2020-10-19T07:46:21.977 に答える