# Git コマンドティップス

**URL:** https://forum.ficusonline.com/t/topic/269
**Category:** Linux
**Created:** [2019 年 1 月 13 日午前 11:36 UTC](https://forum.ficusonline.com/t/topic/269 "2019-01-13T11:36:35Z")
**Posts on this page:** 14
**Page:** 1

<div class="post-metadata">

### Author: ![tk-fuse](https://forum.ficusonline.com/user_avatar/forum.ficusonline.com/tk-fuse/32/255_2.png) [@tk-fuse](https://forum.ficusonline.com/u/tk-fuse)
#### Post date: [2019 年 1 月 13 日午前 11:36 UTC](https://forum.ficusonline.com/t/topic/269/1 "2019-01-13T11:36:35Z")

</div>

![Git-logo](https://forum.ficusonline.com/uploads/default/original/2X/e/ed82ec52db013ef6a749448b4b63f3be3faa4b50.png)

**ブランチクローン作成**

> <https://stackoverflow.com/questions/1911109/how-do-i-clone-a-specific-git-branch/4568323#4568323>

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

```

* * *

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

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

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

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

[https://github.com/settings/tokens](https://github.com/settings/tokens)

---

<div class="post-metadata">

### Author: ![tk-fuse](https://forum.ficusonline.com/user_avatar/forum.ficusonline.com/tk-fuse/32/255_2.png) [@tk-fuse](https://forum.ficusonline.com/u/tk-fuse)
#### Post date: [2019 年 1 月 20 日午前 1:47 UTC](https://forum.ficusonline.com/t/topic/269/2 "2019-01-20T01:47:02Z")

</div>

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

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

[https://git-scm.com/book/en/v2/Git-Basics-Working-with-Remotes](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](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

---

<div class="post-metadata">

### Author: ![tk-fuse](https://forum.ficusonline.com/user_avatar/forum.ficusonline.com/tk-fuse/32/255_2.png) [@tk-fuse](https://forum.ficusonline.com/u/tk-fuse)
#### Post date: [2019 年 9 月 15 日午後 11:09 UTC](https://forum.ficusonline.com/t/topic/269/3 "2019-09-15T23:09:31Z")

</div>

**Git-Branch**

> **[Git - Basic Branching and Merging](https://git-scm.com/book/en/v2/Git-Branching-Basic-Branching-and-Merging)**

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

```

This is shorthand for:

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

```

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

> <https://stackoverflow.com/questions/38564473/how-to-fork-a-branch-and-not-the-repository>

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

```

---

<div class="post-metadata">

### Author: ![tk-fuse](https://forum.ficusonline.com/user_avatar/forum.ficusonline.com/tk-fuse/32/255_2.png) [@tk-fuse](https://forum.ficusonline.com/u/tk-fuse)
#### Post date: [2019 年 12 月 4 日午前 2:46 UTC](https://forum.ficusonline.com/t/topic/269/4 "2019-12-04T02:46:31Z")

</div>

### git cloneのアップデート

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

**fetchとmergeを同時に実行**

`$git pull`

**fetchのみ実行**

`$git fetch`

---

<div class="post-metadata">

### Author: ![tk-fuse](https://forum.ficusonline.com/user_avatar/forum.ficusonline.com/tk-fuse/32/255_2.png) [@tk-fuse](https://forum.ficusonline.com/u/tk-fuse)
#### Post date: [2020 年 3 月 6 日午後 1:38 UTC](https://forum.ficusonline.com/t/topic/269/5 "2020-03-06T13:38:14Z")

</div>

### submodule update

> <https://stackoverflow.com/questions/5828324/update-git-submodule-to-latest-commit-on-origin>

**git submodule**

> **[Git - Submodules](https://git-scm.com/book/en/v2/Git-Tools-Submodules)**

> **[Git - git-submodule Documentation](https://git-scm.com/docs/git-submodule)**

---

<div class="post-metadata">

### Author: ![tk-fuse](https://forum.ficusonline.com/user_avatar/forum.ficusonline.com/tk-fuse/32/255_2.png) [@tk-fuse](https://forum.ficusonline.com/u/tk-fuse)
#### Post date: [2020 年 7 月 31 日午前 9:24 UTC](https://forum.ficusonline.com/t/topic/269/6 "2020-07-31T09:24:40Z")

</div>

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

> **[Git - git-clone Documentation](https://git-scm.com/docs/git-clone)**

```auto
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](https://jitsi.org/)をDockerコンテナで構築するため、以下最新の安定版をクローン

```auto
$ 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`指定

> <https://stackoverflow.com/questions/20280726/how-to-clone-a-specific-git-tag>

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

```

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

> <https://stackoverflow.com/questions/1911109/how-do-i-clone-a-specific-git-branch>

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

```

---

<div class="post-metadata">

### Author: ![tk-fuse](https://forum.ficusonline.com/user_avatar/forum.ficusonline.com/tk-fuse/32/255_2.png) [@tk-fuse](https://forum.ficusonline.com/u/tk-fuse)
#### Post date: [2020 年 12 月 22 日午後 1:43 UTC](https://forum.ficusonline.com/t/topic/269/7 "2020-12-22T13:43:57Z")

</div>

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

> **[How Git Partial Clone lets you fetch only the large file you need](https://about.gitlab.com/blog/partial-clone-for-massive-repositories/)**
>
> Work faster with this experimental Partial Clone feature for huge Git repositories, saving you time, bandwidth, and storage, one large file at a time.

**sparse-checkout**

> **[Bring your monorepo down to size with sparse-checkout](https://github.blog/open-source/git/bring-your-monorepo-down-to-size-with-sparse-checkout/)**
>
> Git 2.25.0 includes a new experimental git sparse-checkout command that makes the existing feature easier to use, along with some important performance benefits for large repositories.

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

> **[Highlights from Git 2.25](https://github.blog/open-source/git/highlights-from-git-2-25/)**
>
> Take a look at some of the new features in the latest Git release.

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

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

```

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

```auto
$ 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` により、部分クローンするフォルダを指定

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

```

---

<div class="post-metadata">

### Author: ![tk-fuse](https://forum.ficusonline.com/user_avatar/forum.ficusonline.com/tk-fuse/32/255_2.png) [@tk-fuse](https://forum.ficusonline.com/u/tk-fuse)
#### Post date: [2021 年 6 月 23 日午後 12:06 UTC](https://forum.ficusonline.com/t/topic/269/8 "2021-06-23T12:06:44Z")

</div>

# Adding an existing project to GitHub using the command line

> **[Adding locally hosted code to GitHub - GitHub Docs](https://docs.github.com/en/migrations/importing-source-code/using-the-command-line-to-import-source-code/adding-locally-hosted-code-to-github)**
>
> If your code is stored locally on your computer and is tracked by Git or not tracked by any version control system (VCS), you can import the code to GitHub using GitHub CLI or Git commands.

1. [Create a new repository](https://docs.github.com/en/articles/creating-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](https://forum.ficusonline.com/uploads/default/original/1X/40053e768f12cd3e891da02a4604ad1c4e158a86.png)
2. Open Terminal.
3. Change the current working directory to your local project.
4. Initialize the local directory as a Git repository.

```auto
$ git init -b main

```

1. Add the files in your new local repository. This stages them for the first commit.

```auto
$ 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.

```auto
$ 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](https://forum.ficusonline.com/uploads/default/original/1X/c70a9127e0dde5763c2d3e83f3d34bc1ac25298c.png)
2. In Terminal, [add the URL for the remote repository](https://docs.github.com/en/github/getting-started-with-github/managing-remote-repositories) where your local repository will be pushed.

```auto
$ git remote add origin <REMOTE_URL> 
# Sets the new remote
$ git remote -v
# Verifies the new remote URL

```

1. [Push the changes](https://docs.github.com/en/github/getting-started-with-github/pushing-commits-to-a-remote-repository) in your local repository to GitHub.

```auto
$ git push origin main
# Pushes the changes in your local repository up to the remote repository you specified as the origin

```

---

<div class="post-metadata">

### Author: ![tk-fuse](https://forum.ficusonline.com/user_avatar/forum.ficusonline.com/tk-fuse/32/255_2.png) [@tk-fuse](https://forum.ficusonline.com/u/tk-fuse)
#### Post date: [2022 年 4 月 9 日午前 1:29 UTC](https://forum.ficusonline.com/t/topic/269/9 "2022-04-09T01:29:55Z")

</div>

### 新規レポジトリ `prestashop1784` を `Github` サイトで作成後

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

```auto
$ 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履歴フォルダを削除し新規ブランチを作成

```auto
$ 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

```auto
$ 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フォルダーを削除し、イニシャライズすること。

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

```

> <https://stackoverflow.com/questions/28983842/remote-rejected-shallow-update-not-allowed-after-changing-git-remote-url>

---

<div class="post-metadata">

### Author: ![tk-fuse](https://forum.ficusonline.com/user_avatar/forum.ficusonline.com/tk-fuse/32/255_2.png) [@tk-fuse](https://forum.ficusonline.com/u/tk-fuse)
#### Post date: [2022 年 7 月 27 日午前 2:00 UTC](https://forum.ficusonline.com/t/topic/269/10 "2022-07-27T02:00:09Z")

</div>

## リポジトリのリネーム

> **[Renaming a repository - GitHub Docs](https://docs.github.com/en/repositories/creating-and-managing-repositories/renaming-a-repository)**
>
> You can rename a repository if you're either an organization owner or have admin permissions for the repository.

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

 ![Screenshot from 2022-07-27 10-56-57](https://forum.ficusonline.com/uploads/default/original/2X/2/2428964e92cf16fe6f39acf07f53fd90aec30893.png)

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

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

```

---

<div class="post-metadata">

### Author: ![tk-fuse](https://forum.ficusonline.com/user_avatar/forum.ficusonline.com/tk-fuse/32/255_2.png) [@tk-fuse](https://forum.ficusonline.com/u/tk-fuse)
#### Post date: [2022 年 7 月 27 日午前 4:59 UTC](https://forum.ficusonline.com/t/topic/269/11 "2022-07-27T04:59:46Z")

</div>

## タグからブランチを作成

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

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

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

**Ex) PrestaShop**

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

```auto
$ git clone git@github.com:capitalfuse/PrestaShop.git

```

タグ確認

```auto
$ 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

```

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

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

```auto
$ git checkout tags/1.7.8.4 -b v1784

```

➡ **リモートにも新規ブランチを登録**

```auto
$ 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の確認

```auto
$ git remote -v
origin	git@github.com:capitalfuse/PrestaShop.git (fetch)
origin	git@github.com:capitalfuse/PrestaShop.git (push)
upstream	git://github.com/PrestaShop/PrestaShop.git (fetch)
upstream	git://github.com/PrestaShop/PrestaShop.git (push)

```

リモートのブランチ確認

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

```

ローカルのブランチ確認

```auto
$ git branch -l
  develop
* v1784

```

ブランチ詳細

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

```

---

<div class="post-metadata">

### Author: ![tk-fuse](https://forum.ficusonline.com/user_avatar/forum.ficusonline.com/tk-fuse/32/255_2.png) [@tk-fuse](https://forum.ficusonline.com/u/tk-fuse)
#### Post date: [2022 年 11 月 21 日午前 2:17 UTC](https://forum.ficusonline.com/t/topic/269/12 "2022-11-21T02:17:22Z")

</div>

# git stash

**Git - git-stash Documentation**

> **[Git - git-stash Documentation](https://git-scm.com/docs/git-stash)**

**Git - Stashing and Cleaning**

> **[Git - Stashing and Cleaning](https://git-scm.com/book/en/v2/Git-Tools-Stashing-and-Cleaning)**

**Git Workflow**  
[https://twitter.com/NikkiSiapno/status/1593882400983072769](https://twitter.com/NikkiSiapno/status/1593882400983072769)

 ![git_flow](https://forum.ficusonline.com/uploads/default/original/2X/9/97593e3c71f17724af247913493a691ac408318d.jpeg)

---

<div class="post-metadata">

### Author: ![tk-fuse](https://forum.ficusonline.com/user_avatar/forum.ficusonline.com/tk-fuse/32/255_2.png) [@tk-fuse](https://forum.ficusonline.com/u/tk-fuse)
#### Post date: [2023 年 4 月 11 日午後 12:37 UTC](https://forum.ficusonline.com/t/topic/269/13 "2023-04-11T12:37:05Z")

</div>

# git log, git reset

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

> <https://stackoverflow.com/questions/70043929/push-error-from-large-files-even-after-adding-it-to-gitignore-and-unstaging>

```auto
$ 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

```

```auto
$ git reset --soft DDDD

```

```auto
$ git add .
$ git commit -m "my_commit"
$ git push ssh://~

```

---

<div class="post-metadata">

### Author: ![tk-fuse](https://forum.ficusonline.com/user_avatar/forum.ficusonline.com/tk-fuse/32/255_2.png) [@tk-fuse](https://forum.ficusonline.com/u/tk-fuse)
#### Post date: [2026 年 1 月 5 日午前 1:44 UTC](https://forum.ficusonline.com/t/topic/269/14 "2026-01-05T01:44:18Z")

</div>

## lazygit

gitの操作を視覚的に把握・実行できる便利はツールです。

> **[GitHub - jesseduffield/lazygit: simple terminal UI for git commands](https://github.com/jesseduffield/lazygit)**
>
> simple terminal UI for git commands

Ubuntu24.04ではバイナリからインストールする必要があります。現時点での最新版はv0.58です。

> **[Releases · jesseduffield/lazygit](https://github.com/jesseduffield/lazygit/releases)**
>
> simple terminal UI for git commands. Contribute to jesseduffield/lazygit development by creating an account on GitHub.

ダウンロード  
[https://github.com/jesseduffield/lazygit/releases/download/v0.58.0/lazygit\_0.58.0\_linux\_x86\_64.tar.gz](https://github.com/jesseduffield/lazygit/releases/download/v0.58.0/lazygit_0.58.0_linux_x86_64.tar.gz)

解凍してインストール

```auto
$ cd lazygit_0.58.0_linux_x86_64
$ sudo install lazygit /usr/local/bin

```

コマンドオプション

```shell
$ lazygit -h
  Usage:
    lazygit [git-arg]

  Positional Variables: 
    git-arg Panel to focus upon opening lazygit. Accepted values (based on git terminology): status, branch, log, stash. Ignored if --filter arg is passed.
  Flags: 
    -h --help Displays help with available flag, subcommand, and positional value parameters.
    -p --path Path of git repo. (equivalent to --work-tree=<path> --git-dir=<path>/.git/)
    -f --filter Path to filter on in `git log -- <path>`. When in filter mode, the commits, reflog, and stash are filtered based on the given path, and some operations are restricted
    -v --version Print the current version
    -d --debug Run in debug mode with logging (see --logs flag below). Use the LOG_LEVEL env var to set the log level (debug/info/warn/error)
    -l --logs Tail lazygit logs (intended to be used when `lazygit --debug` is called in a separate terminal tab)
       --profile Start the profiler and serve it on http port 6060. See CONTRIBUTING.md for more info.
    -c --config Print the default config
    -cd --print-config-dir Print the config directory
    -ucd --use-config-dir override default config directory with provided directory
    -w --work-tree equivalent of the --work-tree git argument
    -g --git-dir equivalent of the --git-dir git argument
    -ucf --use-config-file Comma separated list to custom config file(s)
    -sm --screen-mode The initial screen-mode, which determines the size of the focused panel. Valid options: 'normal' (default), 'half', 'full'

```
