LoginSignup
0
1

More than 1 year has passed since last update.

Git 小技集(2021年卯月)

Last updated at Posted at 2021-04-26

初めに

CentOS7系では、gitのバージョンが1.8系で古いため、エラーになるスクリプトがあることに最近気づいた @CinTAKE です。

gitは、コードの共同開発には欠かせない便利なツールですが、親切すぎる部分もあり、問題が発生したりする。その中から重大なものや、よく出くわすものを取り上げ、原因や対処法を解説する。

事例と対処

Windowsでの改行コード問題

前提

  • 発生環境:Windows
  • バージョン:git --version => git version 2.32.0.windows.1
  • パス:get-command git => C:\Users\stake\scoop\shims\git.ps1

現象

クローン、チェックアウトでコード類の改行コードがCRLFになってしまい。
そのままの状態でコミットされてしまうこともあり、bashのスクリプトなどが動かなくなる。

原因

WindowsのGITでは、デフォルトインストールで、改行コードの自動変換が設定されている。

# 設定を確認する;core.autocrlf => true ??
# 設定の確認:グルーバル
git config --list --global
# 設定の確認:ローカル(グローバルより優先となる)
git config --list --local

対処

まず、グローバル設定を変更し、ローカル設定がある場合は、そちらも変更する。
CRLFが必要な場合は、ローカルで「git config --local core.autocrlf false」とするか、VSCの設定を変更する。1

# 改行コードをコミット時にのみ CRLF=>LF に変換(それ以外の操作では無変換)
# グローバル
git config --global core.autocrlf input
# ローカル
git config core.autocrlf input

結果と予防

clone や checkout ではリポジトリのままで持ってくることができるようになった。Linux系での運用が中心の場合はこの設定がよいと思う。CIで改行コードチェッカーなどをかますのも防止に役立つかも。

Centos7でgitを2.xに更新

前提

  • 発生環境:CentOS7; cat /etc/redhat-release => CentOS Linux release 7.7.1908 (Core)
  • バージョン: git --version => git version 1.8.3.1
  • パス:type git => git is hashed (/usr/bin/git)

現象

git 2.x 前提の操作が実行できない

$ git tag -l --sort=-creatordate --format='%(creatordate:short):   %(refname:short)'
error: unknown option `sort=-creatordate'
usage: git tag [-a|-s|-u <key-id>] [-f] [-m <msg>|-F <file>] <tagname> [<head>]
   or: git tag -d <tagname>...
   or: git tag -l [-n[<num>]] [--contains <commit>] [--points-at <object>] 
                [<pattern>...]
   or: git tag -v <tagname>...

    -l, --list            list tag names
    -n[<n>]               print <n> lines of each tag message
    -d, --delete          delete tags
    -v, --verify          verify tags

Tag creation options
    -a, --annotate        annotated tag, needs a message
    -m, --message <message>
                          tag message
    -F, --file <file>     read message from file
    -s, --sign            annotated and GPG-signed tag
    --cleanup <mode>      how to strip spaces and #comments from message
    -u, --local-user <key id>
                          use another key to sign the tag
    -f, --force           replace the tag if exists
    --column[=<style>]    show tag list in columns

Tag listing options
    --contains <commit>   print only tags that contain the commit
    --points-at <object>  print only tags of the object

原因

Centos7の標準的なYumRepository(base,extra,updates,epel,,,)では、gitのバージョンが1.xとなっている。

対処

リポジトリをインストール
1.xを削除
2.xをインストール

# rootになる
sudo su -
#IUS リポジトリをインストール
yum -y install https://repo.ius.io/ius-release-el7.rpm
# git 1.x を削除
yum remove git
# git 2.xをインストール
yum -y install git224 --enablerepo=ius
# 確認
git --version
# IUS リポジトリを不活性化
yum-config-manager --disable ius
yum repolist ius -v|grep status

結果と予防

$ git tag -l --sort=-creatordate --format='%(creatordate:short):   %(refname:short)'
2020-11-24:   v_2.4.47
2020-11-06:   v_2.4.46
2020-10-29:   v_2.4.44
2020-10-28:   v_2.4.43
2020-09-30:   v_2.4.42
2020-09-15:   v_2.4.41
2020-09-09:   v_2.4.40
. . . . . 

AnsibleなどのProvisioningツールにて設定しておくとよい。

終りに

昔のMacOSはCRのみだったなーーー。CentOSは安定重視だなーーー。

親記事:WSL2とVSCで作るWindowsでのDocker内開発環境(2021年睦月)

テンプレート

## 事象のタイトル
### 前提
- 発生環境:
### 現象
### 原因
### 対処
### 結果と予防
0
1
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
0
1