Skip to content

Instantly share code, notes, and snippets.

@uupaa
Last active May 8, 2019 04:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save uupaa/6986937 to your computer and use it in GitHub Desktop.
Save uupaa/6986937 to your computer and use it in GitHub Desktop.
git github git command

github flow

https://gist.github.com/uupaa/4e179b1f720d564e2d3e3f3ec4573e2f

revision 間の比較をとる

https://github.com/USER-NAME/REPOSITORY/compare/A...B で A - B 間の diff を取れる

例: https://github.com/uupaa/WebModule/compare/d578d13270b5bfc607cca7a6ed21eb5daa547316...98dfe841e6dcdcc96e6f9ce95ac88166e5322b2a

issues をクローズする

# + issues-id 付きで commit する

$ git commit -m "#3 fixed"

#issues-id を付け忘れてコミットした時は、issues に @ + hash を付けてコメントすると、ハッシュの示すコミットにリンクが張られる

@hash

間違って push してしまった

$ git push -f origin HEAD^:master

でリモートのpushを取り消し、

$ git reset --soft HEAD^

で、ローカルの変更を取り消すと、コミットとpushをやり直しできます。

リモートブランチをローカルにもってくる

git pull は現在のブランチ(masterにいる場合はmasterに)にリモートブランチを取り込みます。

リモートブランチをローカルに取得したいだけの場合は、git pull ではなく、git branch を行います。

$ git branch remote-branch origin/remote-branch

または、 git checkout -b remote-branch origin/remote-branch とすると、同時にチェックアウトもしてくれます。

$ git checkout -b remote-branch origin/remote-branch

ブランチ間の差分を表示したい

// git diff --name-status [ブランチ1] [ブランチ2]

$ git diff --name-status master develop

D	demos/icons/base.css
A	demos/mydemo/Hoge.js
M	demos/mydemo/manifest.json
 :
 :
// ファイルの差分を表示する, ブランチをまたがった差分表示も可能
// git diff [ブランチ:]ファイルパス [ブランチ:]ファイルパス

$ git diff master:Hoge.js develop:Hoge.js

HEAD と 作業ツリー(git add済みのファイル)との差分が見たい

$ git diff HEAD

HEAD と 作業ツリー(git add済みのファイル)の特定のファイルの差分が見たい

$ git diff HEAD src/path

git commit を取り消して git add したい

git commit してしまったが、ファイルを入れ忘れた事に気がついたので、git commit をやり直したい

$  git commit --amend

このコマンドを実行すると、直前のcommitが無効化され、新しいコミットが作成されます(新しいコミットは新しい hash 値を持ちます)。

最後のコミットを取り消す

最後のコミット(02abdb...)そのものを取り消すには、git reset --soft HEAD^ を実行します。 「git reset --soft」は、ワークディレクトリの内容(追加したファイルや修正したファイル)はそのままでコミットだけを取り消したい場合に使用します。

$ git log
commit 02abdb...
commit 34efd0...

$ git reset --soft HEAD^

実行後に git log を見てみると 02abdb が消えています

git reset --hard を行うと、ワークディレクトリの内容も取り消されます(追加/修正したファイルが消えます)

ブランチの一覧を表示する

git branch でローカルブランチ。git branch -a でリモートブランチも含めて表示します。

$ git branch -a

* master
  develop
  origin/master
  origin/develop

不要になったブランチを削除する

不要になったブランチを削除します。

$ git branch -a

* master
  develop
  origin/master
  origin/develop

の状態で、git branch -d develop とするとローカルのdevelopブランチが消えます。

$ git branch -d develop

Deleted branch develop (was .....).

さらに、git push origin :develop とすると、リモートのdevelopブランチも消えます。ブランチ名の前に:が必要です。

$ git push origin :develop

developブランチにいる状態で、developブランチを削除しようとするとエラーになります。

$ git branch
* develop

$ git branch -d develop
error: Cannot delete the branch 'develop' which you are currently on.

tag をつける

git tag -a v1.0.0 -m "version 1.0.0"
git push --tags

タグをつけると、GitHub Release のページに乗る

tag を削除する

ローカルで削除して、リモートにpushすると消える

git tag -d TAGNAME
git push origin :TAGNAME
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment