TOP ▲ itcore TOPTIPSgit_local.php  タグ:centos7 git 世代管理 バックアップ

gitのローカルリポジトリを使ってコンテンツの世代バックアップを行う。 | itcore 2019年

概要 運用 設定 管理対象からはずす 新しく作成したファイルの登録 ファイルの修正とコミット 変更履歴とdiff gitのネスト 指定したファイルのみコミットする

概要

頻繁に更新するコンテンツやプログラムソースをgitで管理すると、
バックアップや更新履歴を見たりするのに便利です。

運用

常に git status で状況を確認して、こまめにコミットしましょう。

設定

# yum -y install git
管理したいコンテンツのディレクトリへ移動します。
# cd /var/www/www
ローカルリポジトリの作成
# git init
Initialized empty Git repository in /var/www/www/.git/
.gitという隠しディレクトリが作られる。(ここに各種ファイルが作られる)
# ls -a
. .. .bash_history .cache .git .viminfo cgi-bin data htdocs log
管理者情報を登録する。
# git config --global user.name "Your Name"
# git config --global user.email you@example.com
ディレクトリ配下のすべてのファイルをgitに登録する。(ファイルが多い場合は少し時間がかかる)
# git add .
最初のコミット
# git commit -m 'git start'
 create mode 100644 ... たくさん出る
状態確認
[root@www www.itcore.jp]# git status
# On branch master
# Changes not staged for commit:
# (use "git add <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# modified: log/access.log
#
no changes added to commit (use "git add" and/or "git commit -a")

管理対象からはずす

# vi .gitignore
/log/
*.tmp
# git rm --cached -r log
※ --cached 実体は削除せずに管理対象からはずすだけ
# git add .gitignore
# git commit -m 'add .gitignore'
変更がない状態
[root@www www.itcore.jp]# git status
# On branch master
nothing to commit, working directory clean

新しく作成したファイルの登録

[root@www www.itcore.jp]# git status
# On branch master
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# htdocs/tips/git_local.php
nothing added to commit but untracked files present (use "git add" to track)
# git add htdocs/tips/git_local.php
# git commit -m 'add htdocs/tips/git_local.php'
[master c2f8983] add htdocs/tips/git_local.php
 1 file changed, 54 insertions(+)
 create mode 100644 htdocs/tips/git_local.php

ファイルの修正とコミット

[root@www www.itcore.jp]# git status
# On branch master
# Changes not staged for commit:
# (use "git add <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# modified: htdocs/tips/git_local.php
#
no changes added to commit (use "git add" and/or "git commit -a")
# git add htdocs/tips/git_local.php
# git commit -m 'modify git_local.php'
[master 412ff64] modify git_local.php
 1 file changed, 13 insertions(+)

変更履歴とdiff

# git log --pretty=oneline
412ff64d982bde702bda40f96e1a80f77557b20e modify git_local.php
c2f898359a758750a12fb5bea88d361dc66e1a57 add htdocs/tips/git_local.php
9ac1c6753d53c10fbe726866f42c222b297c2827 _index.txt
c7ab6f835ec8cf274338ce7e40609d2925da0cf6 rm /log/
88d067bffa9a1715583409e6358d104676235477 log/access.log
c6bf15738926c09c8c973081e19c5f7954e3072f add .gitignore
be3d7d5771b28639d05b051ae2135411069a3229 git start
現在のファイルとの比較
# git diff
qで終了
変更行のみ表示
# git diff -U0
コミット同士の比較
# git diff c2f898359a758750a12fb5bea88d361dc66e1a57 412ff64d982bde702bda40f96e1a80f77557b20e

ファイルの削除と履歴表示

# /bin/rm -r htdocs/rireki/
# git add --all
※削除したファイルも含める
# git commit -m '/bin/rm -r htdocs/rireki/'

# git log --diff-filter=D --summary
※削除したファイルのリスト
delete mode 100644 htdocs/rireki/.htaccess_20170406
# git log --pretty=oneline -- htdocs/rireki/.htaccess_20170406
※指定したファイルの変更履歴
9dd79cddfa6b45618e8cf450c8380518bbffd586 /bin/rm -r htdocs/rireki/
be3d7d5771b28639d05b051ae2135411069a3229 git start
# git show be3d7d5771b28639d05b051ae2135411069a3229:htdocs/rireki/.htaccess_20170406
※指定したコミットのファイルの中身を表示

gitのネスト

[root@erp2 erp2.itcore.jp]# git status
# On branch master
# Changed but not updated:
# (use "git add <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
# (commit or discard the untracked or modified content in submodules)
#
# modified: htdocs/koganephp_git/koganephp (modified content, untracked content)
#
[root@erp2 erp2.itcore.jp]# cd htdocs/koganephp_git/koganephp
[root@erp2 erp2.itcore.jp]# git diff
[root@erp2 erp2.itcore.jp]# git status
[root@erp2 erp2.itcore.jp]# git add .
[root@erp2 erp2.itcore.jp]# git commit -m 'xxx'
[root@erp2 erp2.itcore.jp]# git status
[root@erp2 erp2.itcore.jp]# cd ../../..
[root@erp2 erp2.itcore.jp]# git status
[root@erp2 erp2.itcore.jp]# git add .
[root@erp2 erp2.itcore.jp]# git commit -m 'htdocs/koganephp_git/koganephp'
[root@erp2 erp2.itcore.jp]# git status
# On branch master
nothing to commit (working directory clean)
<

指定したファイルのみコミットする

[root@www www.itcore.jp]# git status
# modified: data/otoiawase/201904.csv
# modified: htdocs/tips/git_local.php
[root@www www.itcore.jp]# git diff -- data/otoiawase/201904.csv
[root@www www.itcore.jp]# git add data/otoiawase/201904.csv
[root@www www.itcore.jp]# git commit -m 'otoiawase'
[root@www www.itcore.jp]# git status
# modified: htdocs/tips/git_local.php