0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

git 学習中

0
Last updated at Posted at 2019-11-30

#まとめて git pull したいときは

$ find . -name .git -printf '%h\n' | xargs -I{} sh -c 'cd {}; git pull;'

これをaliasにでも登録しておけば、OK

もう少しスマートな方法が分かれば、更新します。

#checkoutをすべて破棄したい

$ git checkout master
$ git status

壊しちゃった

特定のSHAに戻して、何事もなかったことにしたい

git push -f origin <SHA#>:<Branch Name>

自分のローカルブランチなら、簡単です。メインブランチなど重要なものは管理者でロックされている可能性はあります。そうなると、本コマンドは実行できません。

そうなる前に

なにをcommitするか、確認しましょう

git ls-files -m

変更対象のファイルが表示されます。注意事項は、自分のディレクトリ以下の情報しか表示しません。全体を表示したければ、gitのルートディレクトリまで移動してから実行しましょう。
"-m"は、modify。変更したファイルを表示してくれます。

過去commitをまっさらにする

過去commitをまっさらにするには、次の手順で進めます。平たく言うと、次の手順です。

  • 新しくブランチを設定
  • そこに、master/mainブランチを移動
  • (option) github/gitlab側から、デフォルトの設定を変更
$ git checkout --orphan main
$ git add -A
$ git commit -m "The first commit"
$ git push origin main
$ git branch -D master
(GUIからデフォルトの設定とブランチの削除)

普通の人は使わないと思います。

直前のcommitをまっさらにする

pushしようと思って、準備をしたが、

$ git reset --soft HEAD^    (commit状態からadd状態へ戻す。git statusでadd/new表示される)
$ git reset HEAD .          (add状態を解除する)

Remote Branchを削除する

もう使わないリモートブランチを根こそぎ削除

# ローカルブランチを削除
git branch -D dev/02

# リモートブランチを削除
git push origin --delete dev/02

新しいBranchを作ったらまずpush

git checkout -b dev/02
git push --set-upstream origin dev/02

ブランチ名を変更する

新しく命名し直したい

git branch -m old/00 new/00

それをremote側にも適用したい

git push --force origin new/00

forceはおまじない。普通はいらない

アカウントを間違えたぁ

操作の前に、ローカルの.git/configにしっかりと、user.name, user.emailが書いてあることを確認しましょう。
global側で、違うものが設定されていて、こちらにないと、globalが適用され、あれれ、となる

git commit --amend --author="myemail <myemail@examples.com>" --no-edit

で、remoteに適用するにはおまじない

git push --force origin <current branch>

upstreamを明示的に指定する

myrepoに本物を入れて

git remote set-url origin git@github.com:samuraieng/<myrepo>.git

変化点を見る

名前だけ、全部、個別ファイルの変化点、は、次のコマンドで確認可能。

git diff --name-only main..<target-branch>
git diff main..<target-branch>
git diff main..<target-branch> -- <target-file>

mainへの統合

git checkout main
git merge dev/00
git push

PRするまで

よくわからないので、3 branchを用意した

  • main (fork元と同期)
  • dev/00 (PR用)
  • dev/01 (local update用)

ローカル更新

予定作業

  • mainを同期した上で、dev/01へ展開
  • dev/01で作業
git checkout main
git fetch upstream

PR準備・ローカル

  • mainを同期
  • dev/00を上書き
  • dev/01の内容をマージ
git checkout main
git fetch upstream
git reset --hard upstream/main
git checkout dev/00
git reset --hard upstream/main
git checkout -p dev/01 abc.py
git diff --cached abc.py
git push --force-with-lease

mainからfetchすると、mainの履歴が含まれるので、pushするとき、いろいろ言われる
たとえば、

and have 22 and 1 different commits each, respectively.
  (use "git pull" to merge the remote branch into yours)

こうなると、単純にpushできないので、"--force-with-lease"が必要。これをつけると、自分のcommitだけをpushして、それ以外をmainにあわせることができる。

ただ、このやりかただと、自分の履歴が残らないので注意が必要です。履歴を残すブランチとまっさらにするブランチ、mainを追従するブランチと色々必要そう。

おしまい

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?