git pushコマンドの使い方とは?基本的な使い方から途中までpushする方法について紹介!
git pushの使い方を紹介します

今回は、git pushコマンドの使い方について説明します。
リモートリポジトリの更新のためにgit pushコマンドを使用します。ここでは、以下について紹介します。
git pushの使い方を紹介します

今回は、git pushコマンドの使い方について説明します。
リモートリポジトリの更新のためにgit pushコマンドを使用します。ここでは、以下について紹介します。
・基本的な使い方
・ブランチ名指定push
・強制push
・pushの取り消し
・未push一覧
・途中までpush
また、gitのバージョンは以下とします。
|
1
2
|
~$ git --version
git version 2.25.1
|
git pushコマンドの使い方に興味のある方はぜひご覧ください。
基本的な使い方
git pushコマンドの基本的な使い方を紹介します。ここでは、「sample」リポジトリを使用します。まずは、commitします。
|
1
2
3
4
5
6
|
~/sample$ touch readme.txt
~/sample$ git add readme.txt
~/sample$ git commit -m "first commit"
[master (root-commit) d417dc0] first commit
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 readme.txt
|
git pushは以下のように実行します。
|
1
2
3
4
5
6
7
8
9
|
~/sample$ git push
Username for 'https://github.com': ${GitHubユーザ名}
Password for 'https://${GitHubユーザ名}@github.com':
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Writing objects: 100% (3/3), 211 bytes | 105.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://github.com/${GitHubユーザ名}/sample.git
* [new branch] master -> master
|
git logで履歴を確認してみます。
|
1
2
3
4
5
6
|
~/sample$ git log
commit d417dc080654d8e0c4d0e5c2d40554693f93822b (HEAD -> master, origin/master)
Author: ${GitHubユーザ名} <GitHubメールアドレス>
Date: Sat Jan 30 18:12:40 2021 +0900
first commit
|
ブランチ名指定push
リモートブランチ名を指定してpushする方法を紹介します。まずはcommitします。
|
1
2
3
4
5
6
|
~/sample$ touch test.txt
~/sample$ git add test.txt
~/sample$ git commit -m "add test.txt"
[master f42ee26] add test.txt
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 test.txt
|
pushするリモートブランチ名を指定するには、以下のように記述します。
|
1
2
3
4
5
6
7
8
9
10
11
12
|
~/sample$ git push origin master:master
Username for 'https://github.com': ${GitHubユーザ名}
Password for 'https://${GitHubユーザ名}@github.com':
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Delta compression using up to 2 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 242 bytes | 80.00 KiB/s, done.
Total 2 (delta 0), reused 0 (delta 0)
To https://github.com/${GitHubユーザ名}/sample.git
d417dc0..f42ee26 master -> master
~/sample$
|
強制push
コンフリクトが発生した場合に、強制pushする方法を紹介します。リモートリポジトリを共同開発者が更新した場合、コンフリクトが発生することがあります。
|
1
2
3
4
5
6
7
8
9
10
11
12
|
~/sample$ git push
Username for 'https://github.com': ${GitHubユーザ名}
Password for 'https://${GitHubユーザ名}@github.com':
To https://github.com/${GitHubユーザ名}/sample.git
! [rejected] master -> master (fetch first)
error: failed to push some refs to 'https://github.com/${GitHubユーザ名}/sample.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
~/sample$
|
こちらの修正を強制的にpushしたい場合、-fオプションを指定します。
|
1
2
3
4
5
6
7
8
9
10
11
12
|
~/sample$ git push -f
Username for 'https://github.com': ${GitHubユーザ名}
Password for 'https://${GitHubユーザ名}@github.com':
Enumerating objects: 8, done.
Counting objects: 100% (8/8), done.
Delta compression using up to 2 threads
Compressing objects: 100% (5/5), done.
Writing objects: 100% (8/8), 671 bytes | 335.00 KiB/s, done.
Total 8 (delta 0), reused 0 (delta 0)
To https://github.com/${GitHubユーザ名}/sample.git
+ 269e45a...9ce366f master -> master (forced update)
~/sample$
|
pushの取り消し
pushの取り消し方法を紹介します。まずはlogを確認してみます。
|
1
2
3
4
|
~/sample$ git log --oneline
9ce366f (HEAD -> master, origin/master) modify test.txt
f42ee26 add test.txt
d417dc0 first commit
|
直前のpushを取り消したい場合、git resetコマンドに「–hard」オプションを指定します。
|
1
2
|
~/sample$ git reset --hard HEAD^
HEAD is now at f42ee26 add test.txt
|
logを確認すると、HEADが変わっていることが分かります。
|
1
2
3
4
|
~/sample$ git log --oneline
f42ee26 (HEAD -> master) add test.txt
d417dc0 first commit
~/sample$
|
未push一覧
commit済みでpushしていない修正が何か、忘れてしまうことがあると思います。未push一覧を確認する方法を紹介します。実際のコマンドを見てみましょう。
|
1
2
3
4
5
|
~/sample$ git log --oneline origin/master..master
1f16f84 (HEAD -> master) modify3 test.txt
05937f9 modify2 test.txt
943cc7e modify test.txt
~/sample$
|
この場合、3つのcommitがpushされていないことが分かります。
途中までpush
中途半端なcommit状態の場合、pushしたくないことがあります。途中までpushする方法を紹介します。未pushのcommitが以下の3つだとします。
|
1
2
3
4
5
|
~/sample$ git log --oneline origin/master..master
1f16f84 (HEAD -> master) modify3 test.txt
05937f9 modify2 test.txt
943cc7e modify test.txt
~/sample$
|
このうち、先頭のcommitはまだpushしたくない場合、以下のようなコマンドを実行します。
|
1
2
3
4
5
6
7
8
9
10
11
|
~/sample$ git push origin HEAD~:master
Username for 'https://github.com': ${GitHubユーザ名}
Password for 'https://${GitHubユーザ名}@github.com':
Enumerating objects: 8, done.
Counting objects: 100% (8/8), done.
Delta compression using up to 2 threads
Compressing objects: 100% (4/4), done.
Writing objects: 100% (6/6), 530 bytes | 265.00 KiB/s, done.
Total 6 (delta 0), reused 0 (delta 0)
To https://github.com/${GitHubユーザ名}/sample.git
+ 9ce366f...05937f9 HEAD~ -> master (forced update)
|
未push一覧を確認してみます。先頭のcommitだけがpushされていないことが分かります。
|
1
2
3
|
~/sample$ git log --oneline origin/master..master
1f16f84 (HEAD -> master) modify3 test.txt
~/sample$
|
HEAD~をHEAD~2にすれば、先頭2つのcommit以外をpushします。
まとめ
いかがでしたでしょうか。git pushコマンドの使い方について説明しました。
基本的な使い方から、ブランチ名指定pushや強制pushについて紹介しました。また、pushの取り消しや未push一覧、途中までpushする方法についても触れました。
ぜひご自身でコマンドを書いて、理解を深めてください。
・基本的な使い方
・ブランチ名指定push
・強制push
・pushの取り消し
・未push一覧
・途中までpush
また、gitのバージョンは以下とします。
|
1
2
|
~$ git --version
git version 2.25.1
|
git pushコマンドの使い方に興味のある方はぜひご覧ください。
基本的な使い方
git pushコマンドの基本的な使い方を紹介します。ここでは、「sample」リポジトリを使用します。まずは、commitします。
|
1
2
3
4
5
6
|
~/sample$ touch readme.txt
~/sample$ git add readme.txt
~/sample$ git commit -m “first commit”
[master (root-commit) d417dc0] first commit
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 readme.txt
|
git pushは以下のように実行します。
|
1
2
3
4
5
6
7
8
9
|
~/sample$ git push
Username for 'https://github.com': ${GitHubユーザ名}
Password for 'https://${GitHubユーザ名}@github.com':
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Writing objects: 100% (3/3), 211 bytes | 105.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://github.com/${GitHubユーザ名}/sample.git
* [new branch] master -> master
|
git logで履歴を確認してみます。
|
1
2
3
4
5
6
|
~/sample$ git log
commit d417dc080654d8e0c4d0e5c2d40554693f93822b (HEAD -> master, origin/master)
Author: ${GitHubユーザ名} <GitHubメールアドレス>
Date: Sat Jan 30 18:12:40 2021 +0900
first commit
|
ブランチ名指定push
リモートブランチ名を指定してpushする方法を紹介します。まずはcommitします。
|
1
2
3
4
5
6
|
~/sample$ touch test.txt
~/sample$ git add test.txt
~/sample$ git commit -m “add test.txt”
[master f42ee26] add test.txt
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 test.txt
|
pushするリモートブランチ名を指定するには、以下のように記述します。
|
1
2
3
4
5
6
7
8
9
10
11
12
|
~/sample$ git push origin master:master
Username for 'https://github.com': ${GitHubユーザ名}
Password for 'https://${GitHubユーザ名}@github.com':
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Delta compression using up to 2 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 242 bytes | 80.00 KiB/s, done.
Total 2 (delta 0), reused 0 (delta 0)
To https://github.com/${GitHubユーザ名}/sample.git
d417dc0..f42ee26 master -> master
~/sample$
|
強制push
コンフリクトが発生した場合に、強制pushする方法を紹介します。リモートリポジトリを共同開発者が更新した場合、コンフリクトが発生することがあります。
|
1
2
3
4
5
6
7
8
9
10
11
12
|
~/sample$ git push
Username for 'https://github.com': ${GitHubユーザ名}
Password for 'https://${GitHubユーザ名}@github.com':
To https://github.com/${GitHubユーザ名}/sample.git
! [rejected] master -> master (fetch first)
error: failed to push some refs to 'https://github.com/${GitHubユーザ名}/sample.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
~/sample$
|
こちらの修正を強制的にpushしたい場合、-fオプションを指定します。
|
1
2
3
4
5
6
7
8
9
10
11
12
|
~/sample$ git push -f
Username for 'https://github.com': ${GitHubユーザ名}
Password for 'https://${GitHubユーザ名}@github.com':
Enumerating objects: 8, done.
Counting objects: 100% (8/8), done.
Delta compression using up to 2 threads
Compressing objects: 100% (5/5), done.
Writing objects: 100% (8/8), 671 bytes | 335.00 KiB/s, done.
Total 8 (delta 0), reused 0 (delta 0)
To https://github.com/${GitHubユーザ名}/sample.git
+ 269e45a...9ce366f master -> master (forced update)
~/sample$
|
pushの取り消し
pushの取り消し方法を紹介します。まずはlogを確認してみます。
|
1
2
3
4
|
~/sample$ git log --oneline
9ce366f (HEAD -> master, origin/master) modify test.txt
f42ee26 add test.txt
d417dc0 first commit
|
直前のpushを取り消したい場合、git resetコマンドに「–hard」オプションを指定します。
|
1
2
|
~/sample$ git reset --hard HEAD^
HEAD is now at f42ee26 add test.txt
|
logを確認すると、HEADが変わっていることが分かります。
|
1
2
3
4
|
~/sample$ git log --oneline
f42ee26 (HEAD -> master) add test.txt
d417dc0 first commit
~/sample$
|
未push一覧
commit済みでpushしていない修正が何か、忘れてしまうことがあると思います。未push一覧を確認する方法を紹介します。実際のコマンドを見てみましょう。
|
1
2
3
4
5
|
~/sample$ git log --oneline origin/master..master
1f16f84 (HEAD -> master) modify3 test.txt
05937f9 modify2 test.txt
943cc7e modify test.txt
~/sample$
|
この場合、3つのcommitがpushされていないことが分かります。
途中までpush
中途半端なcommit状態の場合、pushしたくないことがあります。途中までpushする方法を紹介します。未pushのcommitが以下の3つだとします。
|
1
2
3
4
5
|
~/sample$ git log --oneline origin/master..master
1f16f84 (HEAD -> master) modify3 test.txt
05937f9 modify2 test.txt
943cc7e modify test.txt
~/sample$
|
このうち、先頭のcommitはまだpushしたくない場合、以下のようなコマンドを実行します。
|
1
2
3
4
5
6
7
8
9
10
11
|
~/sample$ git push origin HEAD~:master
Username for 'https://github.com': ${GitHubユーザ名}
Password for 'https://${GitHubユーザ名}@github.com':
Enumerating objects: 8, done.
Counting objects: 100% (8/8), done.
Delta compression using up to 2 threads
Compressing objects: 100% (4/4), done.
Writing objects: 100% (6/6), 530 bytes | 265.00 KiB/s, done.
Total 6 (delta 0), reused 0 (delta 0)
To https://github.com/${GitHubユーザ名}/sample.git
+ 9ce366f...05937f9 HEAD~ -> master (forced update)
|
未push一覧を確認してみます。先頭のcommitだけがpushされていないことが分かります。
|
1
2
3
|
~/sample$ git log --oneline origin/master..master
1f16f84 (HEAD -> master) modify3 test.txt
~/sample$
|
HEAD~をHEAD~2にすれば、先頭2つのcommit以外をpushします。
まとめ
いかがでしたでしょうか。git pushコマンドの使い方について説明しました。
基本的な使い方から、ブランチ名指定pushや強制pushについて紹介しました。また、pushの取り消しや未push一覧、途中までpushする方法についても触れました。
ぜひご自身でコマンドを書いて、理解を深めてください。
FEnetインフラはサービス開始から10年以上『エンジニアの生涯価値の向上』をミッションに掲げ、
多くのエンジニアの就業を支援してきました。
転職をお考えの方は気軽にご登録・ご相談ください。
新着案件New Job
-
【高額年収】/【CCNA取得者歓迎】/ネットワークの構築/BIG-IP/東京都千代田区/【WEB面談可】/在宅ワーク/20代~30代の方活躍中
年収540万~540万円東京都千代田区(神保町駅) -
東京都中央区/【WEB面談可/インフラサーバ経験者/20~40代の方活躍中】/在宅ワーク
年収600万~600万円東京都中央区(小伝馬町駅) -
【高額年収】/インフラ構築支援/東京都港区/【WEB面談可/インフラサーバ経験者/20~40代の方活躍中】/在宅ワーク
年収960万~960万円東京都港区(新橋駅) -
ガバナンス推進、セキュリティ基盤支援/東京都港区/【WEB面談可】/在宅ワーク/20代~40代の方活躍中
年収780万~780万円東京都港区(新橋駅) -
カー用品販売会社の情報システム運用/東京都千代田区/【WEB面談可/インフラサーバ経験者/20~40代の方活躍中】/テレワーク
年収576万~576万円東京都千代田区(水道橋駅) -
ネットワーク構築、検証/東京都渋谷区/【WEB面談可】/テレワーク/20代~40代の方活躍中
年収540万~540万円東京都渋谷区(渋谷駅)
人気記事Popular Posts
-

PowerShellの文字コードについて解説!文字化けで困らないための対処法
2021-01-19 2021-05-28 -

ベンダーとはどういう意味なのか|ベンダーとつくIT用語10個
2020-10-29 2022-02-22 -

PowerShellでファイルコピーをする方法とは?PowerShellのインストール方法も紹介!
2021-03-10 -

PowerShellでのOut-Fileコマンドの使い方|各オプションもご紹介
2021-03-09 2021-05-28 -

DataGridViewの使い方4つ|行の追加や削除の方法を解説
2020-12-28 2022-01-18 -

PowerShellでのWhere-Objectの使い方とは?Where-Objectを正しく理解しよう
2021-03-10 2021-12-23 -

Linuxでログを調査する方法|syslogや/var/log/についても解説
2020-07-31 2021-12-06 -

PowerShellでの正規表現の使い方について紹介!正規表現を正しく使いこなそう!
2021-01-20 2021-05-28 -

PowerShellのスクリプトとは?スクリプトの実行方法やパラメータの指定方法を紹介!
2021-02-19 -

IPv4とIPv6の違い8つとは?IPv4 over IPv6についても解説
2020-10-29 2020-11-16 -

Windows上にLinuxを導入する方法3選|WSL2に使えるターミナルも紹介
2020-09-25 2021-11-26 -

PowerShellのStart-Processの使用方法とは? Start-Processを使いこなそう!
2021-03-02 2021-05-28




