0
1

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 コマンド

Last updated at Posted at 2024-12-02

名目

Command

ターミナル
和訳

リポジトリを初期化

git init

% git init
hint: Using 'master' as the name for the initial branch. This default branch name
hint: is subject to change. To configure the initial branch name to use in all
hint: of your new repositories, which will suppress this warning, call:
hint: 
hint: 	git config --global init.defaultBranch <name>
hint: 
hint: Names commonly chosen instead of 'master' are 'main', 'trunk' and
hint: 'development'. The just-created branch can be renamed via this command:
hint: 
hint: 	git branch -m <name>
ヒント: 'master' を初期ブランチの名前として使います。このデフォルトのブランチ名
hint: は変更される可能性があります。新しいリポジトリのすべての
hint: この警告を表示しないようにするには、次のようにします:
を呼び出します: 
hint: git config --global init.defaultBranch <名前> を実行します。
とします: 
hint: 'master' の代わりによく使われる名前は、'main'、'trunk'、そして
hint: 'development' です。作成したばかりのブランチは、このコマンドで名前を変更できます:
ヒント: 作成したばかりのブランチは、次のコマンドで名前を変更できます: 
hint: git branch -m <名前> とします。

リポジトリの状態を確認

git status

% git status
On branch master

No commits yet

nothing to commit (create/copy files and use "git add" to track)
master ブランチ

まだコミットしていない

コミットするものはありません (ファイルを作成/コピーし、「git add」 を使って追跡します)

README.md 作成

touch README.md

% touch README.md
% ls -l
total 0
-rw-r--r--  1 user  staff  0 11 28 02:59 README.md

ステージ領域へファイルを追加

git add [filename]

% git add README.md
% git status 
On branch master

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)
	new file:   README.md
master ブランチ

まだコミットしていない

コミットすべき変更:
  (ステージを解除するには 「git rm --cached <file>...」 を使用します)
	新しいファイル   README.md

リポジトリの履歴を記録 (オプション:1行メッセージ)

git commit -m '[massage]'

% git commit -m 'first commit'
[master (root-commit) 0e3e554] first commit
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 README.md
 
% git status 
On branch master
nothing to commit, working tree clean

[master (root-commit) 0e3e554] 最初のコミット
 1 ファイル変更、0 挿入(+)、0 削除(-)
 作成モード 100644 README.md
 
% git status 
master ブランチ
コミットするものはありません。

コミットLogを確認

git log

% git log
commit 0e3e554a38018dc89a669c87eac33d353ef879e7 (HEAD -> master)
Author: username <user.mail>
Date:   Thu Nov 28 03:12:57 2024 +0900

コミットメッセージの確認(1行のみ)

git log --pretty=short

% git log --pretty=short
commit 0e3e554a38018dc89a669c87eac33d353ef879e7 (HEAD -> master)
Author: username <user.mail>

    first commit  ←

指定したファイル(ディレクトリ)のみのLog

git log [ファイル名]

% git log README.md
commit 0e3e554a38018dc89a669c87eac33d353ef879e7 (HEAD -> master)
Author: username <user.mail>
Date:   Thu Nov 28 03:12:57 2024 +0900

    first commit

変更の差分を確認

git diff

git diff
diff --git a/README.md b/README.md
index e69de29..1c79caf 100644
--- a/README.md
+++ b/README.md
@@ -0,0 +1 @@
+1行目に文字を追記    ← 差分

% git add README.md
% git diff           
            ← add後は差分無し

ワークツリーと最新コミットの差分

git diff HEAD

% git add README.md
% git diff HEAD           
diff --git a/README.md b/README.md
index e69de29..1c79caf 100644
--- a/README.md
+++ b/README.md
@@ -0,0 +1 @@
+1行目に文字を追記    ← add後も差分あり

% git commit -m "second commit"
[master 3b224af] second commit
 1 file changed, 1 insertion(+)
 
% git diff HEAD
                     ← commit後は差分無し

ブランチ一覧を表示

git branch

% git branch 
* master       ← * が現在のBranch

ブランチを作成し、Newブランチへ切替

git checkout -b "[BranchName]"

% git checkout -b "sub-A"
Switched to a new branch 'sub-A'

% git branch
  master
* sub-A

新しいブランチ 'sub-A' に切り替わりました。

git ブランチ
  マスター
* サブA

ブランチを視覚的に確認する 

git log --graph

*   commit bb77533c2b105db886ecf35961def479c363fd7a (HEAD -> master)
|\  Merge: 3b224af d2a76cb
| | Author: username <user.mail>
| | Date:   Thu Nov 28 04:34:19 2024 +0900
| | 
| |     commit message
| | 
| * commit d2a76cb7a51ce61597a993dc814b1fc881e82909 (sub-A)
| | Author: username <user.mail>
| | Date:   Thu Nov 28 04:29:38 2024 +0900
| | 
| |     commit message

歴史を遡る

git reset --hard [hush値]

% git reset --hard 3b224af0eb209b2e5bc8f858f9a1abe42f10e63e
HEAD is now at 3b224af second commit

現在のリポジトリの操作履歴

git reflog

% git reflog
3b224af (HEAD -> sub-B, master) HEAD@{0}: checkout: moving from master to sub-B
3b224af (HEAD -> sub-B, master) HEAD@{1}: reset: moving to 3b224af0eb209b2e5bc8f858f9a1abe42f10e63e
d2a76cb (sub-A) HEAD@{2}: reset: moving to d2a76cb7a51ce61597a993dc814b1fc881e82909
bb77533 HEAD@{3}: commit (merge): sub-A_merge
3b224af (HEAD -> sub-B, master) HEAD@{4}: checkout: moving from sub-A to master
d2a76cb (sub-A) HEAD@{5}: commit: 3行目追記
b6d8bd1 HEAD@{6}: checkout: moving from master to sub-A
3b224af (HEAD -> sub-B, master) HEAD@{7}: checkout: moving from sub-A to master
b6d8bd1 HEAD@{8}: commit: merge
8e93f8b HEAD@{9}: checkout: moving from master to sub-A
3b224af (HEAD -> sub-B, master) HEAD@{10}: checkout: moving from sub-A to master
8e93f8b HEAD@{11}: checkout: moving from master to sub-A
3b224af (HEAD -> sub-B, master) HEAD@{12}: checkout: moving from sub-A to master
8e93f8b HEAD@{13}: commit: sub-A_2行目追記
3b224af (HEAD -> sub-B, master) HEAD@{14}: checkout: moving from master to sub-A
3b224af (HEAD -> sub-B, master) HEAD@{15}: commit: second commit
0e3e554 HEAD@{16}: commit (initial): first commit

コミットメッセージを修正

git commit --amend

% git commit --amend 
[master a30218d] master_sub-B_fix
 Date: Sat Nov 30 11:24:37 2024 +0900

add . を省略してcommitを実行

git commit -am [message]

% git commit -am '5行目追記'
[sub-C 183f504] 5行目追記
 1 file changed, 1 insertion(+)

他ブランチの追記をマージ

git merge --no-ff [BranchName]

% git merge --no-ff sub-C
==エディタ==
Merge branch 'sub-C'
# Please enter a commit message to explain why this merge is necessary,
# especially if it merges an updated upstream into a topic branch.
#
# Lines starting with '#' will be ignored, and an empty message aborts
# the commit.
===========
Merge made by the 'ort' strategy.
 README.md | 1 +
 1 file changed, 1 insertion(+)

リモートリポジトリを登録

git remote add origin git@github.com:[git-hubname]/[repository].git

% git remote add origin git@github.com:[git-hubname]/[repository].git

リモートリポジトリへ送信

git push -u origin master
*-u は現在のローカルリポジトリの上流をリモートのmasterブランチである事を設定する。

% git push -u origin master
Enumerating objects: 25, done.
Counting objects: 100% (25/25), done.
Delta compression using up to 4 threads
Compressing objects: 100% (15/15), done.
Writing objects: 100% (25/25), 1.99 KiB | 680.00 KiB/s, done.
Total 25 (delta 7), reused 0 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (7/7), done.
To github.com:[git-hubname]/[repository].git
 * [new branch]      master -> master
branch 'master' set up to track 'origin/master'.
オブジェクトの列挙: 25, 完了
オブジェクトを数えています: 100% (25/25), 完了。
4スレッドまでのデルタ圧縮
オブジェクトを圧縮: 100%(15/15)、完了。
オブジェクトの書き込み: 100% (25/25), 1.99 KiB | 680.00 KiB/s, 完了。
合計25(デルタ7)、再利用0(デルタ0)、パック再利用0
リモート: デルタの解決: 100%(7/7)、完了。
github.com:[git-hubname]/[repository].gitへ。
 * 新しいブランチ] master -> master
ブランチ 'master' が 'origin/master' を追跡するように設定。

% git push -u origin sub-A
Total 0 (delta 0), reused 0 (delta 0), pack-reused 0
remote: 
remote: Create a pull request for 'sub-A' on GitHub by visiting:
remote:      https://github.com/[git-hubname]/[repository].git/pull/new/sub-A
remote: 
To github.com:[git-hubname]/[repository].git
 * [new branch]      sub-A -> sub-A
branch 'sub-A' set up to track 'origin/sub-A'.








#ステージングエリアにあげる

git add (ファイル名) or git add .

#コミットする

git commit "コミット名"

#コミットメッセージを1行以内に納める

git commit -m "コミット名"

#一つ前のコミットと統合する

git commit --amend -m "コミット名"

#gitのログを確認する

git log

#Gitのlogを簡潔にまとめて表示する

git log --oneline

#オプション
git log --oneline
git log -p(変更した場所を見たい場合)
git log --stat(より詳しく変更した場所を見たい場合)

#現在の状態

git status

#前の状態に戻る

git checkout --(file名)

#何処を編集したのか知りたい場合

git diff

#ステージングエリアにあげた場合、コミットで変更されるファイルが分かる

git diff --cached

#コミットした後の編集されたファイルが分かる
git diff -r (ID名)

#git addを取り消す

git reset HEAD または git reset HEAD (ファイル名)

#直前に戻る

git reset --hard HEAD

#1つ前に戻る
git reset --hard HEAD^

#指定されたlogに戻る

git reset --hard (ID)

#マージを始めた頃のブランチに戻る

git reset --hard ORIG_HEAD

#git 修正したファイルの変更を取り消す事はできるけど、特定のバージョンに戻したい場合

git checkout コミット ファイル

#ブランチの一覧表示

git branch

#ブランチを作る

git branch

#ブランチを削除

git branch -d (ブランチ名)

#リポジトリの複製

git clone git@(アドレス名)

#新規ブランチを作りそれをカレントブランチにする

git checkout -b (ブランチ名)

#リモートリポジトリにPushする前に登録するコマンド

git remote add origin(リモートリポジトリ名) git@~

#リモートリポジトリ削除

git remote rm origin

#ローカルリポジトリをリモートリポジトリに同期する

git fetch origin

#リモートブランチと同期したデータ、追跡ブランチをローカルリポジトリに取り込む

git merge origin / (ブランチ名)

#mergeとfetchをまとめて行う

git pull origin (ブランチ名)

#ローカルブランチのデータをリモートブランチに送る(最新のもの)

git push origin (ブランチ名)

#他のブランチを現在のカレントブランチに取り込む

git merge (branch name)

#rebaseをする(履歴を綺麗にする、まとめてコミットを取り込む)

git rebase (branch name)

#rebaseインタラクティブモード

intの所にはなにか任意の数字を入れる

git rebase -i HEAD~int

#cherry-pickをする(直接コミットを取り込みたいとき)

git cherry-pick (コミットID)

コミットIDはgit logで確認

#git の履歴を削除する

git filter-branch -f --index-filter 'git rm --ignore-unmatch filename' HEAD

-rfを付けるとディレクトリを削除

git filter-branch -f --index-filter 'git rm -rf --ignore-unmatch dirname' HEAD

#名前の登録

config --global user.name ”(your name)"

#メールの登録

config --global user.email "(your email)"

#メッセージの色分け

config --global color.ui true

#設定一覧

config -l

#ヘルプを見る

config --help
help config

0
1
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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?