LoginSignup
5

More than 3 years have passed since last update.

git push -n でドライランが実行できるんですってよ

Last updated at Posted at 2019-04-11

どうもこんにちは。今日は僕の好きな Git の記事です。
難しいことは書いてないのでお立ち寄りの方は気軽に読んでいってください。
(※いつも難しいことは書けていない\(^O^)/)

概要

git push のドキュメント眺めてたらこんなオプションを見かけました。

Git - git-push Documentation

-n
--dry-run
Do everything except actually send the updates.

dry run ...だと?
言葉の響き的には 実際の push は実行せずにどういう push が行われるのかだけの確認 ができそうな気配ですね:smile:

実験

ドライランオプションが期待したようなものなのか、実際に使って確認してみたいとおもいます。

-n オプション付きで push してみます

# ブランチを作成
$ git checkout -b proj/pushingTest

# 適当なファイルを作成
$ echo 'hoge' > hoge.txt

# commit
$ git commit
[proj/pushingTest 760da1e] create hoge.txt
 1 file changed, 1 insertion(+)
 create mode 100644 hoge.txt

# -n 付きで push
$ git push -nu origin proj/pushingTest
To https://github.com/Jpsern/TestRepo.git
 * [new branch]      proj/pushingTest -> proj/pushingTest
Would set upstream of 'proj/pushingTest' to 'proj/pushingTest' of 'origin'

push すると 新しい branch が作成される、upstream branch がセットされる という2点の変更が行われることがわかります。
github 上で確認しましたが もちろんこの変更は push されていません。 さらに、ローカル環境内に origin/proj/pushingTest も作成されていません。

$ git branch -r
  origin/HEAD -> origin/master
  origin/master

まさしく期待していたドライランの挙動だと言えると思います。
(ちなみに -u オプションは --set-upstream と同じです。)

オプションなしで push してみます

$ git push -u origin proj/pushingTest
Enumerating objects: 4, done.
Counting objects: 100% (4/4), done.
Delta compression using up to 4 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 281 bytes | 70.00 KiB/s, done.
Total 3 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
remote:
remote: Create a pull request for 'proj/pushingTest' on GitHub by visiting:
remote:      https://github.com/Jpsern/TestRepo/pull/new/proj/pushingTest
remote:
To https://github.com/Jpsern/TestRepo.git
 * [new branch]      proj/pushingTest -> proj/pushingTest
Branch 'proj/pushingTest' set up to track remote branch 'proj/pushingTest' from 'origin'.

実際の push はこのようになります。これはいつも見るやつですね。

実験2

前述は新しいブランチの作成を伴うものでしたが、では単純にファイルが更新されたり、新しいファイルを作成しただけの場合はどうでしょうか。

-n 付きで push してみます

# 更新
$ echo 'hogehoge' > hoge.txt

# 新規ファイル
$ echo 'newfile' > newfile.txt

# commit
$ git add hoge.txt
$ git commit
[proj/pushingTest 06aabac] update hoge.txt
 1 file changed, 1 insertion(+), 1 deletion(-)

$ git add newfile.txt
$ git commit
[proj/pushingTest c861aa1] create newfile.txt
 1 file changed, 1 insertion(+)
 create mode 100644 newfile.txt

# -n 付きで push
$ git push -n
To https://github.com/Jpsern/TestRepo.git
   760da1e..c861aa1  proj/pushingTest -> proj/pushingTest

push されるコミットのハッシュと、ブランチが表示されますね。
このへんは見たまんまのようです。

オプションなしで push してみます

$ git push
Enumerating objects: 8, done.
Counting objects: 100% (8/8), done.
Delta compression using up to 4 threads
Compressing objects: 100% (4/4), done.
Writing objects: 100% (6/6), 544 bytes | 136.00 KiB/s, done.
Total 6 (delta 2), reused 0 (delta 0)
remote: Resolving deltas: 100% (2/2), completed with 1 local object.
To https://github.com/Jpsern/TestRepo.git
   760da1e..c861aa1  proj/pushingTest -> proj/pushingTest

こちらも実際 push した場合の出力も載せておきます。

検証済み Git バージョン

  • 1.8.3
  • 2.19.0

バージョン1系の古い環境でも使えました。man git-push コマンドの出力結果から OPTIONS の欄を確認すると -n オプションの使用可否を確認できますので、気になる方はお使いの環境下で試してみてください。

感想

どのようなものが push されるか、大雑把につかむには十分だと思います。慎重派にはこういうオプションはうれしいですね。よかったらみなさまも試してみてください。お気に召すようでしたら、開発チームの運用などに導入してみるのもありかも?しれませんよ。

他にもドライランオプションが使える Git のコマンドありそうなのでそのうちもう少しまとめて記事にしてみたいと思います。(予定)

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
5