Skip to content

Latest commit

Β 

History

History
465 lines (302 loc) Β· 10.2 KB

README.md

File metadata and controls

465 lines (302 loc) Β· 10.2 KB

Chapter 3: Browsing History in Git

In this chapter, we explore Git's powerful features for navigating and understanding the history of your project. Learn how to view, filter, and format commit logs, work with aliases for efficiency, inspect changes in commits, and more. This chapter aims to provide you with comprehensive tools for effective version control and collaboration.

πŸ“š Learning Objectives

🎯 Learning Objectives

In this chapter, you'll learn the intricacies of navigating and manipulating your project's history in Git through a series of practical tasks. Follow these steps to gain hands-on experience with reviewing past changes, managing versions, and ensuring efficient project tracking.

Learn how to use git log and other commands to review the commit history, providing insights into the evolution of your project.

Discover how to filter the commit log based on criteria like author, date, or keywords to find specific changes or contributions.

Explore customizing the appearance of the commit history using formatting options for better readability and information extraction.

βš™οΈ Creating Aliases

Understand how to create shorthand commands, or aliases, for complex Git commands to streamline your workflow.

πŸ‘οΈ Viewing a Commit

Learn the details of inspecting individual commits, including the changes made and metadata associated with each commit.

Master the technique of comparing changes between different commits or branches to understand the evolution of specific parts of your project.

πŸ•°οΈ Checking Out a Commit

Discover how to navigate your project's history by checking out specific commits, allowing you to review or build previous states of your project.

Learn to use git bisect to perform a binary search through your project's history to identify the commit that introduced a bug.

Understand how to summarize git log output with git shortlog to see contributions by each author, aiding in project management and collaboration.

Discover the process for restoring files that were deleted in previous commits, effectively recovering lost work.

πŸ”Ž Blaming

Learn to use git blame to trace changes in a file back to specific commits, helping identify who made certain changes and why.

🏷️ Tagging

Master the practice of tagging specific commits in your history, typically used for marking release points like v1.0, v2.0, etc.

πŸ“ Tasks

Viewing the history

Summary of all commits

 git log

Summary of all commits in one line

git log --oneline

List files changed in a commit

git log --oneline --stat

Detailed information about each commit

git log --stat

See actual changes in each commit

git log --oneline --patch

Filtering the history

See last three commits

git log --oneline -3

Filter commits by author

git log --oneline --author="Hossein"

Filter commits by date (before or after a specific date)

git log --oneline --before="2020-08-17" or git log --oneline --after="2020-08-17"

Filter commits by relative date (e.g., yesterday, two weeks ago)

git log --oneline --after="yesterday" 
or 
git log --oneline --after="two weeks ago"

Filter commits by submit message (commit message contains specific text)

git log --oneline --grep="GUI"

Filter commits by changes in a specific function (add or remove)

git log --oneline -S"OBJECTIVES"

See actual changes in a specific commit

git log --oneline --patch -S"OBJECTIVES"

Filter commit history by a range of commits

git log --oneline fb034ss..edb3595

Find all commits that modified a specific file

git log --oneline toc.txt
or 
git log --oneline -- toc.txt
πŸ’‘ The double hyphen (`--`) is typically used to separate the Git command options from the filenames when there is a possibility of ambiguity.

See actual changes in a specific file

git log --oneline --patch toc.txt

Formatting the Log Output

Display author name in a specific format

git log --pretty=format:"hello %an"

See author name and full hash for each commit

git log --pretty=format:"%an committed %H"

See author name and abbreviated hash for each commit.

git log --pretty=format:"%an committed %h"

See author name, abbreviated hash, and commit date for each commit

git log --pretty=format:"%an committed %h on %cd"

Change the color of the author name to green

git log --pretty=format:"%Cgreen%an committed %h on %cd"

Change the color of the author name to green and reset to default color

git log --pretty=format:"%Cgreen%an%Creset committed %h on %cd"

Creating Aliases

Add an alias to see the log

git config --global alias.lg "log --pretty=format:'%an committed %h'"

View the Git configuration file

git config --global -e

Add an alias to unstage files

git config --global alias.unstage "restore --staged ."

Viewing a Commit

View the changes in a commit, two steps back from HEAD

git show HEAD~2

View the changes in a specific file from a commit two steps back from HEAD

git show HEAD~2:sections/creating-snapshots/staging-changes.txt

List all files modified, deleted, or added in a commit two steps back from HEAD

git show HEAD~2 --name-only

Show the status (modified, deleted, or added) of each file in a commit two steps back from HEAD

git show HEAD~2 --name-status

Viewing changes across Commits

Compare the changes between the commit two steps back from HEAD and the most recent commit

git diff HEAD~2 HEAD

Compare the changes in a specific file between the commit two steps back from HEAD and the most recent commit

git diff HEAD~2 HEAD audience.txt

Compare the changes in all files between the commit two steps back from HEAD and the most recent commit

git diff HEAD~2 HEAD --name-only

Compare the changes in all files with their change type between the commit two steps back from HEAD and the most recent commit

git diff HEAD~2 HEAD --name-status

Checking out a commit

Checkout a specific commit by its hash ID

git checkout <HASH ID>

Checkout the master branch

git checkout master

Finding a bug with bisect

Start the Git bisect process

git bisect start

Mark the current state as a bad (faulty) point

git bisect bad

Mark a known good commit as a reference point

git bisect good <good commit id>

Mark the current state as a good (working) point

git bisect good

Continue the bisect process by marking the current state as good

git bisect good

Continue the bisect process by marking the current state as bad

git bisect bad

Observe the changes in the identified problematic commit

git bisect bad

Reset and exit the bisect process

git bisect reset

finding contributes Using Shortlog

View a summary of contributors to the repository

git shortlog

Get help on using the git shortlog command

git shortlog --help
git shortlog -> see who contribute to this repository
git shortlog --help

Restoring a Deleted File

Remove the file toc.txt from the repository

git rm toc.txt

Commit the removal of toc.txt

git commit -m "Remove toc.txt"

View the commit history for toc.txt

git log --oneline -- toc.txt

Restore the content of toc.txt to a specific commit

git checkout <Parent Commit ID>

Switch to a specific commit by its hash ID

git checkout a642e12

Commit the restoration of toc.txt

git commit -m "Restore toc.txt"

Blaming

Identify the commit and author responsible for each line of the fileΒ audience.txt

git blame audience.txt

View the email addresses of the authors responsible for each line of the fileΒ audience.txt

git blame -e audience.txt

View the commit and author information for a specific range of lines in the fileΒ audience.txt

git blame -e -L 1,3 audience.txt

Tagging

Create a new tag named v1.0 at an earlier commit

git tag v1.0 <Earlier commit ID>

Checkout a specific tag, v1.0

git checkout v1.0

View all tags in the repository

git tag

Create an annotated tag v1.1 with a descriptive message

git tag -a v1.1 -m "My new version 1.1"

View tags along with their associated messages

git tag -n

View detailed information about a specific tag, v1.1

git show v1.1