mirror of
https://github.com/k88hudson/git-flight-rules.git
synced 2025-03-10 12:48:43 -03:00
Translate ## Editing Commits
This commit is contained in:
parent
a9acfde034
commit
5d1814a388
203
README_ja.md
203
README_ja.md
@ -263,65 +263,67 @@ $ (master) git merge upstream/master
|
|||||||
$ (master) git pull upstream master
|
$ (master) git pull upstream master
|
||||||
```
|
```
|
||||||
|
|
||||||
## Editing Commits
|
## コミットの編集
|
||||||
|
|
||||||
<a name="diff-last"></a>
|
<a name="diff-last"></a>
|
||||||
### What did I just commit?
|
### 何をコミットしたかわからなくなった
|
||||||
|
|
||||||
Let's say that you just blindly committed changes with `git commit -a` and you're not sure what the actual content of the commit you just made was. You can show the latest commit on your current HEAD with:
|
何も考えず `git commit -a` で編集をコミットしてしまい、その内容がわからないとします。
|
||||||
|
現在の HEAD の最新のコミット内容は次のように表示できます:
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
(master)$ git show
|
(master)$ git show
|
||||||
```
|
```
|
||||||
|
|
||||||
Or
|
あるいは:
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
$ git log -n1 -p
|
$ git log -n1 -p
|
||||||
```
|
```
|
||||||
|
|
||||||
If you want to see a file at a specific commit, you can also do this (where `<commitid>` is the commit you're interested in):
|
特定のコミットにおけるファイルの中身を見たいときは次のようにします(`<commitid>` は見たいコミット):
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
$ git show <commitid>:filename
|
$ git show <commitid>:filename
|
||||||
```
|
```
|
||||||
|
|
||||||
### I wrote the wrong thing in a commit message
|
### コミットメッセージに間違った内容を書いてしまった
|
||||||
|
|
||||||
If you wrote the wrong thing and the commit has not yet been pushed, you can do the following to change the commit message without changing the changes in the commit:
|
コミットメッセージに間違った内容を書いてしまったとします。
|
||||||
|
コミットがまだプッシュされていない場合は、次のようにして編集内容は変えずにコミットメッセージを編集できます:
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
$ git commit --amend --only
|
$ git commit --amend --only
|
||||||
```
|
```
|
||||||
|
|
||||||
This will open your default text editor, where you can edit the message. On the other hand, you can do this all in one command:
|
デフォルトのテキストエディタが開き、コミットメッセージを編集できます。これらを一つのコマンドでいっぺんにやることもできます:
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
$ git commit --amend --only -m 'xxxxxxx'
|
$ git commit --amend --only -m 'xxxxxxx'
|
||||||
```
|
```
|
||||||
|
|
||||||
If you have already pushed the message, you can amend the commit and force push, but this is not recommended.
|
既にコミットをプッシュしてしまった場合、コミットを修正して強制プッシュすることはできますが、おすすめしません。
|
||||||
|
|
||||||
<a name="commit-wrong-author"></a>
|
<a name="commit-wrong-author"></a>
|
||||||
### I committed with the wrong name and email configured
|
### 間違った名前・メールアドレスの設定でコミットしてしまった
|
||||||
|
|
||||||
If it's a single commit, amend it
|
コミットが一つだけなら、次のように修正します。
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
$ git commit --amend --no-edit --author "New Authorname <authoremail@mydomain.com>"
|
$ git commit --amend --no-edit --author "New Authorname <authoremail@mydomain.com>"
|
||||||
```
|
```
|
||||||
|
|
||||||
An alternative is to correctly configure your author settings in `git config --global author.(name|email)` and then use
|
あるいは、名前とメールアドレスを `git config --global author.(name|email)` で正しく設定してから、次のようにします:
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
$ git commit --amend --reset-author --no-edit
|
$ git commit --amend --reset-author --no-edit
|
||||||
```
|
```
|
||||||
|
|
||||||
If you need to change all of history, see the man page for `git filter-branch`.
|
履歴すべてについて変更したい場合は、`git filter-branch` の man ページを参照してください。
|
||||||
|
|
||||||
### I want to remove a file from the previous commit
|
### 直前のコミットからファイルを削除したい
|
||||||
|
|
||||||
In order to remove changes for a file from the previous commit, do the following:
|
直前のコミットから特定のファイルの編集内容を削除するには次のようにします。
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
$ git checkout HEAD^ myfile
|
$ git checkout HEAD^ myfile
|
||||||
@ -329,48 +331,53 @@ $ git add myfile
|
|||||||
$ git commit --amend --no-edit
|
$ git commit --amend --no-edit
|
||||||
```
|
```
|
||||||
|
|
||||||
In case the file was newly added to the commit and you want to remove it (from Git alone), do:
|
直前のコミットで新たに追加したファイルを(Git のみから)削除したいときは次の通りです。
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
$ git rm --cached myfile
|
$ git rm --cached myfile
|
||||||
$ git commit --amend --no-edit
|
$ git commit --amend --no-edit
|
||||||
```
|
```
|
||||||
|
|
||||||
This is particularly useful when you have an open patch and you have committed an unnecessary file, and need to force push to update the patch on a remote. The `--no-edit` option is used to keep the existing commit message.
|
このコマンドは、パッチに不要なファイルをコミットしてしまい、強制プッシュでリモートのパッチを更新したいときに特に便利です。
|
||||||
|
オプション `--no-edit` は既にあるコミットメッセージを変更しないようにするためのものです。
|
||||||
|
|
||||||
<a name="delete-pushed-commit"></a>
|
<a name="delete-pushed-commit"></a>
|
||||||
### I want to delete or remove my last commit
|
### 直前のコミットを削除したい
|
||||||
|
|
||||||
If you need to delete pushed commits, you can use the following. However, it will irreversibly change your history, and mess up the history of anyone else who had already pulled from the repository. In short, if you're not sure, you should never do this, ever.
|
既にプッシュしたコミットを削除するには次のようにします。
|
||||||
|
ただし、編集履歴が不可逆的に変更され、リポジトリから変更内容をプルしてしまった他の人の編集履歴は滅茶苦茶になります。
|
||||||
|
要するに、よくわからない場合は絶対にしないでください。
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
$ git reset HEAD^ --hard
|
$ git reset HEAD^ --hard
|
||||||
$ git push --force-with-lease [remote] [branch]
|
$ git push --force-with-lease [remote] [branch]
|
||||||
```
|
```
|
||||||
|
|
||||||
If you haven't pushed, to reset Git to the state it was in before you made your last commit (while keeping your staged changes):
|
まだコミットをプッシュしていない場合は、次のようにして(ステージされた編集はそのままで)直前のコミットをする前の状態に Git をリセットできます。
|
||||||
|
|
||||||
```
|
```
|
||||||
(my-branch*)$ git reset --soft HEAD@{1}
|
(my-branch*)$ git reset --soft HEAD@{1}
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
This only works if you haven't pushed. If you have pushed, the only truly safe thing to do is `git revert SHAofBadCommit`. That will create a new commit that undoes all the previous commit's changes. Or, if the branch you pushed to is rebase-safe (ie. other devs aren't expected to pull from it), you can just use `git push --force-with-lease`. For more, see [the above section](#deleteremove-last-pushed-commit).
|
これはプッシュしていない場合にのみ有効な方法です。
|
||||||
|
プッシュしてしまった場合、本当に安全な方法は `git revert SHAofBadCommit` だけです。
|
||||||
|
このコマンドは、直前のコミットを取り消すようなコミットを新たに作成します。
|
||||||
|
プッシュしたブランチがリベースについて安全である場合(つまり、他の開発者がプルすることを想定していない場合)は、`git push --force-with-lease` を使っても大丈夫です。<!--For more, see [the above section](#deleteremove-last-pushed-commit).-->
|
||||||
|
|
||||||
<a name="delete-any-commit"></a>
|
<a name="delete-any-commit"></a>
|
||||||
### Delete/remove arbitrary commit
|
### 任意のコミットを削除したい
|
||||||
|
|
||||||
The same warning applies as above. Never do this if possible.
|
上で述べたのと同様に、やむを得ない場合以外絶対に行わないでください。
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
$ git rebase --onto SHA1_OF_BAD_COMMIT^ SHA1_OF_BAD_COMMIT
|
$ git rebase --onto SHA1_OF_BAD_COMMIT^ SHA1_OF_BAD_COMMIT
|
||||||
$ git push --force-with-lease [remote] [branch]
|
$ git push --force-with-lease [remote] [branch]
|
||||||
```
|
```
|
||||||
|
|
||||||
Or do an [interactive rebase](#interactive-rebase) and remove the line(s) corresponding to commit(s) you want to see removed.
|
あるいは [対話的 rebase](#interactive-rebase) で削除したいコミットに対応する行を選択して削除します。
|
||||||
|
|
||||||
<a name="#force-push"></a>
|
<a name="#force-push"></a>
|
||||||
### I tried to push my amended commit to a remote, but I got an error message
|
### 修正したコミットをリモートにプッシュしようとしたら、エラーメッセージが出た
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
To https://github.com/yourusername/repo.git
|
To https://github.com/yourusername/repo.git
|
||||||
@ -382,61 +389,71 @@ hint: 'git pull ...') before pushing again.
|
|||||||
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
|
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
|
||||||
```
|
```
|
||||||
|
|
||||||
Note that, as with rebasing (see below), amending **replaces the old commit with a new one**, so you must force push (`--force-with-lease`) your changes if you have already pushed the pre-amended commit to your remote. Be careful when you do this – *always* make sure you specify a branch!
|
amend による修正は、rebase と同様に(後述)、**古いコミットを新たなコミットで置き換えます**。それゆえ、修正前のコミットを既にリモートにプッシュしてしまっている場合は、強制プッシュ (`--force-with-lease`) しなければいけません。
|
||||||
|
強制プッシュには細心の注意が必要です。*必ず*ブランチを指定するように!
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
(my-branch)$ git push origin mybranch --force-with-lease
|
(my-branch)$ git push origin mybranch --force-with-lease
|
||||||
```
|
```
|
||||||
|
|
||||||
In general, **avoid force pushing**. It is best to create and push a new commit rather than force-pushing the amended commit as it will cause conflicts in the source history for any other developer who has interacted with the branch in question or any child branches. `--force-with-lease` will still fail, if someone else was also working on the same branch as you, and your push would overwrite those changes.
|
一般論として、**強制プッシュは避けましょう**。修正したコミットを強制プッシュするよりは、新たなコミットを作ってプッシュするのがベストです。
|
||||||
|
強制プッシュは、対象のブランチやその子ブランチで作業した他の開発者のソース履歴に齟齬をきたしてしまいます。
|
||||||
|
誰かが同じブランチで作業していて、強制プッシュがその人の編集を上書きしてしまう場合には、`--force-with-lease` も失敗します。
|
||||||
|
|
||||||
If you are *absolutely* sure that nobody is working on the same branch or you want to update the tip of the branch *unconditionally*, you can use `--force` (`-f`), but this should be avoided in general.
|
他の誰も同じブランチで作業していないことが*絶対に*確実な場合、あるいはブランチの一部を*無条件で*更新したい場合は `--force` (`-f`) で行うことができますが、これは原則的に避けるべきです。
|
||||||
|
|
||||||
<a href="undo-git-reset-hard"></a>
|
<a href="undo-git-reset-hard"></a>
|
||||||
### I accidentally did a hard reset, and I want my changes back
|
### 間違えて hard reset してしまい、元に戻したい
|
||||||
|
|
||||||
If you accidentally do `git reset --hard`, you can normally still get your commit back, as git keeps a log of everything for a few days.
|
間違えて `git reset --hard` をしてしまった場合でも、大抵はコミットを復元できます。Git は数日間のログを全て残してくれているからです。
|
||||||
|
|
||||||
Note: This is only valid if your work is backed up, i.e., either committed or stashed. `git reset --hard` _will remove_ uncommitted modifications, so use it with caution. (A safer option is `git reset --keep`.)
|
注意:これは作業がバックアップされている場合、つまりコミットかスタッシュされている場合のみです。`git reset --hard` はコミットされていない変更を_削除_してしまうので、注意して使ってください。(安全なのは `git reset --keep` を使うことです。)
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
(master)$ git reflog
|
(master)$ git reflog
|
||||||
```
|
```
|
||||||
|
|
||||||
You'll see a list of your past commits, and a commit for the reset. Choose the SHA of the commit you want to return to, and reset again:
|
過去のコミットとリセットのコミットが表示されるので、復元したいコミットの SHA を選んでリセットします:
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
(master)$ git reset --hard SHA1234
|
(master)$ git reset --hard SHA1234
|
||||||
```
|
```
|
||||||
|
|
||||||
And you should be good to go.
|
これで大丈夫です。
|
||||||
|
|
||||||
<a href="undo-a-commit-merge"></a>
|
<a href="undo-a-commit-merge"></a>
|
||||||
### I accidentally committed and pushed a merge
|
### 間違えてマージをコミットしてプッシュしてしまった
|
||||||
|
|
||||||
If you accidentally merged a feature branch to the main development branch before it was ready to be merged, you can still undo the merge. But there's a catch: A merge commit has more than one parent (usually two).
|
フィーチャーブランチをマージの準備が整う前に間違えてメインのブランチにマージしてしまった場合、マージを取り消すことができます。
|
||||||
|
ただし落とし穴があります:マージコミットには複数(通常は二つ)の親があります。
|
||||||
|
|
||||||
|
次のコマンドを実行します。
|
||||||
|
|
||||||
The command to use
|
|
||||||
```sh
|
```sh
|
||||||
(feature-branch)$ git revert -m 1 <commit>
|
(feature-branch)$ git revert -m 1 <commit>
|
||||||
```
|
```
|
||||||
where the -m 1 option says to select parent number 1 (the branch into which the merge was made) as the parent to revert to.
|
|
||||||
|
|
||||||
Note: the parent number is not a commit identifier. Rather, a merge commit has a line `Merge: 8e2ce2d 86ac2e7`. The parent number is the 1-based index of the desired parent on this line, the first identifier is number 1, the second is number 2, and so on.
|
ここでオプション `-m 1` は親 1(マージした先のブランチ)を差し戻す先の親に指定するものです。
|
||||||
|
|
||||||
|
注意:親の番号はコミット ID ではありません。マージコミットの行には `Merge: 8e2ce2d 86ac2e7` のように書かれています。親番号はこのコミットにおいて親を指定するための 1 から始まる番号で、最初の番号は 1 番、次は 2 番、のように振られます。
|
||||||
|
|
||||||
<a href="undo-sensitive-commit-push"></a>
|
<a href="undo-sensitive-commit-push"></a>
|
||||||
### I accidentally committed and pushed files containing sensitive data
|
### 間違えて機密情報を含むファイルをコミットしプッシュしてしまった
|
||||||
|
|
||||||
If you accidentally pushed files containing sensitive, or private data (passwords, keys, etc.), you can amend the previous commit. Keep in mind that once you have pushed a commit, you should consider any data it contains to be compromised. These steps can remove the sensitive data from your public repo or your local copy, but you **cannot** remove the sensitive data from other people's pulled copies. If you committed a password, **change it immediately**. If you committed a key, **re-generate it immediately**. Amending the pushed commit is not enough, since anyone could have pulled the original commit containing your sensitive data in the meantime.
|
機密情報やプライベートな情報(パスワードやキー等)を含むデータを誤ってプッシュしてしまった場合、コミットを修正することができます。
|
||||||
|
ただし、ひとたびデータをコミットしてプッシュしてしまったら、その内容は盗み取られる恐れがあることに留意してください。
|
||||||
|
以下の手順でパブリックリポジトリやローカルからデータを削除することはできますが、他の誰かが既にプルしてしまったデータを削除することは**不可能です**。
|
||||||
|
パスワードをコミットしてしまった場合は**直ちに変更してください**。キーをコミットしてしまった場合は**直ちに再生成しましょう**。
|
||||||
|
誰かが既に機密情報をプルしてしまった可能性がある限り、プッシュしたコミットを修正するだけでは不十分です。
|
||||||
|
|
||||||
If you edit the file and remove the sensitive data, then run
|
ファイルを編集して機密情報を削除したあと、次を実行します。
|
||||||
```sh
|
```sh
|
||||||
(feature-branch)$ git add edited_file
|
(feature-branch)$ git add edited_file
|
||||||
(feature-branch)$ git commit --amend --no-edit
|
(feature-branch)$ git commit --amend --no-edit
|
||||||
(feature-branch)$ git push --force-with-lease origin [branch]
|
(feature-branch)$ git push --force-with-lease origin [branch]
|
||||||
```
|
```
|
||||||
|
|
||||||
If you want to remove an entire file (but keep it locally), then run
|
ファイルごと削除したいがローカルには残しておきたい場合、次を実行します。
|
||||||
```sh
|
```sh
|
||||||
(feature-branch)$ git rm --cached sensitive_file
|
(feature-branch)$ git rm --cached sensitive_file
|
||||||
echo sensitive_file >> .gitignore
|
echo sensitive_file >> .gitignore
|
||||||
@ -444,100 +461,124 @@ echo sensitive_file >> .gitignore
|
|||||||
(feature-branch)$ git commit --amend --no-edit
|
(feature-branch)$ git commit --amend --no-edit
|
||||||
(feature-branch)$ git push --force-with-lease origin [branch]
|
(feature-branch)$ git push --force-with-lease origin [branch]
|
||||||
```
|
```
|
||||||
Alternatively store your sensitive data in local environment variables.
|
あるいは、機密情報をローカルの環境変数に保存しておきましょう。
|
||||||
|
|
||||||
If you want to completely remove an entire file (and not keep it locally), then run
|
ファイルごと削除した上でローカルからも削除したい場合は、次を実行します。
|
||||||
```sh
|
```sh
|
||||||
(feature-branch)$ git rm sensitive_file
|
(feature-branch)$ git rm sensitive_file
|
||||||
(feature-branch)$ git commit --amend --no-edit
|
(feature-branch)$ git commit --amend --no-edit
|
||||||
(feature-branch)$ git push --force-with-lease origin [branch]
|
(feature-branch)$ git push --force-with-lease origin [branch]
|
||||||
```
|
```
|
||||||
|
|
||||||
If you have made other commits in the meantime (i.e. the sensitive data is in a commit before the previous commit), you will have to rebase.
|
他のコミットを既にしてしまっている場合(つまり、機密情報のコミットが直前のコミットよりも以前である場合)は、リベースする必要があります。
|
||||||
|
|
||||||
<a href="#i-want-to-remove-a-large-file-from-ever-existing-in-repo-history"></a>
|
<a href="#i-want-to-remove-a-large-file-from-ever-existing-in-repo-history"></a>
|
||||||
### I want to remove a large file from ever existing in repo history
|
### 大容量のファイルに関する履歴を完全に削除したい
|
||||||
|
|
||||||
If the file you want to delete is secret or sensitive, instead see [how to remove sensitive files](#i-accidentally-committed-and-pushed-files-containing-sensitive-data).
|
削除したいファイルが機密情報である場合は[機密情報を削除する方法](#i-accidentally-committed-and-pushed-files-containing-sensitive-data)を参照してください。
|
||||||
|
|
||||||
|
最近のコミットで大容量のファイルや不要なファイルを削除しても、`.git` フォルダにある Git の履歴には残ので、`git clone` したときに余計なファイルまでダウンロードしてしまうことになります。
|
||||||
|
|
||||||
Even if you delete a large or unwanted file in a recent commit, it still exists in git history, in your repo's `.git` folder, and will make `git clone` download unneeded files.
|
Even if you delete a large or unwanted file in a recent commit, it still exists in git history, in your repo's `.git` folder, and will make `git clone` download unneeded files.
|
||||||
|
|
||||||
|
ここで説明する手順には強制プッシュを必要とし、リポジトリ履歴を大きく変更してしまいます。リモートで共同作業している人がいる場合は、全員のローカルな編集履歴がプッシュされていることをまず確認しておいてください。
|
||||||
|
|
||||||
The actions in this part of the guide will require a force push, and rewrite large sections of repo history, so if you are working with remote collaborators, check first that any local work of theirs is pushed.
|
The actions in this part of the guide will require a force push, and rewrite large sections of repo history, so if you are working with remote collaborators, check first that any local work of theirs is pushed.
|
||||||
|
|
||||||
There are two options for rewriting history, the built-in `git-filter-branch` or [`bfg-repo-cleaner`](https://rtyley.github.io/bfg-repo-cleaner/). `bfg` is significantly cleaner and more performant, but it is a third-party download and requires java. We will describe both alternatives. The final step is to force push your changes, which requires special consideration on top of a regular force push, given that a great deal of repo history will have been permanently changed.
|
履歴を書き換えるのには二つの方法があります。ビルトインの `git-filter-branch` と [`bfg-repo-cleaner`](https://rtyley.github.io/bfg-repo-cleaner/) です。
|
||||||
|
`bfg` はエレガントで性能がよい一方、サードパーティ製のソフトをダウンロードしなければならず、Java も必要です。
|
||||||
|
ここでは両方の方法を説明します。
|
||||||
|
最後のステップでは強制プッシュをしますが、リポジトリの履歴の大部分を永久に変更してしまうため、通常の強制プッシュよりもなお特殊な配慮が必要になります。
|
||||||
|
|
||||||
#### Recommended Technique: Use third-party bfg
|
<!--There are two options for rewriting history, the built-in `git-filter-branch` or [`bfg-repo-cleaner`](https://rtyley.github.io/bfg-repo-cleaner/). `bfg` is significantly cleaner and more performant, but it is a third-party download and requires java. We will describe both alternatives. The final step is to force push your changes, which requires special consideration on top of a regular force push, given that a great deal of repo history will have been permanently changed.-->
|
||||||
|
|
||||||
Using bfg-repo-cleaner requires java. Download the bfg jar from the link [here](https://rtyley.github.io/bfg-repo-cleaner/). Our examples will use `bfg.jar`, but your download may have a version number, e.g. `bfg-1.13.0.jar`.
|
#### おすすめの方法:サードパーティ製の bfg を使う
|
||||||
|
|
||||||
To delete a specific file.
|
bfg-repo-cleaner を使うには Java が必要です。[ここ](https://rtyley.github.io/bfg-repo-cleaner/)から bfg の jar ファイルをダウンロードしてください。
|
||||||
|
以下の例では `bfg.jar` を使いますが、ダウンロードしたものには `bfg-1.13.0.jar` のようにバージョン番号がついているかもしれません。
|
||||||
|
|
||||||
|
<!--Using bfg-repo-cleaner requires java. Download the bfg jar from the link [here](https://rtyley.github.io/bfg-repo-cleaner/). Our examples will use `bfg.jar`, but your download may have a version number, e.g. `bfg-1.13.0.jar`.-->
|
||||||
|
|
||||||
|
特定のファイルを削除する場合は次のようにします。
|
||||||
```sh
|
```sh
|
||||||
(master)$ git rm path/to/filetoremove
|
(master)$ git rm path/to/filetoremove
|
||||||
(master)$ git commit -m "Commit removing filetoremove"
|
(master)$ git commit -m "Commit removing filetoremove"
|
||||||
(master)$ java -jar ~/Downloads/bfg.jar --delete-files filetoremove
|
(master)$ java -jar ~/Downloads/bfg.jar --delete-files filetoremove
|
||||||
```
|
```
|
||||||
Note that in bfg you must use the plain file name even if it is in a subdirectory.
|
bfg を使うときは、ファイルがサブディレクトリにあるときもそのままのファイル名を入力することに注意してください。
|
||||||
|
|
||||||
You can also delete a file by pattern, e.g.:
|
パターンからファイルを削除することもできます。例えば:
|
||||||
```sh
|
```sh
|
||||||
(master)$ git rm *.jpg
|
(master)$ git rm *.jpg
|
||||||
(master)$ git commit -m "Commit removing *.jpg"
|
(master)$ git commit -m "Commit removing *.jpg"
|
||||||
(master)$ java -jar ~/Downloads/bfg.jar --delete-files *.jpg
|
(master)$ java -jar ~/Downloads/bfg.jar --delete-files *.jpg
|
||||||
```
|
```
|
||||||
|
|
||||||
With bfg, the files that exist on your latest commit will not be affected. For example, if you had several large .tga files in your repo, and then in an earlier commit, you deleted a subset of them, this call does not touch files present in the latest commit
|
bfg は最新のコミットにあるファイルには影響しません。例えば、リポジトリに複数あった大容量の .tga ファイルのうち一部を以前のコミットで削除したとして、bfg を実行しても最新のコミットにあるファイルはそのままです。
|
||||||
|
|
||||||
Note, if you renamed a file as part of a commit, e.g. if it started as `LargeFileFirstName.mp4` and a commit changed it to `LargeFileSecondName.mp4`, running `java -jar ~/Downloads/bfg.jar --delete-files LargeFileSecondName.mp4` will not remove it from git history. Either run the `--delete-files` command with both filenames, or with a matching pattern.
|
なお、コミットでファイル名を変更した場合、例えばもともと `LargeFileFirstName.mp4` だったファイルが後のコミットで `LargeFileSecondName.mp4` に変更されている場合は、`java -jar ~/Downloads/bfg.jar --delete-files LargeFileSecondName.mp4` を実行しても Git の履歴からは削除されません。両方のファイル名それぞれについて `--delete-files` を実行するか、パターンマッチで両方削除してください。
|
||||||
|
|
||||||
#### Built-in Technique: Use git-filter-branch
|
#### ビルトインの方法:git-filter-branch を使う
|
||||||
|
|
||||||
`git-filter-branch` is more cumbersome and has less features, but you may use it if you cannot install or run `bfg`.
|
`git-filter-branch` はややこしくて機能も貧弱ですが、`bfg` のインストールや実行ができなくても使えます。
|
||||||
|
|
||||||
In the below, replace `filepattern` may be a specific name or pattern, e.g. `*.jpg`. This will remove files matching the pattern from all history and branches.
|
以下では、`filepattern` を名前やパターン(`*.jpg` など)に置き換えてください。パターンにマッチしたファイルの履歴が全ての履歴とブランチから削除されます。
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
(master)$ git filter-branch --force --index-filter 'git rm --cached --ignore-unmatch filepattern' --prune-empty --tag-name-filter cat -- --all
|
(master)$ git filter-branch --force --index-filter 'git rm --cached --ignore-unmatch filepattern' --prune-empty --tag-name-filter cat -- --all
|
||||||
```
|
```
|
||||||
|
|
||||||
Behind-the-scenes explanation:
|
何をしているのか:
|
||||||
|
|
||||||
`--tag-name-filter cat` is a cumbersome, but simplest, way to apply the original tags to the new commits, using the command cat.
|
`--tag-name-filter cat` は煩雑ですが、これが最も簡単に元のタグを新しいコミットにつける `cat` を使った方法です。
|
||||||
|
|
||||||
`--prune-empty` removes any now-empty commits.
|
`--prune-empty` は現在空のコミットを全て削除します。
|
||||||
|
|
||||||
#### Final Step: Pushing your changed repo history
|
#### 最後のステップ: 変更した履歴をプッシュする
|
||||||
|
|
||||||
|
ファイルを削除したら、リポジトリのものを壊してしまっていないか慎重に確認してください。
|
||||||
|
何か壊してしまった場合は、リポジトリを再度クローンしてやり直すのが最も簡単です。
|
||||||
|
最後のステップとして、必要に応じて Git ガベージコレクションで .git フォルダの容量を最小化してから、強制プッシュします。
|
||||||
|
|
||||||
Once you have removed your desired files, test carefully that you haven't broken anything in your repo - if you have, it is easiest to re-clone your repo to start over.
|
|
||||||
To finish, optionally use git garbage collection to minimize your local .git folder size, and then force push.
|
|
||||||
```sh
|
```sh
|
||||||
(master)$ git reflog expire --expire=now --all && git gc --prune=now --aggressive
|
(master)$ git reflog expire --expire=now --all && git gc --prune=now --aggressive
|
||||||
(master)$ git push origin --force --tags
|
(master)$ git push origin --force --tags
|
||||||
```
|
```
|
||||||
|
|
||||||
Since you just rewrote the entire git repo history, the `git push` operation may be too large, and return the error `“The remote end hung up unexpectedly”`. If this happens, you can try increasing the git post buffer:
|
リポジトリの履歴をすべて書き換えているので、`git push` の量が膨大すぎて `“The remote end hung up unexpectedly”` というエラーが返るかもしれません。この場合は Git の post buffer を増やしてみます。
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
(master)$ git config http.postBuffer 524288000
|
(master)$ git config http.postBuffer 524288000
|
||||||
(master)$ git push --force
|
(master)$ git push --force
|
||||||
```
|
```
|
||||||
|
|
||||||
If this does not work, you will need to manually push the repo history in chunks of commits. In the command below, try increasing `<number>` until the push operation succeeds.
|
うまくいかない場合は、コミットを手作業で小分けにしてプッシュします。
|
||||||
|
プッシュが成功するまで、`<number>` を増やしながら次のコマンドを試してください。
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
(master)$ git push -u origin HEAD~<number>:refs/head/master --force
|
(master)$ git push -u origin HEAD~<number>:refs/head/master --force
|
||||||
```
|
```
|
||||||
Once the push operation succeeds the first time, decrease `<number>` gradually until a conventional `git push` succeeds.
|
プッシュが最初に成功したら、通常の`git push` が 成功するまで `<number>` を徐々に減らしてください。
|
||||||
|
|
||||||
<a href="i-need-to-change-the-content-of-a-commit-which-is-not-my-last"></a>
|
<a href="i-need-to-change-the-content-of-a-commit-which-is-not-my-last"></a>
|
||||||
### I need to change the content of a commit which is not my last
|
### 直近でないコミットの内容を編集したい
|
||||||
|
|
||||||
Consider you created some (e.g. three) commits and later realize you missed doing something that belongs contextually into the first of those commits. This bothers you, because if you'd create a new commit containing those changes, you'd have a clean code base, but your commits weren't atomic (i.e. changes that belonged to each other weren't in the same commit). In such a situation you may want to change the commit where these changes belong to, include them and have the following commits unaltered. In such a case, `git rebase` might save you.
|
複数(たとえば三件)のコミットを行ったあと、文脈的に最初のコミットに属する作業をし忘れたことに気づいたとします。
|
||||||
|
この作業を新たなコミットとして行えばコードベースは綺麗に保てるものの、コミットがアトミックでなくなってしまう(同じ文脈の作業が同じコミットに属さない)ので、この状況は厄介です。
|
||||||
|
し忘れた作業が属するべきコミットを編集し、作業を取り入れつつ、その後のコミットには手をつけないようにしたいとき、`git rebase` が役に立ちます。
|
||||||
|
|
||||||
Consider a situation where you want to change the third last commit you made.
|
<!--Consider you created some (e.g. three) commits and later realize you missed doing something that belongs contextually into the first of those commits.
|
||||||
|
This bothers you, because if you'd create a new commit containing those changes, you'd have a clean code base, but your commits weren't atomic (i.e. changes that belonged to each other weren't in the same commit). In such a situation you may want to change the commit where these changes belong to, include them and have the following commits unaltered. In such a case, `git rebase` might save you.-->
|
||||||
|
|
||||||
|
最後から三件目のコミットを編集したいとします。
|
||||||
|
<!--Consider a situation where you want to change the third last commit you made.-->
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
(your-branch)$ git rebase -i HEAD~4
|
(your-branch)$ git rebase -i HEAD~4
|
||||||
```
|
```
|
||||||
|
|
||||||
gets you into interactive rebase mode, which allows you to edit any of your last three commits. A text editor pops up, showing you something like
|
上のコマンドで対話的リベースモードに入り、直近三件のコミットを編集できるようになります。
|
||||||
|
テキストエディタが開き、次のような内容が表示されます。
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
pick 9e1d264 The third last commit
|
pick 9e1d264 The third last commit
|
||||||
@ -545,7 +586,7 @@ pick 4b6e19a The second to last commit
|
|||||||
pick f4037ec The last commit
|
pick f4037ec The last commit
|
||||||
```
|
```
|
||||||
|
|
||||||
which you change into
|
これを次のように編集します。
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
edit 9e1d264 The third last commit
|
edit 9e1d264 The third last commit
|
||||||
@ -553,19 +594,29 @@ pick 4b6e19a The second to last commit
|
|||||||
pick f4037ec The last commit
|
pick f4037ec The last commit
|
||||||
```
|
```
|
||||||
|
|
||||||
This tells rebase that you want to edit your third last commit and keep the other two unaltered. Then you'll save (and close) the editor. Git will then start to rebase. It stops on the commit you want to alter, giving you the chance to edit that commit. Now you can apply the changes which you missed applying when you initially committed that commit. You do so by editing and staging them. Afterwards you'll run
|
これは最後から三件目のコミットを編集しつつ、他の二件はそのままにするよう `rebase` に指示しています。
|
||||||
|
テキストエディタを保存して終了したら、Git がリベースを始めます。指定したコミットで止まり、そのコミットを編集できるようになります。
|
||||||
|
これで最初にコミットしたときにし忘れた作業を適用できます。編集とステージによって適用しましょう。
|
||||||
|
その後、次を実行します。
|
||||||
|
|
||||||
|
<!--This tells rebase that you want to edit your third last commit and keep the other two unaltered. Then you'll save (and close) the editor. Git will then start to rebase. It stops on the commit you want to alter, giving you the chance to edit that commit. Now you can apply the changes which you missed applying when you initially committed that commit. You do so by editing and staging them. Afterwards you'll run-->
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
(your-branch)$ git commit --amend
|
(your-branch)$ git commit --amend
|
||||||
```
|
```
|
||||||
|
|
||||||
which tells Git to recreate the commit, but to leave the commit message unedited. Having done that, the hard part is solved.
|
これはコミットメッセージはそのままでコミットを作り直すよう Git に指示しています。
|
||||||
|
これで面倒な作業は終わりです。
|
||||||
|
|
||||||
|
<!--which tells Git to recreate the commit, but to leave the commit message unedited. Having done that, the hard part is solved.-->>
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
(your-branch)$ git rebase --continue
|
(your-branch)$ git rebase --continue
|
||||||
```
|
```
|
||||||
|
|
||||||
will do the rest of the work for you.
|
あとは上を実行すれば完了です。
|
||||||
|
|
||||||
|
<!--will do the rest of the work for you.-->
|
||||||
|
|
||||||
## Staging
|
## Staging
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user