KnowHow

技術的なメモを中心にまとめます。
検索にて調べることができます。

Gitの使い方(3 pushしないファイルを設定git ignore)

登録日 :2024/01/21 13:47
カテゴリ :Git

Gitのバージョン管理をしないものを設定します

Gitのバージョン管理が必要ないもの
  • 機密情報(ハッシュや秘密鍵、パスワードなど)のファイル
  • チーム開発で共有しているだけ情報(システムに影響ないもの)
    などが考えられます

管理しないファイルをGitの管理から外すものを指定する

.gitignoreファイルに指定する
(#で指定するとコメントアウトになる)

書き方は以下の通り
  • 指定したファイルを除外する場合、ファイル名を記載
    index.html
  • ルートディレクトリを指定する
    /root.html
  • ディレクトリ以下を除外する場合
    dir/
  • /以外の文字列にマッチ[]
    /
    /*.css

--- 実行例

$ ls
index.html

ステータスを確認する

$ git st
On branch master
Your branch is up to date with 'origin/master'.

nothing to commit, working tree clean

git pushしたくないsecretファイルを作成してみる

$ touch secret.txt
$ ls
index.html      secret.txt

ステータスを見ると、add前なのでワークツリーとの間での変更が表示される。

$ git st
On branch master
Your branch is up to date with 'origin/master'.

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        secret.txt

nothing added to commit but untracked files present (use "git add" to track)

こののままでは、secretファイルがpush対象と認識されており、リモートリポジトリにアップロードされてしまう。
secretファイルを管理対象から除外したいので、.gitignoreを作成する

$ vi .gitignore

再度、ステータスを確認すると、secretファイルが消えて、.gitignoreファイルが認識される。

$ git st
On branch master
Your branch is up to date with 'origin/master'.

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        .gitignore

nothing added to commit but untracked files present (use "git add" to track)

ステージにアップロードする。

$ git add .

コミットして、リモートリポジトリに更新をかける

$ git ci -m".gitignoreファイルを追加"
[master 24025e6] .gitignoreファイルを追加
 1 file changed, 1 insertion(+)
 create mode 100644 .gitignore

再度ステータスを確認する

$ git st
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
  (use "git push" to publish your local commits)

nothing to commit, working tree clean