Skip to content

Instantly share code, notes, and snippets.

@tokunami
Last active August 29, 2015 14:03
Show Gist options
  • Save tokunami/6627dda12728faa00a75 to your computer and use it in GitHub Desktop.
Save tokunami/6627dda12728faa00a75 to your computer and use it in GitHub Desktop.
Fork(必要に応じて)
  • Forkボタンをおす
clone
  • 手元の開発環境にリポジトリをclone
$ git clone [email protected]:tokunami/sample.git
Cloning into 'sample'...
remote: Counting objects: 5, done.
remote: Compressing objects: 100% (4/4), done.
remote: Total 5 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (5/5), done.
Checking connectivity... done.

'sample'ディレクトリ以下にGitリポジトリが作成される

  • ディレクトリ移動
$ cd sample
branch
  • cloneしたリポジトリのブランチを確認
$ git branch -a
* master
  remotes/origin/HEAD -> origin/master
  remotes/origin/master

現在のブランチ(ここではmaster)に*がついている

  • トピックブランチを作成する
$git checkout -b work master
Switched to a new branch 'work'
  • workブランチに切り替わっていることを確認
$git branch -a
  master
* work
  remotes/origin/HEAD -> origin/master
  remotes/origin/master

workに*がついている→現在のブランチがworkになっている

コードを編集
add(新規ファイルを追加した場合)
$git add scode.js
commit
  • 既存ファイルを書き換えた場合は git diff コマンドで修正が正しく行われているか確認する
$git commit -am "commit message"
[work 8a54f32] commit message
 1 file changed, 6 insertions(+)
 create mode 100644 scode.js
  • -aオプション…ファイルの更新やファイルの削除をインデックスに加えてコミット
  • -mオプション…コミットメッセージを指定してコミット
push
$ git push origin work
Counting objects: 4, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 363 bytes | 0 bytes/s, done.
Total 3 (delta 1), reused 0 (delta 0)
To [email protected]:tokunami/sample.git
 * [new branch]      work -> work
  • GitHub側のリポジトリに修正を加えたブランチとして手元のworkに相当するブランチを作る
$ git branch -a
  master
* work
  remotes/origin/HEAD -> origin/master
  remotes/origin/master
  remotes/origin/work
  • ↑再終行に/origin/workが作成されている
pull request
  • pull requestボタンをおす
merge
  • 差分をチェックしてmergeする
※ブランチを切ることは必須ではない
@stoshiya
Copy link

diff, status, stashなどのサブコマンドは比較的よく使うものかと思います.使ってみると良いでしょう.

各gistはgitリポジトリになっていて,右上の方にURLをつかって手元で編集することも可能です.

$ git clone [email protected]:/6627dda12728faa00a75.git

@tokunami
Copy link
Author

tokunami commented Sep 5, 2014

git pushがrejectされたとき

To https://github.com/tokunami/AP-Monitor.git
 ! [rejected]        work -> work (fetch first)
error: failed to push some refs to 'https://github.com/tokunami/AP-Monitor.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.

自分がリモートの変更をpullしてからpushするまでの間に、他からのpushがあるなどしてリモートが変更されている状況。
自分のブランチの先端がリモートより後ろになっている。

→リモートの変更をマージしてから、改めてpushする。

対応 1) git pull
.git/config に指定されたリモートリポジトリの変更を取得し、 現在のブランチにマージするコマンド
http://transitive.info/article/git/command/pull/

対応 2) fetch して merge する

git fetch    (リモートの変更を取ってきて)
git merge origin/work    (マージする)

対応3) fetch して rebase する

git fetch    (リモートの変更を取ってきて)
git rebase origin/work

http://www.softel.co.jp/blogs/tech/archives/3569

@tokunami
Copy link
Author

tokunami commented Sep 5, 2014

git log --graph

@tokunami
Copy link
Author

tokunami commented Sep 5, 2014

git rebase -i HEAD~2
pickをfixupにする

@tokunami
Copy link
Author

tokunami commented Sep 5, 2014

git commit時のコメントを英語で書くための最初の一歩

http://www.sssg.org/blogs/hiro345/archives/11721.html

@tokunami
Copy link
Author

tokunami commented Sep 9, 2014

Pull Requestがきたら、内容を確認してMergeボタン押す→ $ git pull <remote> <branch>で手元環境に反映

実践! Pull Request対応
http://marutanm.hatenablog.com/entry/20110927/p1

GitHubでプルリクエストをマージする方法
http://garafu.blogspot.jp/2013/10/github.html

@tokunami
Copy link
Author

gitのリモートリポジトリの変更を手元に反映する
  • リモートリポジトリの確認
$ git remote -v

tokunami    https://github.com/tokunami/AP-Monitor.git (fetch)
tokunami    https://github.com/tokunami/AP-Monitor.git (push)
  • fetch
$ git fetch tokunami

↑このtokunamiはremote/tokunamiと関連する手元のbranch

  • branch の確認
$ git branch -a
*master
  • merge
$ git merge tokunami/master

↑from tokunami(fetchしてきたbranch)/to master(現在のbranch)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment