【Git】gitの使い方(gitコマンド集)

公開日時:2021年09月18日 / 最終更新日時:2022年08月28日

git コマンドについて解説します。

 

git bash もしくは Cloud9 がおすすめ

git コマンドを利用する場合は、git bash か Cloud9 がおすすめです。

デフォルトで現在選択しているブランチがかっこの中に表示されます。

【例】

test:~/work (main) $ git branch -a

 

 

初期設定

■ユーザ名とメールアドレスを登録する

$ git config --global user.name "Taro Yamada" ← ユーザ名を登録する
$ git config --global user.email taro_yamada@example.com ← メールアドレスを登録する

 

 

■ユーザー名と E メールアドレスをローカルレポジトリに追加する

$ git config --local user.name "test01 user" ← ユーザ名をローカルレポジトリに登録する

$ git config --local user.email xxxx@gmail.com ← メールアドレスをローカルレポジトリに登録する

 

情報収集

■バージョン確認

$ git --version
git version 2.33.0.windows.2

 

 

リポジトリの操作

■CodeCommitのリモートリポジトリを登録する

$ git remote add origin https://git-codecommit.ap-northeast-1.amazonaws.com/v1/repos/MyAngularRepo01

 

「git remote add origin リモートリポジトリ(CodeCommitのリポジトリ)の場所」コマンドで、現在のローカルリポジトリに指定したリモートリポジトリを追加します。

 

 

 

 

■AWS CodeCommitのレポジトリをローカルにコピーする

git clone https://git-codecommit.ap-northeast-1.amazonaws.com/v1/repos/MyAngularRepo 
Cloning into 'MyAngularRepo'... 
warning: You appear to have cloned an empty repository. 

 

 

作業

git コマンドを使用した作業です。

 

最初に git pull 

まずは最初に git pull コマンドを実施します。

$ git pull

 

git pull コマンドを実行すると git リポジトリにある最新のソースコードを取得することができます。

 

 

ブランチを作成する

ブランチを作成します。

ブランチは命名規則を作り分かりやすいブランチ名にします。

 

■ブランチを作成する

$ git branch aiproj-prod-20220621

 

【例】

AIプロジェクトの本番環境で2022年6月21日に作成

 

 

■現在のブランチを確認する

$ git branch

aiproj-prod-20220621

* master ← *(アスタリスクが付いているブランチが現在選択されているブランチ)

 

現在作成されているブランチを確認します。

 

 

■ブランチを削除する

$ git branch -d aiproj-prod-20220621

 

不要になったブランチは -d オプションを付けて削除します。

 

■ブランチを変更する

$ git checkout aiproj-prod-20220621
$ git checkout master

 

git checkout [ブランチ名] でブランチを変更することができます。

コミットしていないファイルがあると切り替えられません。

 

 

 

■自分自身が削除対象のブランチにチェックアウトしていた場合はエラーになる

$ git branch
* aiproj-prod-20220621
master
$ git branch -d aiproj-prod-20220621
error: Cannot delete branch 'aiproj-prod-20220621' checked out at '/home/ec2-user/aws'

 

 

 

ローカルのソースコードを改修した場合

 

■ステータスを確認する

git status 
On branch master 
Changes not staged for commit: 
  (use "git add ..." to update what will be committed) 
  (use "git restore ..." to discard changes in working directory) 
        modified:   buildspec.yml 
 
no changes added to commit (use "git add" and/or "git commit -a") 

 

 

■変更内容をインデックスに追加する

$ git add .

 

 

 

■再度ステータスを確認する

$ git status 
On branch master 
Changes to be committed: 
  (use "git restore --staged ..." to unstage) 
        modified:   buildspec.yml 
 
 

 

 

 

■コミットする

$ git commit -m "first Commit"

 

 

 

■プッシュする

git push origin master 
Enumerating objects: 5, done. 
Counting objects: 100% (5/5), done. 
Delta compression using up to 8 threads 
Compressing objects: 100% (3/3), done. 
Writing objects: 100% (3/3), 304 bytes | 304.00 KiB/s, done. 
Total 3 (delta 2), reused 0 (delta 0), pack-reused 0 
To https://git-codecommit.ap-northeast-1.amazonaws.com/v1/repos/MyAngularRepo 
   1ff32be..a6b748b  master -> master 

 

 

 

■プッシュする

git push https://git-codecommit.ap-northeast-1.amazonaws.com/v1/repos/MyAngularRepo --all 
Enumerating objects: 56, done. 
Counting objects: 100% (56/56), done. 
Delta compression using up to 8 threads 
Compressing objects: 100% (54/54), done. 
Writing objects: 100% (56/56), 140.03 KiB | 5.60 MiB/s, done. 
Total 56 (delta 10), reused 0 (delta 0), pack-reused 0 
To https://git-codecommit.ap-northeast-1.amazonaws.com/v1/repos/MyAngularRepo 
 * [new branch]      master -> master 

 

 

リモートのブランチ一覧を表示する

■リモートのブランチ一覧を表示する

test:~/work/test_branch (main) $ git fetch ← リモートリポジトリから最新の情報を取得する。
test:~/work/test_branch (main) $ git branch -a ← リモートリポジトリのブランチ一覧を表示する。
* main
remotes/origin/HEAD -> origin/main ← origin とあるのはローカルにある、リモートブランチを追跡するブランチです。
remotes/origin/feature/test_branch
remotes/origin/feature/test_branch1
remotes/origin/feature/test_branch2
remotes/origin/feature/test_branch3
test:~/work/test_branch (main) $

 

いきなり git branch -a コマンドを実行しても、表示されたブランチ一覧は最新のものとは限りません。

git branch -a コマンドを実行する前に、git fetch コマンドで最新の情報を取得してからにします。

 

 

git の管理から外す場合

ソースコードとは関係のないファイルは git から管理を外したい場合もあります。

 

■git の管理から外す場合

$ git rm package-lock.json
rm 'test/test_branch/package-lock.json'

 

 

トラブル対応

複数人で git でソースコードを管理しているといろんなトラ物が発生します。

ここからはトラブル対応について記載します。

 

 

間違って削除したファイルを戻したい

間違ってファイルを削除してしまった場合です。

 

■間違って削除してしまったファイルを元に戻す(git add コマンド実行前の状態)

$ git status
On branch feature/cpu_memory_20220308
Changes not staged for commit:
(use "git add/rm <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: test/CommonBatch/package-lock.json
deleted: test/CommonBatch/fonts1.zip ← 間違って削除したので元に戻したい
deleted: test/CommonBatch/fonts2.zip ← 間違って削除したので元に戻したい

 

$ git checkout test/CommonBatch/fonts1.zip ← git checkout xxx で元に戻す
Updated 1 path from the index
$ git checkout test/CommonBatch/fonts2.zip ← git checkout xxx で元に戻す
Updated 1 path from the index
$ git status
On branch feature/cpu_memory_20220308
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: test/CommonBatch/package-lock.json
modified: checkout test/CommonBatch/fonts1.zip ← deleted  から modifiedになっている。

modified: checkout test/CommonBatch/fonts2.zip ← deleted から modifiedになっている。

no changes added to commit (use "git add" and/or "git commit -a")
$

 

 

 

Untracked files を消したい

「Untracked files」と表示されているファイルがソースコードに不要なので消したい場合です。

 

■Untracked files のファイルを消したい

$ git clean -f
$ git status

 

 

git clean -f コマンドでも「Untracked files」の表示が消えない場合は rm コマンドで物理的に削除してしまいましょう。

 

■git clean -f コマンドでも消えない場合

$ rm -rf open_prod_20220811 ← rm コマンドでフォルダごと削除します。

 

error: Your local changes to the following files would be overwritten by checkoutが出力される場合

■error: Your local changes to the following files would be overwritten by checkoutが出力される場合

$ git checkout main
error: Your local changes to the following files would be overwritten by checkout:
test_branch1/service.tf
Please commit your changes or stash them before you switch branches.
Aborting

 

$ git stash -u ← コミットはせずに変更を取り消したい場合は git stash コマンドを実行します。
Saved working directory and index state WIP on open-dev-20220719: 02e6744 open-dev-20220719

 

 

 

ファイルの容量が大きすぎてエラーになる場合

ログファイルなど1つのファイルが肥大化して100MB以上になった場合です。

 

■ファイルの容量が大きすぎてエラーになる

$ git push origin close-net-prod-20220713
Enumerating objects: 28, done.
Counting objects: 100% (28/28), done.
Delta compression using up to 2 threads
Compressing objects: 100% (21/21), done.
Writing objects: 100% (27/27), 54.63 MiB | 2.21 MiB/s, done.
Total 27 (delta 1), reused 0 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
remote: error: Trace: 58af107e1xxxxxxxxxxxxxx6e96ae5f56916df7cd53b2c463
remote: error: See http://git.io/iEPt8g for more information.
remote: error: File test_branch/.terraform/providers/registry.terraform.io/hashicorp/aws/4.22.0/linux_amd64/terraform-provider-aws_v4.22.0_x5 is 259.80 MB; this exceeds GitHub's file size limit of 100.00 MB
remote: error: GH001: Large files detected. You may want to try Git Large File Storage - https://git-lfs.github.com.
To github.com:test/terraform-repo1.git
! [remote rejected] close-net-prod-20220713 -> close-net-prod-20220713 (pre-receive hook declined)
error: failed to push some refs to 'github.com:test_branch/terraform-repo1.git'

 

 

■対応方法(.gitignoreファイルを設定する)

$ vi .gitignore
*.tfstate
*.tfstate.backup
.terraform

 

 

■.gitigonore ファイルを設定後に一度キャッシュをクリアする

$ git rm -r --cached .
rm 'test_branch1/.gitignore'
rm 'test_branch1/.terraform.lock.hcl'
rm 'test_branch1/.terraform/providers/registry.terraform.io/hashicorp/aws/4.22.0/linux_amd64/terraform-provider-aws_v4.22.0_x5'
rm 'test_branch1/.terraform/terraform.tfstate'
rm 'test_branch1/main.tf'
rm 'test_branch1/s3.tf'
rm 'test_branch1/terraform.tf'
rm 'test_branch1/variables.tf'
$

 

 

■キャッシュクリア後に再度 push する

$ git push origin close-net-prod-20220713

 

 

 

リモートにあるリポジトリをローカルに持ってきたい場合

単純に git pull コマンドを実行してもローカルにリポジトリを持ってこれないことがあります。

 

■ローカルにはリポジトリがない状態

$ git branch
key-prod-20220613
* main
waf-prod-20220624
waf-dev-20220624

 

 

 

■git pull コマンドを実行してもローカルには持ってこれない状態

$ git pull

$ git branch ← git pull 後に確認してもローカルにリポジトリがない状態
key-prod-20220613
* main
waf-prod-20220624
waf-dev-20220624

 

 

■リモートにはちゃんとリポジトリがある状態

$ git branch -r
origin/HEAD -> origin/main
origin/ai-prod-20220719 ← リモートにはある

 

 

■以下のコマンドでローカルにリポジトリを持ってくる

$ git fetch

 

$ git branch ← git pull 後に確認してもローカルにリポジトリがない状態
key-prod-20220613
* main
waf-prod-20220624
waf-dev-20220624

 

$ git checkout ai-prod-20220719 ← git branchからは見えてこないが、構わずに git checkout [リポジトリ] をする。

branch 'ai-prod-20220719' set up to track 'origin/ai-prod-20220719'.
Switched to a new branch 'ai-prod-20220719'
$ git branch
* ai-prod-20220719 ← ai-prod-20220719 リポジトリがローカルにある。
key-prod-20220613
main
waf-prod-20220624
waf-dev-20220624

$

 

 

 

Posted by 100%レンタルサーバーを使いこなすサイト管理人

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

AlphaOmega Captcha Medica  –  What Do You See?
     
 

このサイトはスパムを低減するために Akismet を使っています。コメントデータの処理方法の詳細はこちらをご覧ください

Secured By miniOrange