ホームページ >開発ツール >Git >git現在のコミットを表示する方法

git現在のコミットを表示する方法

Emily Anne Brown
Emily Anne Brownオリジナル
2025-03-06 13:40:15791ブラウズ

This article details methods to view Git commit information. It explores commands like git log, git rev-parse HEAD, and git show to display commit hashes, author, date, message, and changes. Various formatting options are also presented for customi

git現在のコミットを表示する方法

Viewing the Current Commit in Git

This question refers to how to see the commit information of the HEAD commit, which is the latest commit on your current branch. There are several ways to achieve this. The simplest is using git log with the --oneline flag for a concise view, or without any flags for a more detailed view.

<code class="bash">git log --oneline
# or
git log</code>

git log --oneline will show a short hash and the commit message for each commit, making it easy to identify the current commit at the top of the output. git log without any flags provides a more detailed view, including author, date, and a full commit message.

Seeing the Commit Hash of Your Current Branch

The commit hash uniquely identifies a specific commit in your Git repository. To see the commit hash of the current branch's HEAD (latest) commit, you can use several commands. The simplest and most direct is:

<code class="bash">git rev-parse HEAD</code>

This command will output the full 40-character SHA-1 hash of the current commit. Alternatively, you can also use git log --pretty=format:"%H" which will output only the commit hash. This is useful if you want to extract the hash for scripting purposes. Finally, a shortened hash can be obtained (though not recommended for absolute certainty) using git log --oneline as mentioned above.

Information Provided by git show

The git show command is a powerful tool that displays information about a commit. When used without arguments, it displays information about the current commit (HEAD). The information includes:

  • Commit Hash: The unique identifier for the commit.
  • Author: The name and email address of the person who made the commit.
  • Date: The timestamp of when the commit was made.
  • Commit Message: The description of the changes made in the commit.
  • Changes Made: A diff showing the changes introduced by the commit (added, modified, and deleted files and lines). This is arguably the most useful part of git show, as it visually represents the changes.

For example, git show will output something similar to this:

<code>commit <commit-hash>
Author: Your Name <your.email@example.com>
Date:   Wed Sep 27 14:30:00 2023 -0700

    Commit message here

diff --git a/file1.txt b/file1.txt
index <hash>..<hash> 100644
--- a/file1.txt
+++ b/file1.txt
@@ -1,2 +1,3 @@
 This is a line.
+This is a new line.
 This is another line.</code>

Finding the Author and Date of the Current Commit

To find the author and date of the current commit, several Git commands can be used. The simplest is git show, as explained above. This command provides both pieces of information in its output.

Alternatively, you can use git log with appropriate formatting options. For example:

<code class="bash">git log -1 --pretty=format:"Author: %an%nDate: %ad" --date=short</code>

This command displays only the first commit (using -1), formats the output to show only the author name (%an) and date (%ad with --date=short for a concise date format), and separates them with a newline character (%n). The output will be similar to:

<code>Author: Your Name
Date: 2023-10-27</code>

These commands provide various ways to access the crucial information about your current commit, allowing for efficient version control management.

以上がgit現在のコミットを表示する方法の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。