Git コマンドティップス

Git-logo

ブランチクローン作成

$ git clone --single-branch --branch **branchname** https://github.com/user/repo.git

git clone によるダウンロード : ssh または https による相違点

sshによるダウンロードを推奨

  • ssh : ssh公開キーをgithubアカウントページへ登録することでパスワードなしにリモートレポジトリへのアクセスが可能

  • https : リモートへのアクセスにパーソナルアクセストークンが必要だが、作成の手間とGitHubでトークンが保存されないため、個人責任でどこかへ保存する必要がある。

オリジナルからクローンをローカルに作成、クローン内のファイル修正後、フォークしたリモートに修正内容をコミットする場合

  • オリジナルクローン作成 git clone
  • クローンファイル修正(ローカル環境で)
  • git addコマンドまたはgit commitコマンドにより修正内容フィックス
  • オリジナルからフォーク作成
  • リモート追加 git remote add
  • 追加したリモートを指定してgit push

https://git-scm.com/book/en/v2/Git-Basics-Working-with-Remotes

The git remote add command takes two arguments:

  • A remote name, for example, origin_name
  • A remote URL, for example, https://github.com/user/repo.git

For example:

オリジナルからフォークしたリモートの追加

$ git remote add origin_name https://github.com/user/repo.git

追加したリモートの確認

$ git remote -v

origin_name https://github.com/user/repo.git (fetch) 
origin_name https://github.com/user/repo.git (push)

ローカル環境でファイル修正後、

修正したファイルのステージング

$ git add .

ローカルレポジトリに修正内容反映

$ git commit

ローカルレポジトリの内容をリモートレポジトリに反映させる

$ git push origin_name

Git-Branch

https://git-scm.com/book/en/v2/Git-Branching-Basic-Branching-and-Merging

$ git checkout -b iss53
Switched to a new branch "iss53" Branch iss53を作成し移動

This is shorthand for:

$ git branch iss53 --->Branchの作成
$ git checkout iss53 --->Branch iss53へ移動

デフォルトのレポジトリをクローン後、ブランチレポにチェックアウト
このブランチレポから新規ブランチを作成(チェックアウト)

$ git clone http://repository
$ git checkout branchname
$ git checkout -b new-branch-name

git cloneのアップデート

本家リモートからそのままクローンしたレポジトリをアップデートする場合

fetchとmergeを同時に実行

$git pull

fetchのみ実行

$git fetch

submodule update

git submodule

https://git-scm.com/book/en/v2/Git-Tools-Submodules
https://git-scm.com/docs/git-submodule

branchまたはtagによるGitレポジトリクローン

https://git-scm.com/docs/git-clone

git clone [--template=<template_directory>]
	  [-l] [-s] [--no-hardlinks] [-q] [-n] [--bare] [--mirror]
	  [-o <name>] [-b <name>] [-u <upload-pack>] [--reference <repository>]
	  [--dissociate] [--separate-git-dir <git dir>]
	  [--depth <depth>] [--[no-]single-branch] [--no-tags]
	  [--recurse-submodules[=<pathspec>]] [--[no-]shallow-submodules]
	  [--[no-]remote-submodules] [--jobs <n>] [--sparse]
	  [--filter=<filter>] [--] <repository>
	  [<directory>]

オープンソースによる会議システムJetsiをDockerコンテナで構築するため、以下最新の安定版をクローン

$ git clone -b stable-4857 --single-branch https://github.com/jitsi/docker-jitsi-meet.git
Cloning into 'docker-jitsi-meet'...
remote: Enumerating objects: 6, done.
remote: Counting objects: 100% (6/6), done.
remote: Compressing objects: 100% (6/6), done.
remote: Total 1829 (delta 1), reused 4 (delta 0), pack-reused 1823
Receiving objects: 100% (1829/1829), 750.95 KiB | 941.00 KiB/s, done.
Resolving deltas: 100% (888/888), done.
Note: checking out 'd32d74fa138c828b3de34fe7b0fb35c7fc3e0dc8'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:

  git checkout -b <new-branch-name>

tagによるレポジトリも同様に--branch指定

$ git clone --depth 1 --branch <tag_name> <repo_url>

--depth 1 はオプションで、改訂履歴の深さを表す。ある時点でのtag付レポのみをクローンする場合はdepth 1を指定。

$ git clone --single-branch --branch <branchname> <remote-repo>

レポジトリ内の特定フォルダ(ファイル)のクローン(ダウンロード)

sparse-checkout

sparseコマンドによる部分クローン機能は、以下2.25パージョンから導入。

Ubuntuで最新版を導入するため、以下のPPAを追加しバージョンを最新版に更新。

$ sudo add-apt-repository ppa:git-core/ppa 
$ sudo apt update
$ sudo apt install git

Linux Realtek-Wifiドライバの部分クローン

$ mkdir linux_driver
$ cd linux_driver
$ git clone --filter=blob:none --no-checkout --depth 3 https://github.com/torvalds/linux.git
$ cd linux
$ git sparse-checkout init --cone

git sparse-checkout により、部分クローンするフォルダを指定

$ git sparse-checkout add drivers/net/wireless/realtek/rtlwifi
$ git checkout

Adding an existing project to GitHub using the command line

  1. Create a new repository on GitHub. To avoid errors, do not initialize the new repository with README, license, or gitignore files. You can add these files after your project has been pushed to GitHub.Create New Repository drop-down
  2. Open Terminal.
  3. Change the current working directory to your local project.
  4. Initialize the local directory as a Git repository.
$ git init -b main
  1. Add the files in your new local repository. This stages them for the first commit.
$ git add .
# Adds the files in the local repository and stages them for commit. To unstage a file, use 'git reset HEAD YOUR-FILE'.
  1. Commit the files that you’ve staged in your local repository.
$ git commit -m "First commit"
# Commits the tracked changes and prepares them to be pushed to a remote repository. To remove this commit and modify the file, use 'git reset --soft HEAD~1' and commit and add the file again.
  1. At the top of your GitHub repository’s Quick Setup page, click to copy the remote repository URL.Copy remote repository URL field
  2. In Terminal, add the URL for the remote repository where your local repository will be pushed.
$ git remote add origin  <REMOTE_URL> 
# Sets the new remote
$ git remote -v
# Verifies the new remote URL
  1. Push the changes in your local repository to GitHub.
$ git push origin main
# Pushes the changes in your local repository up to the remote repository you specified as the origin

新規レポジトリ prestashop1784Github サイトで作成後

プロジェクトを一から作成する場合

$ echo "# prestashop1784" >> README.md
$ git init
$ git add README.md
$ git commit -m "first commit"
$ git branch -M main
$ git remote add origin https://github.com/capitalfuse/prestashop1784.git
$ git push -u origin main

git clone コマンドにより既にフォークしたプロジェクトを push する場合

特定バージョンのレポジトリをダウンロード、git履歴フォルダを削除し新規ブランチを作成

$ git clone --depth 1 --branch 1.7.8.4 https://github.com/PrestaShop/PrestaShop.git 
$ cd PrestaShop
$ rm -r .git
$ git init
$ git branch -M main
$ git add .

pushする新規リモートurl : originを追加しpush

$ git remote add origin https://github.com/capitalfuse/prestashop1784.git
$ git push --set-upstream origin main

GitHubユーザ認証には、パスワードではなく Personal Access Token が必要です。Settings ---> Developer Settings ---> Personal Access Tokens から新規に作成するか既存のTokenから Regenerate Token して下さい。

メモ

depth=1を指定しているため、gitの履歴ファイルとの整合性が取れないことによるエラー。.gitフォルダーを削除し、イニシャライズすること。

 ! [remote rejected] main -> main (shallow update not allowed)
error: failed to push some refs to 'https://github.com/capitalfuse/prestashop1784.git'

リポジトリのリネーム

Githubのリポジトリの Settings メニューから変更。

リネーム後、リモートURLも変更する必要があります。

$ git remote set-url origin https://github.com/capitalfuse/prestashop_state_multi_lang

タグからブランチを作成

:arrow_right: Githubサイトでフォークしたレポジトリをローカルへクローン

:arrow_right: 特定のタグを指定して新規ブランチを作成しチェックアウト

:arrow_right: リモートにも新規ブランチを登録

Ex) PrestaShop

:arrow_right: Githubサイトでフォークしたレポジトリをローカルへクローン

$ git clone [email protected]:capitalfuse/PrestaShop.git

タグ確認

$ git tag
..........
..........
1.7.7.3
1.7.7.4
1.7.7.5
1.7.7.6
1.7.7.7
1.7.7.8
1.7.8.0
1.7.8.0-beta.1
1.7.8.0-rc.1
1.7.8.1
1.7.8.2
1.7.8.3
1.7.8.4
1.7.8.5
1.7.8.6
1.7.8.7

:arrow_right: 特定のタグを指定して新規ブランチを作成しチェックアウト

上記リストから特定のタグを指定して新規ブランチ v1784 作成(新規ブランチへチェックアウト)

$ git checkout tags/1.7.8.4 -b v1784

:arrow_right: リモートにも新規ブランチを登録

$ git push --set-upstream origin v1784
Total 0 (delta 0), reused 0 (delta 0)
remote: 
remote: Create a pull request for 'v1784' on GitHub by visiting:
remote:      https://github.com/capitalfuse/PrestaShop/pull/new/v1784
remote: 
To github.com:capitalfuse/PrestaShop.git
 * [new branch]            v1784 -> v1784
Branch 'v1784' set up to track remote branch 'v1784' from 'origin'.

git コマンド補足

リモートURLの確認

$ git remote -v
origin	[email protected]:capitalfuse/PrestaShop.git (fetch)
origin	[email protected]:capitalfuse/PrestaShop.git (push)
upstream	git://github.com/PrestaShop/PrestaShop.git (fetch)
upstream	git://github.com/PrestaShop/PrestaShop.git (push)

リモートのブランチ確認

$ git branch -r
  origin/1.7.7.x
  origin/1.7.8.x
  origin/8.0.x
  origin/HEAD -> origin/develop
  origin/develop

ローカルのブランチ確認

$ git branch -l
  develop
* v1784

ブランチ詳細

$ git branch -v
  develop 28e7e2d510 Merge pull request #29141 from abramofranchetti/fix-typo-comment-Reference
* v1784   869e90bf67 // Changelog 1.7.8.4

git stash

Git - git-stash Documentation
https://git-scm.com/docs/git-stash

Git - Stashing and Cleaning
https://git-scm.com/book/en/v2/Git-Tools-Stashing-and-Cleaning

Git Workflow

git log, git reset

git pullコマンドでアップロードファイルサイスが100MBを超えていた場合エラーとなります。その対処法。

$ git log
commit AAAA (HEAD -> main)
Author: koyamashinji 
Date:   Sat Nov 20 16:49:08 2021 +0900

    backup20211120

commit BBBB
Author: koyamashinji 
Date:   Sat Nov 20 15:32:23 2021 +0900

    backup20211120

commit CCCC
Author: koyamashinji 
Date:   Sat Nov 20 15:24:47 2021 +0900

    backup20211120

commit DDDD (origin/main, master)
Author: koyamashinji 
Date:   Sun Nov 14 18:15:27 2021 +0900

    initial backup
$ git reset --soft DDDD
$ git add .
$ git commit -m "my_commit"
$ git push ssh://~