3
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

gitの使い方 練習 備忘録

3
Posted at

gitの使い方&練習

gitの使い方を忘れないように備忘録を残しておきます。
環境はvagrant内のローカル環境のみです。
※主にドットインストールさんの動画を参考にしています。
なので基本的な使い方の練習といった感じです。
また備忘録なのであしからず

内容

1.初期設定(ローカル上ですべて完了します。)
2.コミット、現在の状況、ログ確認
3.差分確認
4.バージョン戻す
5.ブランチの設定、マージ、コンフリクトの解消
6.pushとpullなど

使用ディレクトリ

home/vagrant/app1
home/vagrant/app2
home/vagrant/repo.git

gitの設定

◆使用者の名前

git
git config --global user.name ""

◆使用者のメールアドレス

git
git config --global user.email ""

◆cuiで行うのでカラー表示をオンにします。

git
git config --global color.ui true

◆設定の確認

git
git config -l

ファーストコミット

◆gitの初期化 app1にて作業します。

git
git init

vimでindex.htmlファイルを作成する。

git
git add index.html

◆コミットします。

git
git commit

◆ログの確認

git
git log

◆オプション付き 1行のみ

git
git log --oneline

◆オプション付き 詳細内容

git
git log -p

◆オプション付き 変更内容確認

git
git log --stat

状態確認

index.htmlを変更します。

◆現在の状態確認

git
git status

◆編集を戻します。

git
git checkout -- index.html

※編集した内容が戻っています、確認してください。

差分確認

index.htmlを変更してください。

◆差分確認します。

git
git diff

◆ステージングに上げた状態で確認

git
git diff --cached

gitでのファイル操作(削除、移動)

◆適当にファイルを作成してgit addします。
 addしたファイルはgitコマンドでのみ削除可能です。

git
git rm index.css

git管理に含めないファイルの設定

◆不要なファイルを含めたくない時
git管理下に.gitignoreファイルを作りその中に含めたくないファイルのファイルパスを記載する。

直前のコミットを変更する

◆一度コミットしてもう一度コミットするがログに表示させないためのコマンド

git
git commit --amend

過去のバージョンに戻す

◆ログを確認して直前のコミットに戻す

git
git reset --hard HEAD

◆idを指定してコミットを戻す(大体コミットIDの7桁くらいあれば大丈夫っぽい)

git
git reset --hard 1f32sdf345

◆リセットしたけどリセット自体を戻したい時

git
git reset --hard ORIG_HEAD

※他にもコマンドはあるので状況に応じて使い分ける

共有リポジトリでの作業内容

◆共有リポジトリの設定repo.gitに移動する。

git
git init --bare

◆app1でファイルを作成してpushしてみます。
◆◆1リモートの設定(わからない場合はgit configで確認可能)

git
git remote add origin ~/repo.git

◆◆2ファイルをaddしてリポジトリにpushする

git
git push origin master

◆app2でリポジトリの中身を引っ張ってくる。
◆◆app2のディレクトリで行います。

git
git clone ~/repo.git

コンフリクトが起こった場合(よく仕事で使っていたやり方)

実際は手動でマージするのが多分普通なんでしょうが、どちらかのブランチの内容を
全適用する方法をとっていました。

ローカルを適用する場合:checkout --ours
リモートリポジトリを適用する場合:checkout --theirs

gitの便利な使い方エイリアスの使い方(git logでいい感じにグラフログを出力する)

git
git config --global alias.mylog "log --graph --date=short --decorate=short --pretty=format:'%Cgreen%h %Creset%cd %Cblue%cn %Cred%d %Creset%s'"

超簡単ですがこんな感じでgitを使っていきます。
後は使用していくうちに他に効率のよいやり方はないか見つけられると思います。

3
4
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
3
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?