top of page

Blaming and Annotating

Objective: The objective of this assignment is to practice using Git's blame and annotate features. You will analyze the history of a file to determine who made specific changes and when those changes were made. Additionally, you will learn how to annotate a file to provide line-by-line explanations.


You want to see who made changes to a specific line of code or file and when those changes were made using the Git blame or annotate command.



Instructions:


Part 1: Repository Setup


1. Create a new directory on your computer for this assignment and name it "GitBlameAnnotateAssignment."


2. Navigate to the "GitBlameAnnotateAssignment" directory in your terminal or command prompt.


3. Initialize a new Git repository in this directory.


4. Create a new text file named "code.py" in the repository.


Part 2: Making Changes


1. Open the "code.py" file using a text editor or a command-line text editor like Vim or Nano.


2. Add some content to the "code.py" file. You can type anything you like.


3. Save the changes to the "code.py" file and exit the text editor.


Part 3: Blaming Changes


1. Use the following Git command to view the history of the "code.py" file and find out who made specific changes:


   

   git blame code.py

   


   This command will display the commit hash, author, and date for each line of the file.


Part 4: Annotating Code


1. Use the following Git command to annotate the "code.py" file with line-by-line explanations:


   

   git annotate code.py

   


   This command will display each line of code with additional information, including the commit hash and author.


Solutions

Part 1: Repository Setup



# Create a directory

mkdir GitBlameAnnotateAssignment


# Navigate to the directory

cd GitBlameAnnotateAssignment


# Initialize a Git repository

git init


# Create a text file

touch code.py



Part 2: Making Changes



# Open the file in a text editor

nano code.py  # Use your preferred text editor


# Add content to code.py and save



Part 3: Blaming Changes



# View the history of the file with blame

git blame code.py



Part 4: Annotating Code



# Annotate the file with explanations

git annotate code.py


bottom of page