Git - Review Changes
Git - Reviewing Changes and History
Section titled “Git - Reviewing Changes and History”Understanding the history of your project and the changes made over time is crucial. Git provides several commands to inspect commits, compare versions, and review modifications.
Viewing Commit History with git log
Section titled “Viewing Commit History with git log”The git log command displays a chronological list of commits for the current branch. Each entry typically shows the commit SHA-1 hash, author, date, and commit message.
[jerry@dev-machine project]$ git logThis will produce output similar to:
commit d1e19d316224cddc437e3ed34ec3c931ad803958 (HEAD -> main, origin/main)Author: Jerry Mouse <jerry@example.com>Date: Wed Sep 11 08:15:30 2024 +0000
Changed return type of my_strlen to size_t
commit cbe1249b140dad24b2c35b15cc7e26a6f02d2277Author: Jerry Mouse <jerry@example.com>Date: Wed Sep 11 08:05:26 2024 +0000
Implemented my_strlen function
commit 19ae20683fc460db7d127cf201a1429523b0e319Author: Tom Cat <tom@example.com>Date: Wed Sep 11 07:32:56 2024 +0000
Initial commitgit log has many useful options for customizing its output:
--oneline: Shows each commit on a single line (SHA-1 prefix and message).
[jerry@dev-machine project]$ git log --onelined1e19d3 (HEAD -> main, origin/main) Changed return type of my_strlen to size_tcbe1249 Implemented my_strlen function19ae206 Initial commit-n <count>or-<count>: Limits the number of commits shown (e.g.,git log -2).--graph: Displays an ASCII art graph of the branch and merge history.--decorate: Shows branch and tag names next to commits.--author="<pattern>": Shows commits by a specific author.--grep="<pattern>": Shows commits whose message matches the pattern.--since="<date>"or--until="<date>": Filters commits by date.-- <file_path>: Shows commits that affected a specific file or path.
Viewing Commit Details with git show
Section titled “Viewing Commit Details with git show”To see the details of a specific commit, including its metadata and the changes (diff) it introduced, use git show <commit-SHA>.
[jerry@dev-machine project]$ git show cbe1249bThis will output:
commit cbe1249b140dad24b2c35b15cc7e26a6f02d2277Author: Jerry Mouse <jerry@example.com>Date: Wed Sep 11 08:05:26 2024 +0000
Implemented my_strlen function
diff --git a/string_utils.c b/string_utils.cnew file mode 100644index 0000000..187afb9--- /dev/null+++ b/string_utils.c@@ -0,0 +1,12 @@+#include <stdio.h>++int my_strlen(const char *s)+{+ const char *p = s;+ while (*p)+ ++p;+ return (p - s);+}+// ... more of the file if it's new ...+If you omit <commit-SHA>, git show displays the details of the latest commit (HEAD).
Comparing Changes with git diff
Section titled “Comparing Changes with git diff”git diff is used to show changes between commits, between commits and the working tree, or between the staging area and the working tree, etc. Lines starting with + indicate additions, and lines starting with - indicate removals.
- Changes in the working directory not yet staged:
[jerry@dev-machine project]$ git diffThis compares your working directory files with the staging area.
- Changes staged but not yet committed:
[jerry@dev-machine project]$ git diff --staged# or git diff --cachedThis compares the staging area with the last commit (HEAD).
- All changes (staged and unstaged) since the last commit:
[jerry@dev-machine project]$ git diff HEAD- Changes between two commits:
[jerry@dev-machine project]$ git diff <commit-SHA1> <commit-SHA2>For example, to see changes between commit cbe1249b and d1e19d31:
[jerry@dev-machine project]$ git diff cbe1249b d1e19d31This might output:
diff --git a/string_utils.c b/string_utils.cindex 187afb9..7da2992 100644--- a/string_utils.c+++ b/string_utils.c@@ -1,11 +1,12 @@ #include <stdio.h>
-int my_strlen(const char *s)+size_t my_strlen(const char *s) // Changed int to size_t { const char *p = s; while (*p) ++p; return (p - s); }-+// Example usage for testing int main(void) { const char *test_strings[] = {"hello", "world", ""}; for (int i = 0; i < 3; ++i) {- printf("Length of '%s' = %d\n", test_strings[i], my_strlen(test_strings[i]));+ printf("Length of '%s' = %zu\n", test_strings[i], my_strlen(test_strings[i])); // Changed %d to %zu } return 0; }Reviewing changes and history regularly helps in understanding project evolution, debugging issues, and collaborating effectively.