LoginSignup
1
1

More than 5 years have passed since last update.

gitの使い方の忘備録

Last updated at Posted at 2019-02-15

はじめに

gitの勉強を少ししたのでその忘備録

git diff

git diff

これでファイルの変更を見れる

git diff --staged

git diff --staged

これでstagedされたファイルの変更を見れる。

git rm filename

git rm filename

これでgitとローカルのファイルからfilenameを削除できる

ディレクトリの場合は

git rm -r dirname

git rm --cached

git rm --cached filename

これでローカルからは削除せずgitからは削除する

git checkout -b

git checkout -b branchname

これでブランチを作成してそのあとそのブランチに移動する

git checkout .

git checkout .

これでgit add していないファイルを元の状態に戻す

こうゆう書き方もするらしい

git checkout -- .

git reset HEAD

git addしたファイルを元の状態に戻したい場合につかう

git reset HEAD filename

git commit --amend

直前のコミットを消し去る感じ

正直あんまり使い方わかっていないけど

直前のコミットをやり直したい場合に

git commit --amend
ファイル訂正やら追加や何かする
git add .
git commit -m "commit message"

みたいに使う

二つのコミットができてしまったのを一つで済ませられる

git pull

git pull origin branchname

これでoriginのbranchnameのファイルを取ってこれる

git fetch

これはリモートレポジトリのデータを取ってくるがローカルのファイルには影響を与えない

git fetch origin branchname

みたいにつかう

これだとまだローカルのファイルは変更されないのでmergeを使って変更をローカルのファイルに適応する

git merge

変更の内容を現在のbranchに適応する

fetchと組み合わせてリモートレポジトリのファイルをとってくるには

git fetch origin branchname

git branch -a <= remotes/origin/branchnameが追加されているのを確認する

git merge remotes/origin/branchname

みたいに使う

ローカルファイルでブランチを作成してその編集をmasterに適用するには

git checkout -b testbranch
何かしらの変更
git add .
git commit -m "message"

git checkout master
git merge testbranch

こんな感じにつかう

git rebase

複数のブランチを一つにまとめる

コンフリクトが起きたらめんどくさいことになる

pushしたファイルに対してrebaseをすると履歴がおかしくなりpushできなくなる

git checkout -b test
ファイルの変更
git add .
git commit -m "message"

この時点ではmasterとtestブランチがあるがこれを一つにまとめたい場合

git checkout test
git rebase master
git checkout master
git merge test

これでOK

単純に

git checkout master
git merge test

でもよいがrebaseすると履歴がきれいになる

git rebase -i HEAD~

あんまり理解できてない

git rebase -i HEAD~2

これで直前の2個のコミットに対して変更を与えれる

例えば二個前のコミットを消したい場合は

git rebase -i HEAD~2

としたら


pick cd462c0 Update asdf <=削除
pick 558970a end

# Rebase abdb3d0..558970a onto abdb3d0 (2 comma$
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit m$
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previou

こんな感じのが表示されるから

これで削除と書かれている行を削除すれば二個前のコミットを削除できる

ファイルも削除される

削除ではなく編集したい場合はpickの代わりにeditと書く

git rebase -i HEAD~2

edit cd462c0 Update asdf
pick 558970a end

# Rebase abdb3d0..558970a onto abdb3d0 (2 comma$
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit m$
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previou

とすると

Stopped at 4a813a6...  add edit1
You can amend the commit now, with

  git commit --amend

Once you are satisfied with your changes, run

  git rebase --continue

このように表示される

これはeditと書いたところでstopしたということと

何か変更を加えたらgit commit --amendしてそれば終わればgit rebase --continueしてということ

なので

ファイルに変更を加える
git add .
git commit --amend
git rebase --continue

これでOKなはず

stash

git stash

で現在のステージングされているファイル(add されていてcommitされていないもの)を退避されられる

退避させたファイルは

git stash list

で確認できて

git stash apply

で復元

git stash clear

でstashのファイルを削除できる

ちなみに

atom使い始めたのですがとても使いやすいです

とくにgitに関して

add commit などがテキストエディタ上でいろいろできます。

git plusという拡張ライブラリを使うとadd commit push pull 以外にもgitに関する細かいことができるようになって便利です。

wslを使う場合コマンドラインがwindowsのが表示されてしまういますが

platformio-ide-terminalという拡張ライブラリを取得して

設定で

shell argumentsに

C:\Windows\System32\bash.exe

と入れれば

ctrl + @ でubuntuのコマンドラインをテキストエディタで開けるようになります。

おわり:sunny:

1
1
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
1
1