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?

More than 5 years have passed since last update.

Git commands

Posted at

Git 自分用コマンド総まとめ

参考にした本:progit

Need to know:

  • vim commands 
  • Linux commands

0. Terminology

  • Committed means that the date is safely stored in the local database
  • Modified means that file is changed but have not committed it to my database yet.
  • Staged means that I have marked a modified file in its current version to go into my next commit snapshot.

More details: Difference between untracked, unstaged and staged changes

  • Git バージョンチェック

git --version

1. コミットの位置:

  1. origin: a term for the remote repository

  2. master: a local branch
    - ローカルリポジトリのブランチ「master」の位置

  3. origin/master
    - リモートリポジトリ「origin」のブランチ「master」の位置。
    - Remote branch (which is a local copy of the branch named master on the remote repository named origin)

  4. origin/HEAD

    • リモートリポジトリ「origin」をクローンした時にダウンロードされるコミットの位置

2. Git commands

Initializing a Repository

  • 新規ローカルリポジトリの作成

mkdir sample
cd sample

で、適当に好きなところに空っぽのディレクトリを作り、

git init

Gitリポジトリに設定。

  • 既にあるプロジェクトからレポ作成

cd /そのプロジェクトのdirectory
git init #新しく作成したディレクトリをGitリポジトリに設定

# directoryにぽいぽい入れたいファイルらを追加する。working tree のみ存在しているので次にインデックスに追加する。

git add <FILE-NAME> #インデックスに登録したいファイルを登録
git add . #もしくはパラメータに「.」を指定すると、すべてのファイルをインデックスに登録できる

git commit -m 'comment'

Adding to remote repository

作ったローカルレポジトリをgithub上でリモートリポジトリに追加

# 新規のGitHubリポジトリのURLをコピーし、リモートブランチoriginとして設定
git remote add origin <URL>

# ローカルのファイルをリモート上にアップロード
git push -u origin master

Cloning an Existing Repository

既にあるリモートリポジトリを複製する。


git clone <url> <保存先のdirectory>

githubでの新規repo作りかた:
新規にリポジトリ作りたいときのgithubとコマンド操作例

Check status

  • Gitの管理下にあるディレクトリの、ワークツリーとインデックスの状態を確認

git status
  • git statusのディスプレイを簡潔にしたい場合
git status -s
git status --short

Screen Shot 2018-08-29 at 6.11.16 PM.png

?? = インデックスにないファイル
M = Modified file, つまりファイル内容がなんらか変更された
A = 既にステージングされているファイル(インデックスに既にある)

There are two columns to the output:
- the left- hand column indicates the status of the staging area and
- the right-hand column indicates the status of the working tree

上記の場合、 README は内容が変更されたけれども、インデックスにはなく、ステージングされていない。
lib/simplegit.rb は、ファイルが変更され、なおかつ既にインデックス内にある状態。
Rakefile は最初の変更がステージングされた後に変更をまたしたので、最後の変更はステージングされていない。

Check Difference

ファイルの差異を確認。

• どのファイルのどの箇所を編集したのか確認したい場合

git diff # index と working tree の差分を表示
git diff --staged # 既にステージされたファイルの変更のみ表示
git diff --cached # index(staging area)と HEAD(repository) の差分
git diff HEAD # Working tree と HEAD(repository) の差分

さらに詳しいgit diffの使い方:よく使う git diff コマンド
Youtube video: git diff Revised
gitの変更/差分確認方法

新しくファイルを作ってそれをローカルレポに追加するには

echoや

echo 'sample' > SAMPLE.txt  

touchコマンドでまず新規ファイルを作成。


touch SAMPLE.txt
  • ファイルをインデックスに追加する、

git add SAMPLE.txt

*もしファイルが既にgit addでインデックスにある状態でそのファイル内容を変更した場合、もう一回git addで最終更新したバージョンのファイルをgit addしなくてはならない。

  • addしたファイルをコミットする

git commit -m "comment"
  • リポジトリの変更履歴を確認
git log
git log -oneline # 一行で表示
git log -2 #最新の二つの履歴までのみ表示
git log --since=2.weeks # 今日から二週間前の履歴まで表示

自分でコミット履歴表示をカスタマイズ

git log --pretty=format:"<options>..."
例:
git log --pretty=format:"%h = %an, %ar: %s"

すると、
Screen Shot 2018-08-31 at 9.37.08 AM.png
と表示される。

--pretty=format オプション一覧
Screen Shot 2018-08-31 at 9.35.24 AM.png
progit から抜粋。

その他のgit logオプション:
git logを見やすくしたい
git log よく使うオプション一覧

Ignoring Files

  • gitに入れたくないファイルや、表示したくないファイルや意図的に追跡対象から外したい(無視したい)ファイルを設定
  • .gitignoreが無視するファイルは、まだインデックスに登録されていないものだけ
cat .gitignore # git ignore ファイルを作成し、次に行から追跡したくないファイルを入れる。
# 例
*.[oa] # .o や、.a で終わるファイル(object or archive file)

EXAMPLE.txt # EXAMPLE.txt は対象外

*.html # 全てのhtmlファイル

!sample.html # ! を使うと、gitの管理対象となる。すなわちsample.htmlは管理対象だがその他のhtmlは対象外

example/ # ディレクトリ名を指定した場合、対象のディレクトリはgitの管理外に。ディレクトリ名は、最後に / を付けて指定。

/TODO # .gitignoreと同ディレクトリにあるTODOのみを無視したい場合は、/TODOと書く。

doc/*.txt # doc/notes.txt は無視されるが、 doc/server/arch.txt は無視されない。

ワイルドカード

  • An asterisk (*) matches zero or more characters
  • [abc] matches any character inside the brackets (in this case a, b, or c)
  • a question mark (?) matches a single character
  • brackets enclosing characters separated by a hyphen ([0- 9]) matches any character between them (in this case 0 through 9).
  • two asterisks to match nested directories
    • EX: a/**/z だと a/z, a/b/z, a/b/c/z が対象。

さらに詳しいgit ignoreの使い方はこちらを参考:[Git] .gitignoreの仕様詳解

git statusで.gitignoreファイルに入っているファイルたちがgitの管理外に置かれているのを確認できる。(.gitignore は管理対象)

Committing your changes

git commit
git commit -v # どこをmodified したのか見ながらコメントが残せる。

# vim エディタが開くのでコメントを書き終えたら esc + :wq でセーブ

git commit -m "comment" #コメントをcommit コマンドと同時に書ける

Warning: local repository にcommitされるのはstagedされたファイルのみなので、既にadd したファイルを後でまた内容を編集した場合、そのファイルをまたgit addしなければ、最終変更した内容は git commit した時に local repository に反映されないので注意。

TIP: commitした後毎回 git status でcommitできたか確認する。

Skipping the Staging Area

いちいち git addでファイルらをインデックスに入れるの面倒な時。

# git add をすっ飛ばしてまとめて直でgit commit してくれる。
git commit -a
git commit -a -m "comment"

例:
Screen Shot 2018-08-30 at 8.14.45 PM.png

  • 既にlocal repositoryに入れてある(すなわちtracked--追跡対象に--されている)ファイル ex2.py & ex3.rtfファイルの中身を変更して git commit -a で一気にaddをすっ飛ばしてcommitできる。

Warning:オンリー追跡対象のファイルのみに適用!!だから新しく入れたファイルはgit addで追跡対象にしなきゃいけないよ

Removing files

  • trackedファイルをGitの対象から外す

Case 1: tracked file をgitからも自分のコンピューターからも削除したい!

git rm <file name>

git statusで、 deleted: <file name> を確認。 git commitで削除。

例:

git rm log/\*.log # log/ ディレクトリ内にある .log で終わるファイルを削除

Case 2: tracked file をgitの追跡対象から外したいけど、削除じゃなくて手元に残しておきたい。(間違えてstageしてしまったファイルや、.gitignoreに追加したいファイルなど)


git rm --cached <File Name>

Case: やっぱ削除やめやめってしたいとき
基本:

git reset --hard HEAD

でも、ケースによるので深くはここで説明しない。

詳しくはこちらを参考(事例ごとにどうやるのかが書かれています): Git で変更を取り消して、元に戻す方法 (事例別まとめ)

その他:

Moving files

-ファイルの移動

  • ファイル名を変更
git mv file_from file_to 

git mvじゃなくて普通にmvコマンドを売ってしまった場合:
Git で管理しているファイルのリネームを git mv でなく mv してしまったときにどうなるのか調べてみた

  • ファイルの移動
git mv <移動したいファイル名> <移動先ディレクトリ>

# 忘れずに移動したよ!ってcommitする。

Undoing Things

-変更を取り消して元に戻したい時

Case 1: 直前コミットを上書きする

  • コミットした後に、最後にコミットしたコメントを編集したい時や、入れ忘れたファイルを入れたい場合。
git commit --amend
  • 変更したファイルを追加したい時
Example:
git commit -m '最初のコミット' #例えばこのコミットにファイルを追加し忘れたら
git add 入れ忘れたファイル
git commit --amend 
# 一つのコミットとして表示される。

Case 2: Unstaging a Staged File
インデックスに追加してしまったファイルを元に戻す。

  • git add*でインデックスに入れたファイルのうち一つをインデックスから外したい場合
git reset HEAD <file_name>
  • 編集したファイルの内容を最後にコミットした時の内容に戻したい時
git checkout <file_name> #注:working tree(unstaged area)内にある時のみ。

もっと詳しく:
git commit を取り消して元に戻す方法、徹底まとめ
Git で変更を取り消して、元に戻す方法 (事例別まとめ)
git add の取り消し方法と、関連コマンドまとめ

Fetching and Pulling from Your Remotes

  • リモート内のファイルらをfetchでローカルに入れる
git fetch <REMOTE>

# EXAMPLE:
git fetch origin #fetchでは、リモートからのファイルは現在のローカルのファイルとmergeしない

fetchについて詳しく:more about fetch

pull とfetch の違い:difference between fetch and pull

  • Pullでリモートから fetch + mergeする。
git pull #remote から全てのブランチがpull
git pull <REMOTE> <local Branch>

# EXAMPLE
git pull origin master # remote origin から master ブランチのみ pull

More about pull:
git pull

Pushing to your remotes

  • ローカルからリモートに上げる
git push <remote> <branch>

# Exmample
git push origin master # local の master ブランチ を origin remote に push

git pushだけでも、今いるブランチのみをpushする。
git push --allだと全てのブランチをpushする。

  • リモート情報をみる
git remote  show <REMOTE>
  • リモートの追加
git remote add <REMOTE> <URL> # REMOTE名はoriginが既にある場合、違うのを選ぶ
  • リモート名の変更
git remote rename <NEW_REMOTE_NAME> <ORIGINAL_REMOTE_NAME>
git remote #ディレクトリ内のリモートの一覧
  • リモートをプロジェクトから外したい
git remote remove <REMOTE>

Tagging

-タグ付け。コミットIDをヒトに理解しやすくするための別名
詳しくは:GitのTagの使い方

  • 二種類のタグ: lightweight(軽量) と annotated(注釈付き)
    • lightweight: 名前を付けられる
    • annotated: 名前を付けられる、コメントを付けられる、署名を付けられる

使い用途:
- リリースタグにはannotated tag を使ってコメントや署名を追加。
- lightweight tagはローカルで一時的に使用する使い捨てなどに使用

git tag # タグ一覧
git show # タグ情報
  • Annotated tagの追加
git tag -a <tag_name> -m "comment"

git tag -a <tag_name> <commit_ID> #過去のコミットにタグをつけたい場合はcommit ID をタグ名の後に追加する。
  • Lightweight tagの追加
git tag <tag_name>
  • Tag を リモートにシェア

注:git pushのみではタグはプッシュされないので、別でタグを上げる

git push <REMOTE> <TAG_NAME> # リモート名とシェアしたいタグ名を載せる
git push <REMOTE> --tags # 複数のtagをリモートにシェア
  • Checking out Tags
git checkout <tag名> # tagが指しているファイルを確認

#  repository が 'detached HEAD'状態になる

# もしそこからファイルを編集してcommitしたいなら、
# -b option で新しいブランチを作ってcommitする。
git checkout -b <new_branch> <tag> 

Git Aliases

  • いちいちgit commitなどと書くのが面倒な時、ショートカットを設定
# よく使うコマンドたちのショートカットを作成
git config --global alias.co checkout #git checkout == git co
git config --global alias.ci commit #git commit == git ci
git config --global alias.st status #git st == git status

# 自分でコマンドを設定
git config --global alias.last 'log -1 HEAD' #git last で最後のコミットをみる

もっと詳しく:gitで便利なエイリアス達

GIT BRANCHING

  • ブランチ,マージについては次の記事でまとめます。

Git Useful Learning Guide:
Resources to learn Git
GitHowTo
図解Git
Githubによるintro to github

感想:
SourceTree でGUIでの操作に慣れると、ど忘れによるコマンド打ち間違えとか多くて困る。

解決したい疑問:

  • git pull, git fetchらで、remote や branch名なしだと、全てのbranches がlocal repoにアップデートされるのか?
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?