LoginSignup
10
26

More than 5 years have passed since last update.

よく使うGitコマンドまとめ

Last updated at Posted at 2018-09-19

よく使うGitコマンドまとめ

前提

  • コマンド例はすべてpowershellによるものです。
  • gitのバージョンは以下の通り

    $ git --version
    git version 2.9.0.windows.1
    

そもそもGitとは

  • 分散型バージョン管理システムです。
  • メリットとして、ローカルリポジトリを持つことにより、リモートリポジトリにアクセスできない環境でも作業ができたり、部分的にリモートに変更を反映するなど、柔軟な開発ができることが挙げられます。

参考

サルでもわかるGit入門

リモートリポジトリのクローン

  1. クローンしたいディレクトリに移動

    $ mkdir repository
    
        ディレクトリ: C:\Users\user
    
    Mode                LastWriteTime         Length Name
    ----                -------------         ------ ----
    d-----       2018/09/19     11:19                repository
    
    $ cd repository
    $ pwd
    
    Path
    ----
    C:\Users\user\repository
    
    
  2. git clone コマンドを実行

    $ git clone <リモートリポジトリのURL>
    Cloning into 'hogehoge'...
    remote: Counting objects: 124, done.
    remote: Compressing objects: 100% (78/78), done.
    used 90 (delta 42)
    Receiving objects: 100% (124/124), 24.36 KiB | 0 bytes/s, done.Resolving deltas: 100% (61/61), done.
    Checking connectivity... done.
    
    $ ls
    
        ディレクトリ: C:\Users\user\repository
    
    Mode                LastWriteTime         Length Name
    ----                -------------         ------ ----
    d-----       2018/09/19     11:24                <クローンしたフォルダ>>
    
    

新規ブランチの作成

  1. 新しいブランチを切る元ブランチを確認

    $ git branch
    * master   # 「*」がついているブランチが、元となるブランチ(現在自分がいるブランチ)
    
  2. ブランチの作成

  • 方法1 git branchコマンドを利用する場合

    $ git branch <新規ブランチ名>
    (例) git branch develop 
    $ git branch
      develop  # developブランチが作成されていることを確認
    * master
    $
    $ git checkout develop 
    Switched to branch 'develop'  # ブランチの移動
    $
    $ git branch
    * develop  # developブランチに移動出来ていることを確認
      master 
    
  • 方法2 git checkout -b コマンドを利用する場合

    $ git checkout -b <新規ブランチ名>
    Switched to a new branch 'develop'
    $ git branch
    * develop  # ブランチの移動と作成が同時にできる。
        master
    

ブランチの削除

  1. 現在作業しているブランチを確認

    $ git branch
    * develop
      master
    

    削除したいブランチで作業している場合は、git checkout <移動先ブランチ名> で別ブランチに移動する。

    $ git checkout master
    Switched to branch 'master'
    Your branch is up-to-date with 'origin/master'.
    $ git branch
      develop
    * master
    
  2. git branch -D <削除するブランチ名>

   $ git branch -D develop
   Deleted branch develop (was d078894).
   $ git branch
   * master

リモートリポジトリにあるブランチを、ローカルに作成する

  1. 作成したいブランチの元となるブランチを最新化する。

    例)masterから作成されたdevelopというリモートブランチを、ローカルに作成する場合

    $ git branch
    * master
    $ git pull
    Already up-to-date.

  2. リモートリポジトリにあるブランチを、ローカルに作成

    $ git branch
    * master
    $ git branch <ローカルに作成するブランチ名> origin/<リモートリポジトリにあるブランチ名>
    例) git branch develop origin/develop
    Branch develop set up to track remote branch develop from origin.
    $ git branch
      develop
    * master
    

ローカルリポジトリにあるブランチを、リモートに作成する

$ git push -u origin <ローカルリポジトリのブランチ名>
Counting objects: 3, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 289 bytes | 0 bytes/s, done.
Total 3 (delta 1), reused 1 (delta 0)
remote:
remote: To create a merge request for develop, visit:
remote:   https://hogehoge?merge_request%5Bsource_branch%5D=develop
remote:
To hogehoge.git
 * [new branch]      develop -> develop
Branch develop set up to track remote branch develop from origin.

ローカルでの作業をaddしてcommitしてpushする流れ

  1. ローカルでの作業を確認

    $ git status
    On branch develop
    Your branch is up-to-date with 'origin/develop'.
    Changes not staged for commit:
        (use "git add <file>..." to update what will be committed)
        (use "git checkout -- <file>..." to discard changes in working directory)
    
            modified:   test
    
    no changes added to commit (use "git add" and/or "git commit -a")
    

    上記の場合、testというファイルが修正されていることが確認できる。
    修正を破棄する場合は、git checkout -- <ファイル名>で破棄可能。

    git diffコマンドで詳しい変更箇所を確認できる。

    $ git diff
    diff --git a/test b/test
    index e69de29..30d74d2 100644
    --- a/test
    +++ b/test
    @@ -0,0 +1 @@
    +test
    \ No newline at end of file
    
  2. 変更ファイルをaddする

    $ git add <addするファイル名>
    $ git status
    On branch develop
    Your branch is up-to-date with 'origin/develop'.
    Changes to be committed:
        (use "git reset HEAD <file>..." to unstage)
    
            modified:   test
    
    
  3. 変更をコミットする

    $ git commit -m "コミットメッセージ"
    [develop e9be86d] commit message
    1 file changed, 1 insertion(+)
    $ git status
    On branch develop
    Your branch is ahead of 'origin/develop' by 1 commit.
    (use "git push" to publish your local commits)
    nothing to commit, working directory clean
    
  4. コミットをpushする

    $ git push origin <プッシュ先のリモートブランチ名>
    Counting objects: 3, done.
    Delta compression using up to 4 threads.
    Compressing objects: 100% (2/2), done.
    Writing objects: 100% (3/3), 283 bytes | 0 bytes/s, done.
    Total 3 (delta 1), reused 0 (delta 0)
    remote:
    remote: To create a merge request for develop, visit:
    remote:   hogehoge?merge_request%5Bsource_branch%5D=develop
    remote:
    To hogehoge
    a5f0f5f..e9be86d  develop -> develop
    

    リモートにまだないブランチにpushする場合は、ローカルリポジトリにあるブランチを、リモートに作成するを参照してリモートブランチ作成後にpushしてください。

ブランチのマージの流れ

例として、developブランチ(マージ元)をmasterにマージ(マージ先)します。

  1. マージ元ブランチで、変更をコミットする

    ローカルでの作業をaddしてcommitしてpushする流れを参照

  2. マージ先ブランチを最新に更新

    $ git checkout <マージ先ブランチ名>
    例)git checkout master
    $ git branch
      develop
    * master
    $ git pull
    Already up-to-date.
    
  3. マージ元ブランチをマージ先ブランチにマージ

    $ git branch
      develop
    * master  # マージ先ブランチにいることを確認
    $ git merge --no-ff <マージ元ブランチ>
    例)git merge --no-ff develop
    Merge made by the 'recursive' strategy.
    test | 1 +
    1 file changed, 1 insertion(+)
    create mode 100644 test
    $ git log --graph --oneline --decorate=full -20 --date=short
    *   55a6f7f (HEAD -> refs/heads/master) Merge branch 'develop'
    |\
    | *   08b239e (refs/heads/develop) Merge branch 'test_branch' into develop
    | |\
    | | * fb290e8 (refs/heads/test_branch) new branch
    | |/
    | * e9be86d (refs/remotes/origin/develop) commit message
    | * a5f0f5f test
    |/
    * d078894 (refs/remotes/origin/master, refs/remotes/origin/HEAD) ss
    * 1fe2cff fix README
    
  4. マージによる変更分をpush

    $ git status
    On branch master
    Your branch is ahead of 'origin/master' by 5 commits.
        (use "git push" to publish your local commits)
    nothing to commit, working directory clean
    $ git push origin <マージ先ブランチ名>
    例)git push origin master
    

コミット・ブランチ状況の確認

  • 方法1 git logコマンドを利用

    $ git log
    commit e9be86dadee4ca8489af60a24d2c74dd3306af12
    Author: onion <onion@onion.com>
    Date:   Wed Sep 19 12:11:55 2018 +0900
    
        commit message
    
    commit a5f0f5fa9128f9faec27bac8d593feacfac8e09b
    Author: onion <onion@onion.com>
    Date:   Wed Sep 19 11:47:09 2018 +0900
    
    test
    
  • 方法2 git log --graph --oneline --decorate=full -20 --date=short を利用

    *   47a9cd5 (HEAD -> refs/heads/develop) Merge branch 'test_branch' into develop
    |\
    | * fb290e8 (refs/heads/test_branch) new branch
    |/
    * e9be86d (refs/remotes/origin/develop) commit message
    * a5f0f5f test
    

    ブランチの情報なども確認できるので便利

一つ前のコミットのコミットメッセージを変更する

$ git commit --amend

エディタが開くので、そこでメッセージを修正・保存する。

10
26
1

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
10
26