あらかじめ Git 管理にあるファイルやディレクトリを .gitignore に追加するには、キャッシュをクリアが必要です。
$ git rm --cached ファイル名
$ git rm --cached -r ディレクトリ名
vi や エディタソフトなどで .gitignore にファイルを追加し、あとはコミットするだけです。
通常 Git でブランチの変更を行うような管理の場合、リモートとブランチ名を指定します。
$ git push -u origin master
ただ、一人で開発する場合、大規模でない限りブランチを利用することはあまりないと思うので、
$ git push
のようにブランチ指定せずに実行もよいかも知れません。
これは、現在のブランチ名がリモートと同じという条件でなりたちます。
現在の状態をデフォルトとする設定をします。
$ git config --global push.default current
$ git config --global pull.default current
config を確認すると、 push.default が current に設定されます。
$ git config -l
...
push.default=current
pull.default=current
...
これで、ブランチ指定せずに現在の状態で pull、 push できるようになりました。
ただしグループ開発するような時は、省略はしない方がよいでしょう。
git pull や git push できなくなったとき、ブランチの間違いやファイルがコンフリクトしている可能性があります。
$ git pull origin master
warning: Pulling without specifying how to reconcile divergent branches is
discouraged. You can squelch this message by running one of the following
commands sometime before your next pull:
...
git pull は fetch と merge を両方実行しているので、コンフリクトが発生するとエラーになります。
$ git fetch
$ git merge origin/master
$ git push origin master
...
fatal: refusing to merge unrelated histories
...
マージを実行するとコンフリクトしています
$ git merge --allow-unrelated-histories origin/master
CONFLICT (add/add): Merge conflict in README.md
Auto-merging README.md
CONFLICT (add/add): Merge conflict in .gitignore
Auto-merging .gitignore
Automatic merge failed; fix conflicts and then commit the result.
コンフリクトファイルを修正して、 commit & push します。
$ git add.
$ git commit -m 'confrict'
$ git push -u origin master
-u は --set-upstream の短縮形です。
・git clone が重い
・秘密のファイルを Commit してしまった
など、過去にファイルを Commit & Pull してしまった場合、問題ファイルをなかったことにしたい場合があります。
履歴の削除は「git filter-branch」を利用してできますが、破壊的に履歴変更するため事前バックアップするなど作業は自己責任で行ってください。
git_find_big.sh で大きなオブジェクトを抽出
$ du -sh .git/objects 12M .git/objects
$ sh git_find_big.sh
「git_find_big.sh」は、gitコマンドを用いて「.git/objects/pack/pack」内の容量の多いオブジェクト順で抽出します。
抽出したファイルやディレクトリを削除 (refs/heads/master と refs/remotes/origin/master を書き換え)
※ディレクトリの場合は「git rm -r」
$ git filter-branch -f --index-filter 'git rm --cached --ignore-unmatch ファイルパス' --prune-empty -- --all
ガベージコレクションを利用して、不要なオブジェクトやログを削除します。
$ git gc --aggressive --prune=now
git オブジェクトのリパックします。
$ git repack -A -d
$ git push --force origin master
共同作業している場合は、clone してもらった方が安全かもしれません。
$ mkdir プロジェクト名 $ cd プロジェクト名 $ git init --bare --shared=true $ git --bare update-server-info
これで空のリポジトリが作成と、WebDav経由での公開設定$ git clone http://サーバ/git/プロジェクト名
$ git add 追加ファイル $ git add . $ git commit -m 'コメント' $ git remote add origin http://リモートサーバ/git/プロジェクト名 $ git push origin master