リポジトリの検査

リポジトリの検査

git status

The git status command displays the state of the working directory and the staging area. It lets you see which changes have been staged, which haven’t, and which files aren’t being tracked by Git. Status output does not show you any information regarding the committed project history. For this, you need to use git log.

使用法

git status

ステージングされているファイル、ステージングされていないファイル、未追跡のファイルを一覧表示します。

ディスカッション

The git status command is a relatively straightforward command. It simply shows you what's been going on with git add and git commit. Status messages also include relevant instructions for staging/unstaging files. Sample output showing the three main categories of a git status call is included below:

# ブランチ master に対して
# コミット対象の変更:
# ("git reset HEAD <file>..." を使用してアンステージ)
#
#変更済み: hello.py
#
# 変更はコミット用にステージングされません:
# ("git add <file>..." を使用してコミットするものを更新します)
# (作業ディレクトリの変更を破棄するには "git checkout -- <file>..." を使用します)
#
#変更済み: main.py
#
#未追跡ファイル:
# (コミットされるものに含めるには "git add <file>..." を使用します)
#
#hello.pyc

無視するファイル

Untracked files typically fall into two categories. They're either files that have just been added to the project and haven't been committed yet, or they're compiled binaries like .pyc, .obj, .exe, etc. While it's definitely beneficial to include the former in the git status output, the latter can make it hard to see what’s actually going on in your repository.

For this reason, Git lets you completely ignore files by placing paths in a special file called .gitignore. Any files that you'd like to ignore should be included on a separate line, and the * symbol can be used as a wildcard. For example, adding the following to a .gitignore file in your project root will prevent compiled Python modules from appearing in git status:

*.pyc

変更をコミットする前にリポジトリの状態を確認するようにしましょう。そうすれば、誤って意図しないものをコミットすることを防ぐことができます。次の例は、スナップショットのステージングおよびコミットの前と後のリポジトリの状態を示しています。

# hello.py の編集
git status
# hello.py は "Changes not staged for commit" の下に一覧表示されます
git add hello.py
git status
# hello.py は "Changes to be committed" の下に一覧表示されます
git commit
git status
# コミットするものはありません (作業ディレクトリはクリーンです)

The first status output will show the file as unstaged. The git add action will be reflected in the second git status, and the final status output will tell you that there is nothing to commit—the working directory matches the most recent commit. Some Git commands (e.g., git merge) require the working directory to be clean so that you don't accidentally overwrite changes.

git log

The git log command displays committed snapshots. It lets you list the project history, filter it, and search for specific changes. While git status lets you inspect the working directory and the staging area, git log only operates on the committed history.

Git チュートリアル:git status と git log

Log output can be customized in several ways, from simply filtering commits to displaying them in a completely user-defined format. Some of the most common configurations of git log are presented below.

使用法

git log

Display the entire commit history using the default formatting. If the output takes up more than one screen, you can use Space to scroll and q to exit.

git log -n <limit>

Limit the number of commits by <limit>. For example, git log -n 3 will display only 3 commits.

git log --oneline

各コミットを 1 行にまとめます。これは、プロジェクト履歴のハイレベルの概要を取得するのに便利です。

git log --stat

Along with the ordinary git log information, include which files were altered and the relative number of lines that were added or deleted from each of them.

git log -p

各コミットを表すパッチを表示します。これは、各コミットの完全な差分を表示します。プロジェクト履歴で取得可能な最も詳細なビューです。

git log --author="<pattern>"

Search for commits by a particular author. The <pattern> argument can be a plain string or a regular expression.

git log --grep="<pattern>"

Search for commits with a commit message that matches <pattern>, which can be a plain string or a regular expression.

git log <since>..<until>

Show only commits that occur between <since> and <until>. Both arguments can be either a commit ID, a branch name, HEAD, or any other kind of revision reference.

git log <file>

指定されたファイルを含むコミットのみを表示します。これは、特定のファイルの履歴を確認する簡単な方法です。

git log --graph --decorate --oneline

考慮すべきいくつかの役立つオプションがあります。--graph フラグを指定すると、コミットメッセージの左側にテキストベースのコミットの図が描画されます。--decorate はブランチの名前または表示されるコミットのタグを追加します。--oneline は、一目で簡単にコミットを参照できるように単一行にコミット情報を表示します。

ディスカッション

The git log command is Git's basic tool for exploring a repository’s history. It’s what you use when you need to find a specific version of a project or figure out what changes will be introduced by merging in a feature branch.

commit 3157ee3718e180a9476bf2e5cab8e3f1e78a73b7
Author: John Smith

Most of this is pretty straightforward; however, the first line warrants some explanation. The 40-character string after commit is an SHA-1 checksum of the commit’s contents. This serves two purposes. First, it ensures the integrity of the commit—if it was ever corrupted, the commit would generate a different checksum. Second, it serves as a unique ID for the commit.

This ID can be used in commands like git log <since>..<until> to refer to specific commits. For instance, git log 3157e..5ab91 will display everything between the commits with ID's 3157e and 5ab91. Aside from checksums, branch names (discussed in the Branch Module) and the HEAD keyword are other common methods for referring to individual commits. HEAD always refers to the current commit, be it a branch or a specific commit.

The ~ character is useful for making relative references to the parent of a commit. For example, 3157e~1 refers to the commit before 3157e, and HEAD~3 is the great-grandparent of the current commit.

The idea behind all of these identification methods is to let you perform actions based on specific commits. The git log command is typically the starting point for these interactions, as it lets you find the commits you want to work with.

The Usage section provides many examples of git log, but keep in mind that several options can be combined into a single command:

git log --author="John Smith" -p hello.py

This will display a full diff of all the changes John Smith has made to the file hello.py.

The .. syntax is a very useful tool for comparing branches. The next example displays a brief overview of all the commits that are in some-feature that are not in master.

git log --oneline master..some-feature

Git を学習する準備はできていますか?

この対話式チュートリアルを利用しましょう。

今すぐ始める