[A-00041]Git/Github使い方まとめ

gitのコマンドの使い方を記載しておく。とりあえずこれ見れば一通り使い方がわかる程度に書いておく。

・github-cliをインストールする

こちらはAnaconda(or Minikonda)での例です。

conda install gh --channel conda-forge

Anacondaのインストールはこちら

・githubのissueとの紐付けしたコメント

#番号をコメントに入れると自動でgithubが紐付けしてくれる

git commit -m "#1 add csr terraform files."

・gitのユーザーを設定する/確認する

git config --global user.name <user-name>
git config --global user.name
MacBook-Pro:~$ git config --global user.name "tanakaryo"
MacBook-Pro:~$ git config --global user.name
tanakaryo

・gitのE-mailアドレスを設定する/確認する

git config --global user.mail <your-email-address>
git config --global user.mail
MacBook-Pro:~$ git config --global user.mail "xxxx@gmail.com"
MacBook-Pro:~$ git config --global user.mail
xxxx@gmail.com

・gitのバージョンを確認する

git --version
anonymous-MacBook-Pro:$ git --version
git version 2.24.3 (Apple Git-128)

・helpを参照する

git --help
anonymous-MacBook-Pro:$ git --help
usage: git [--version] [--help] [-C <path>] [-c <name>=<value>]
           [--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]
           [-p | --paginate | -P | --no-pager] [--no-replace-objects] [--bare]
           [--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]
           <command> [<args>]

These are common Git commands used in various situations:

start a working area (see also: git help tutorial)
   clone     Clone a repository into a new directory
   init      Create an empty Git repository or reinitialize an existing one

work on the current change (see also: git help everyday)
   add       Add file contents to the index
   mv        Move or rename a file, a directory, or a symlink
   restore   Restore working tree files
   rm        Remove files from the working tree and from the index

examine the history and state (see also: git help revisions)
   bisect    Use binary search to find the commit that introduced a bug
   diff      Show changes between commits, commit and working tree, etc
   grep      Print lines matching a pattern
   log       Show commit logs
   show      Show various types of objects
   status    Show the working tree status

grow, mark and tweak your common history
   branch    List, create, or delete branches
   commit    Record changes to the repository
   merge     Join two or more development histories together
   rebase    Reapply commits on top of another base tip
   reset     Reset current HEAD to the specified state
   switch    Switch branches
   tag       Create, list, delete or verify a tag object signed with GPG

collaborate (see also: git help workflows)
   fetch     Download objects and refs from another repository
   pull      Fetch from and integrate with another repository or a local branch
   push      Update remote refs along with associated objects

'git help -a' and 'git help -g' list available subcommands and some
concept guides. See 'git help <command>' or 'git help <concept>'
to read about a specific subcommand or concept.
See 'git help git' for an overview of the system.

・リポジトリを初期化する

主に新しくリポジトリを作成する際に、新規フォルダを作成してそこで実行するコマンド。実行後にはgitの管理用フォルダが作成される。ここで資材を追加していけば新規リポジトリに登録する事ができる。

git init

新規フォルダを作成してgit初期化コマンドを実行するケース

anonymous-MacBook-Pro:$ mkdir git-test
anonymous-MacBook-Pro:$ chmod 777 git-test/
anonymous-MacBook-Pro:$ cd git-test
anonymous-MacBook-Pro:$ git init
Initialized empty Git repository in /Users/anonymous/Documents/test/git-test/.git/
anonymous-MacBook-Pro:$ ls -la
total 0
drwxrwxrwx   3 anonymous  staff   96 Jun  8 08:33 .
drwxr-xr-x@ 19 anonymous  staff  608 Jun  8 08:32 ..
drwxr-xr-x   9 anonymous  staff  288 Jun  8 08:33 .git

・資材を追加する

上記で作成した新規リポジトリで資材をコミットしてみる。コミットする前にまず資材を追加してやる必要がある

git add

適当な資材を作成して上記コマンドを実行する。

anonymous-MacBook-Pro:$ touch test.txt
anonymous-MacBook-Pro:$ chmod 777 test.txt 
anonymous-MacBook-Pro:$ echo "test,test,test" > test.txt 
anonymous-MacBook-Pro:$ git add test.txt 

・資材の名称を変更する

git mv <current-name> <changed-name>
MacBook-Pro:git-demo$ git mv test-shell.txt test-Shell.txt

・資材をコミットする

前述のaddコマンドの後にコミットを実行してみる。

git commit
anonymous-MacBook-Pro:$ git commit

上記を実行するとコミットコメントを入力する画面が表示されるので適当なコメントを入力する。入力したら[esc]キーを押して[:wq]で終了する。

initial commit
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# On branch master
#
# Initial commit
#
# Changes to be committed:
#       new file:   test.txt
#
# Changes not staged for commit:
#       modified:   test.txt
#
~                             
anonymous-MacBook-Pro:$ git commit
[master (root-commit) ae54ff3] initial commit
 1 file changed, 1 insertion(+)
 create mode 100755 test.txt
anonymous-MacBook-Pro:$

・更新した資材をコミットする

git commit . -m "<comment>"

・コミットを取り消す

コミットを取り消す例を記載する。

適当に作成したファイルをコミットして、コミットログを確認します。一番上がpush前のコミットログです。

MacBook-Pro:git-demo$ git log --oneline
0ca113a (HEAD -> master) feat:add new feature.
9c09c1e (origin/master, origin/HEAD) fix:delete file.
5002160 fix:change filename.
5d22292 fix:change file-name.
5a58600 Merge branch 'feature1'
0b5887e feat:add new feature.
a308167 fix:delete a feature.
5bee4f1 feat:add new feature.
c8b68e1 feat:add new text
07c8c1c test from local
a1fd684 test
acdf164 aiute

コミットを取り消す場合は[reset]コマンドでコミットIDを指定します。

git reset <commit-id>

取り消したいのは最新のコミット(HEAD)なので下記のように指定します。

MacBook-Pro:git-demo$ git reset --hard HEAD^
HEAD is now at 9c09c1e fix:delete file.
MacBook-Pro:git-demo$ git log --oneline
9c09c1e (HEAD -> master, origin/master, origin/HEAD) fix:delete file.
5002160 fix:change filename.
5d22292 fix:change file-name.
5a58600 Merge branch 'feature1'
0b5887e feat:add new feature.
a308167 fix:delete a feature.
5bee4f1 feat:add new feature.
c8b68e1 feat:add new text
07c8c1c test from local
a1fd684 test
acdf164 aiute

・ログを確認する

先ほどのコミットログを確認してみる。

git log
anonymous-MacBook-Pro:$ git log
commit ae54ff3ce0a2afbd0d38d90b800a37b166719850 (HEAD -> master)
Author: anonymous <anonymous@icloud.com>
Date:   Thu Jun 8 09:07:50 2023 +0900

    initial commit
anonymous-MacBook-Pro:$

・ログを確認する(もう少し見やすい方法)

こちらの方が先ほどのより見やすく表示してくれます。

git log --oneline
MacBook-Pro:git-demo$ git log --oneline
5a58600 (HEAD -> master) Merge branch 'feature1'
0b5887e (origin/feature1, feature1) feat:add new feature.
a308167 (origin/master, origin/HEAD) fix:delete a feature.
5bee4f1 feat:add new feature.
c8b68e1 feat:add new text
07c8c1c test from local
a1fd684 test
acdf164 aiute

・クローンを作成する

master(main)からクローンを作成してみる。

git clone <remote-repo-URL>
anonymous-MacBook-Pro:git-test$ git clone https://github.com/tanakaryo/git-demo.git
Cloning into 'git-demo'...
remote: Enumerating objects: 8, done.
remote: Counting objects: 100% (8/8), done.
remote: Compressing objects: 100% (5/5), done.
remote: Total 8 (delta 1), reused 7 (delta 0), pack-reused 0
Unpacking objects: 100% (8/8), done.
anonymous-MacBook-Pro:git-test$ ls
git-demo

・ブランチを作成する

masterから別のブランチを作成します。試しにfeature1というブランチを作成します。

git branch <branch-name>
anonymous-MacBook-Pro:git-test$ ls
git-demo
anonymous-MacBook-Pro:git-test$ cd git-demo/
anonymous-MacBook-Pro:git-demo$ git branch feature1

・ブランチを確認する

masterから作成されたブランチの一覧を確認します。

git branch
anonymous-MacBook-Pro:git-demo$ git branch
  feature1
* master

・ブランチを切り替える

上記で作成したブランチに作業を移すため、masterからfeature1にブランチを切り替えます。

git checkout <branch-name>
MacBook-Pro:git-demo$ git checkout feature1
Switched to branch 'feature1'
MacBook-Pro:git-demo$ ls
demo.txt		test-from-local.txt	test-from-vscode.txt

・ブランチをリモートリポジトリにプッシュする

ローカルに作成したブランチをリモートリポジトリにプッシュします。githubのgit-demo

リモートリポジトリ目掛けてプッシュします。remote-nameはデフォルトなのでoriginとなります。

git push <remote-name> <branch-name>
anonymous-MacBook-Pro:git-demo$ git push origin feature1
Total 0 (delta 0), reused 0 (delta 0)
remote: 
remote: Create a pull request for 'feature1' on GitHub by visiting:
remote:      https://github.com/tanakaryo/git-demo/pull/new/feature1
remote: 
To https://github.com/tanakaryo/git-demo.git
 * [new branch]      feature1 -> feature1

上記を実行後、github上にfeature1というブランチが表示されます。

・ブランチ上でadd,commitしてリモートブランチにpushする

先ほど作成したブランチ・feature1で変更を追加し、コミットしてリモートリポジトリのブランチにプッシュしてみます。

まずはテスト用ファイルを作成します。

MacBook-Pro:git-demo$ touch test-from-branch.txt && chmod 777 test-from-branch.txt
MacBook-Pro:git-demo$ ls
demo.txt		test-from-branch.txt	test-from-local.txt	test-from-vscode.txt

次に作成したファイルをaddします。

MacBook-Pro:git-demo$ git add test-from-branch.txt 

次にcommitします。

MacBook-Pro:git-demo$ git commit -m "feat:add new feature." 
[feature1 5bee4f1] feat:add new feature.
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100755 test-from-branch.txt

最後にリモートリポジトリにプッシュします。

MacBook-Pro:git-demo$ git push --set-upstream origin feature1
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Delta compression using up to 4 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 258 bytes | 258.00 KiB/s, done.
Total 2 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
To https://github.com/tanakaryo/git-demo.git
   07c8c1c..5bee4f1  feature1 -> feature1
Branch 'feature1' set up to track remote branch 'feature1' from 'origin'.

githubのfeature1に反映された事を確認します。

・資材を削除する

資材の削除方法を記載します。この作業はmasterブランチで行います。

git rm <item-name>
MacBook-Pro:git-demo$ git rm test-from-branch.txt 
rm 'test-from-branch.txt'
MacBook-Pro:git-demo$ ls
demo.txt		test-from-local.txt	test-from-vscode.txt

続けてmasterブランチに反映します。

MacBook-Pro:git-demo$ git commit -m "fix:delete a feature."
[master a308167] fix:delete a feature.
 1 file changed, 0 insertions(+), 0 deletions(-)
 delete mode 100755 test-from-branch.txt
MacBook-Pro:git-demo$ git push
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Delta compression using up to 4 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 238 bytes | 238.00 KiB/s, done.
Total 2 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
To https://github.com/tanakaryo/git-demo.git
   c8b68e1..a308167  master -> master

・ブランチをマージする

作成したfeature1ブランチをmasterブランチにマージします。

まずfeature1ブランチにしか存在しないファイルを作成します。

add, commit,pushを一気にやってしまいます。

MacBook-Pro:git-demo$ touch only-feature1.txt && chmod 777 only-feature1.txt
MacBook-Pro:git-demo$ git add only-feature1.txt 
MacBook-Pro:git-demo$ git commit -m "feat:add new feature."
[feature1 0b5887e] feat:add new feature.
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100755 only-feature1.txt
MacBook-Pro:git-demo$ git push
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Delta compression using up to 4 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 253 bytes | 253.00 KiB/s, done.
Total 2 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
To https://github.com/tanakaryo/git-demo.git
   5bee4f1..0b5887e  feature1 -> feature1

次にmasterブランチに切り替えます。マージはこの場合(競合が発生しない)はかなりシンプルです。

下記のコマンドを実行するだけでマージは完了します。

git merge <sub-branch-name>

ここでいうsub-branch-nameは吸収される側のブランチを指します。feature1→masterなのでfeature1になります。

吸収する側のブランチに切り替えマージを実行します。マージ後には先ほど作成したファイルが見えるようになります。

MacBook-Pro:git-demo$ git checkout master
Switched to branch 'master'
Your branch is up to date with 'origin/master'.
MacBook-Pro:git-demo$ git merge feature1
Merge made by the 'recursive' strategy.
 only-feature1.txt    | 0
 test-from-branch.txt | 0
 2 files changed, 0 insertions(+), 0 deletions(-)
 create mode 100755 only-feature1.txt
 create mode 100755 test-from-branch.txt
MacBook-Pro:git-demo$ ls
demo.txt		test-from-branch.txt	test-from-vscode.txt
only-feature1.txt	test-from-local.txt

・ローカルブランチを削除する

ローカルブランチを削除してみます。まず削除前の状態が下記

MacBook-Pro:git-demo$ git branch
* feature1
  master

次にブランチを切り替えます。現在のブランチを選択したままだと削除できません。

MacBook-Pro:git-demo$ git checkout master
Switched to branch 'master'
Your branch is ahead of 'origin/master' by 3 commits.
  (use "git push" to publish your local commits)
MacBook-Pro:git-demo$ git branch
  feature1
* master

ローカルブランチ削除コマンドを実行します。

git branch -d <branch-name>
MacBook-Pro:git-demo$ git branch -d feature1
warning: deleting branch 'feature1' that has been merged to
         'refs/remotes/origin/feature1', but not yet merged to HEAD.
Deleted branch feature1 (was eae0f1f).
MacBook-Pro:git-demo$ git branch
* master

・リモートブランチを削除する

次にリモートブランチを削除します。

git push origin --delete <branch-name>
MacBook-Pro:git-demo$ git push origin --delete feature1
To https://github.com/tanakaryo/git-demo.git
 - [deleted]         feature1

githubを確認するとfeature1ブランチがなくなっている事がわかります。

・リモートブランチを参照する

git remote show origin

・Appendix

参考文献はこちら

https://qiita.com/kony/items/f0fb18b04bd5337132e3

https://qiita.com/cotolier_risa/items/210db74e6496d4359be7

コメントを残す

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

*