LoginSignup
9
11

More than 3 years have passed since last update.

Gitのブランチ操作

Last updated at Posted at 2019-07-17

ブランチ周りの操作についてささっと。

リポジトリを新規追加しただけではブランチがmasterしかないので、開発にあたってブランチを追加する必要がある(ほうがいい)

ブランチ周りのコマンドについては以下

#ローカルブランチ一覧(カレントブランチには * が付く)
$ git branch

#リモートブランチ一覧
$ git branch -r

#新規ブランチを作成
$ git branch <New Branch name>

#ブランチの切替
$ git checkout <Next Branch name>

#ブランチの削除(警告なしは -D を使用)
$ git branch -d <Branch name>

新規ブランチ作成からcommitまで

新規ブランチにcommitするまでをやってみる。

practiceブランチを作成し、そこに切り替える

terminal
/Users/sf213471118/git/test
$ git branch practice            /* practiceブランチ作成 */
$ git branch                     /* 作成確認 */
* master
  practice
$ git checkout practice          /* ブランチ切替 */
Switched to branch 'practice'
$ git branch                     /* 切替確認 */
  master
* practice

②新しく作ったpracticeブランチはローカルなのでリモートにcommitしていく

commit用に新たにdiv.mdファイルを作成してみる(中身は適当)

terminal
/Users/sf213471118/git/test
$ echo "## this is a file for practice branch" >> div.md
$ ls
README.md   div.md

変更をコミットログに残すためaddしてからcommit

terminal
/Users/sf213471118/git/test
$ git add div.md
$ git commit -m "first commit on practice"
[practice 35de2c4] first commit on practice
 Committer: sf213471118 <xxxxx@sf213471118.local>
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly:

    git config --global user.name "Your Name"
    git config --global user.email you@example.com

After doing this, you may fix the identity used for this commit with:

    git commit --amend --reset-author

 1 file changed, 1 insertion(+)
 create mode 100644 div.md

commitは終了したのであとはリモートに反映させる。

pushするにあたり、新規ブランチなのでupstream branchが存在していない。
なので1回目のみ新たに設定する必要がある。

#upstream branchが存在していない場合(1回目以降のcommit)
$ git push --set-upstream origin <Branch name>

#upstream branchがすでに存在している場合(2回目以降のcommit)
$ git push origin <Branch name>

originが示すのはデフォルトのリポジトリの場所と覚えておけばOK。

つまり今回であればpracticeはリモートリポジトリではorigin practiceが正式のパス(?)みたいなもの。

もしoriginが抜けると以下のエラーを吐く

terminal
/Users/sf213471118/git/test
$ git push practice
fatal: 'practice' does not appear to be a git repository
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

なのでしっかりoriginを入れてpush開始。

今回はpracticeブランチにおける1回目のcommitなので

terminal
/Users/sf213471118/git/test
$ git push --set-upstream origin practice
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), 311 bytes | 311.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
remote: detect-secrets-stream (beta) ver=252-182e7d2e83422e0275e59e836c92720156echejs FAQ: xxxx
remote: 
remote: Successfully send push metadata.
remote: Push info collect pre-receive hook finished within 3 seconds.
remote: 
remote: Create a pull request for 'practice' on GitHub by visiting:
remote:      https://xxxxx
remote: 
To xxxx:sf213471118/test.git
 * [new branch]      practice -> practice
Branch 'practice' set up to track remote branch 'practice' from 'origin'.

これでターミナル側でのpush作業は終わり。

GitHub上で反映されているか確認して完了。

pullの作業

Gitでは他人が改変したものをローカルでまた書き換えていく作業をしていく。

このためにリモートに上がっているものをローカルに落とし込むのがpullである。

とりあえず今回はリモート上で先ほど使用したdiv.mdを書き換える。

div.md
## this is a file for practice branch
## changed by someone
~
~
~

これをターミナル上でpullしてローカルに落とし込む

terminal
/Users/sf213471118/git/test
$ git pull origin practice
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.
From xxxx:sf213471118/test
 * branch            practice   -> FETCH_HEAD
   23fw2c3..c41949a  practice   -> origin/practice
Updating 23fw2c3..c41949a
Fast-forward
 div.md | 3 +++
 1 file changed, 3 insertions(+)

pullした後はローカルで確認

terminal
/Users/sf213471118/git/test
$ cat div.md 
## this is a file for practice branch
## changed by someone
9
11
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
9
11