LoginSignup
63
68

More than 5 years have passed since last update.

Git(Hub)で作業をする一連の流れ 忘備録

Last updated at Posted at 2014-07-08

最近GitHubを使い始めたが勝手が全くわからないので自分の為にメモを残そうと思った次第です

(11/19:追記)
Git及びVCSの概念の理解を試みるべく、新しく記事を投稿しました。こちらの忘備録と合わせてお読みいただければと思います。
2留でもわかるGit

リモートリポジトリの作成

事前にGitHubでリモートリポジトリを作成します
リモートリポジトリの作成はこちらからできます
スクリーンショット 2014-11-24 17.43.26.png

リポジトリを作成すると以下のようにURLが表示されます。
スクリーンショット 2014-11-24 17.45.26.png

ローカルリポジトリの作成

まずはローカルにリポジトリを作成します

$ mkdir sample
$ cd sample
$ git init
Initialized empty Git repository in /Users/[User name]/git/sample/.git/

リモートリポジトリをクローンする場合はこちら

$ git clone [url]
$ git init
Initialized empty Git repository in /Users/[User name]/git/sample/.git/

追加するファイルをコミットします。

$ touch README.md
$ git add README.md
$ git commit -m "first commit"
[master (root-commit) b1e1bac] first commit
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 README.md

リモートリポジトリにpush

まずはリモートリポジトリを追加します。
リモートからクローンした場合は不要な作業です。

$ git remote add origin [url]

masterにpushします。

$ git push -u origin master
Counting objects: 3, done.
Writing objects: 100% (3/3), 211 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://github.com/[User name]/sample.git
 * [new branch]      master -> master
Branch master set up to track remote branch master from origin.

リポジトリ追加時のエラー

pushする過程でたまにこんなエラーメッセージが出る

$ git remote add origin https://github.com/[User name]/sample.git
fatal: remote origin already exists.

このメッセージはリモートリポジトリが既に使われている名前で登録すると吐かれるのでとりあえず登録されているリポジトリの名前とURLを確認

$ git remote -v
origin  https://github.com/[User name]/sample.git

解決策①

別の名前でリモートリポジトリを登録

$ git remote add origin2 https://github.com/[User name]/sample.git
$ git remote -v
origin  https://github.com/[User name]/sample.git
origin2 https://github.com/[User name]/sample.git

解決策②

リポジトリを削除してから再作成

$ git remote rm origin
$ git remote add origin https://github.com/[User name]/sample.git
$ git remote -v
origin  https://github.com/[User name]/sample.git

ローカルにブランチを作成

ブランチを作成します

$ git branch <branchname>

masterから作成したブランチに切り替えます

$ git checkout <branchname>
Switched to branch '<branchname>'

ブランチにファイルをコミットします

$ touch oreo.md
$ git add oreo.md
$ git commit -m "oreo is delicious"
[<branchname> 4337778] oreo is delicious
1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 oreo.md

※ブランチを切って移動する作業はgit checkout -b <branchname>でも可能

masterにブランチの内容をマージ

ブランチをmasterに戻します

$ git checkout master

ブランチの内容をmasterにmergeします

$ git merge <branchname>
Updating a0e30b2..4337778
Fast-forward
oreo.md | 0
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 oreo.md

例の如くpush

$ git push origin master
Counting objects: 3, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 247 bytes | 0 bytes/s, done.
Total 2 (delta 0), reused 0 (delta 0)
To https://github.com/[User name]/sample.git
a0e30b2..4337778  master -> master
Branch master set up to track remote branch master from origin.

補足 リモートリポジトリにブランチをpush

$ git checkout <branchname>
Switched to branch '<branchname>'
$ git push origin <branchname>
Total 0 (delta 0), reused 0 (delta 0)
To https://github.com/[User name]/sample.git
 * [new branch]      <branchname> -> <branchname>
Branch <branchname> set up to track remote branch <branchname> from origin.

origin masterではなくorigin <branchname>にするだけ

$ git branch -a
master
* <branchname>
remotes/origin/master
remotes/origin/<branchname>

全てのブランチを確認するとremotes/origin/<branchname>が追加されている。

疑問点①

リモートリポジトリにブランチをpushする意味を把握できていない。
記事にはまとめてみたものの未だに使ったことはない。
ブランチ切って移動→作業→commit→push→masterにmergeするのと
ブランチ切って移動→作業→commit→push→リモートリポジトリにブランチをpush→masterにmergeする違いがよくわからない
結局masterにmergeしたらbranchは削除しちゃうし一体何のためにpushするのだろう...

疑問点②

git push -u origin masterを叩くとエラーを吐かれる

$ git push -u origin master
To https://github.com/ore0/sample.git
 ! [rejected]        master -> master (fetch first)
error: failed to push some refs to 'https://github.com/ore0/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.

git pullすれば直るなんて解説もあるけどgit pullがそもそも通らない
まっさらなrepositoryだと問題なくpushできるでおそらくファイルの整合性の問題なんだろうか...


不備などが多々あると思うのでお気づきの際はコメント欄までよろしくお願いします。

それにしてもGitHubは便利だけど難しい 多分行き当たりばったりで使っているのがいけないようで。やっぱり根本的な部分を勉強しないといけないんだろな...

参考にさせて頂いたサイト等

basyura's blog

綾小路龍之介の素人思考

サルでもわかるGit入門

63
68
2

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
63
68