LoginSignup
22
24

More than 3 years have passed since last update.

gitの基本(init~push)+@

Last updated at Posted at 2018-11-02

git init

command-line
$ git init
Initialized empty Git repository in ~/my-first/.git/
$ git config --global user.name "Your Name"
$ git config --global user.email you@example.com

コミットしたくないファイルは.gitignoreファイルを作って下記のように編集

例)

.gitignore
*.pyc
*~
__pycache__
.DS_Store

状態の確認

command-line
$ git status
On branch master

Initial commit

Untracked files:
  (use "git add <file>..." to include in what will be committed)

   ...
   ...

nothing added to commit but untracked files present (use "git add" to track)

addしてコミット

command-line
$ git add --all .
$ git commit -m "first commit"

Githubでレポジトリ作成後,ローカルレポジトリをリモートに追加する

githubでレポジトリ作成
レポジトリ作成後,以下を実行

command-line
$ git remote add origin https://github.com/<your-github-username>/my-first-repository.git
$ git push -u origin master

終了!!後は自分のgithubアカウントで確認

リモートレポジトリの編集

登録されているリモートリポジトリの確認
git remote -v
リモートリポジトリURLの編集
git remote set-url [repository name] [repository url]
リモートリポジトリの削除
git remote rm [repository name]

repository nameはoriginとか

pull

git pull origin master

ブランチの変更

$ git fetch
$ git branch -a
$ git checkout <branch>

# リモートブランチをローカルにチェックアウトする
$ git checkout -b local_branch_name origin/remote_branch_name

branchの作り方→remoteにpush

# ブランチを作成
git checkout -b "#1"
git push -u origin "#1"

git でミスったとき

間違えてcommitして,stagingに戻したい時

git reset --soft HEAD~

間違えてcommitしたコミットを消したい時

git reset --hard HEAD

間違えてpush[1]

ローカルでrevertを使って取り消して、それをリモートへ反映する。

revertは相殺で取り消すのでログからは消えない。(正確には取り消しではなくて打消し)
resetだとログから完全に消えていまい危険なのでrevert推奨らしい。

1.取り消したいコミットのコミットIDを取得する。

$git log 
commit XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Author: Foo Bar <XXX@XXXX>
Date:   Tue Mar 15 10:26:14 2016 +0900
,
    XXXを修正

2.取り消したいコミットをrevertする。
複数のコミットを消したい場合はこれが複数になるだけ。

$git revert <commit id>

3.リモートへpushする

$git push

特定fileのreset(HEADに戻す)

git checkout HEAD -- test_file.txt

変更の退避(stash)

変更を退避する

git stash save

退避した変更の確認

$ git stash list
stash@{0}: WIP on #1: xxxxxx
stash@{1}: WIP on branch_name: xxxxxx

退避した作業を戻す

// stash@{0}をもとに戻す
$git stash apply stash@{0}
// stash@{0}をもとに戻す + addした状態にする
$git stash apply stash@{0} --index
// stash@{0}の作業をもとに戻す + 退避作業の中から削除します
$git stash pop stash@{0}

その他

// stash@{0}を削除
$git stash drop stash@{0}
// stashの詳細を見る
$git stash show stash@{0} -p
// stashのクリア
$git stash clear

ローカルだけで使いたいファイルがある時(credentialなど)

git update-index --skip-worktree file_name

https://qiita.com/nishina555/items/f54fadd16c4f806fd91c

REF

1.【Git】間違ってpushした場合の取り消し方
2. git reset (--hard/--soft)ワーキングツリー、インデックス、HEADを使いこなす方法

22
24
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
22
24