GIT - Day-1
Sep-by-step guide to setting up Git on Windows, Mac, and an Amazon Linux 2 EC2 instance, along with a sample project to practice with.
### Step 1: Install Git
#### On Windows
1. Download Git for Windows: Go to the [Git for Windows download page](https://git-scm.com/download/win) and download the installer.
2. Run the installer: Follow the installation prompts, accepting the default settings. This will also install Git Bash, a command-line interface for Git.
#### On Mac
1. Install Xcode Command Line Tools: Open the Terminal and run:
xcode-select --install
2. Install Git using Homebrew (if you have Homebrew installed):
brew install git
#### On Amazon Linux 2 EC2 Instance
1. Connect to your EC2 instance using SSH.
2. Install Git by running:
sudo yum update -y
sudo yum install git -y
### Step 2: Configure Git
Configure Git with your user name and email address. This is important for tracking changes.
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
### Step 3: Create a Sample Project
#### Step 3.1: Initialize a Local Repository
1. Create a new directory for your project:
mkdir sample-git-project
cd sample-git-project
2. Initialize a Git repository:
git init
#### Step 3.2: Create a README File
1. Create a README file:
echo "# Sample Git Project" > README.md
2. Add the README file to the staging area:
git status
git add README.md
3. Commit the README file:
git status
git commit -m "Initial commit with README"
git status
### Summary
This guide walks you through installing Git on various operating systems, configuring it, creating a sample project. Practicing these steps will help you get comfortable with the basic Git workflow.
step-by-step guide to creating a new sample project to practice various Git commands. I will explain each command as we go along.
### Step 1: Initialize a New Git Repository
1. Create a new directory for your project and navigate into it:
mkdir my-git-practice
cd my-git-practice
2. Initialize a Git repository:
git init
*Explanation*: `git init` initializes a new Git repository. It creates a hidden `.git` directory that stores all the information needed for version control.
3. List all files, including hidden ones:
ls -la
*Explanation*: `ls -la` lists all files and directories, including hidden ones (those starting with a dot), in the current directory. You'll see the `.git` directory here, indicating that this is a Git repository.
### Step 2: Create and Add Files
1. Create a new file using `touch`:
touch README.md
*Explanation*: `touch` creates an empty file. Here, `README.md` is created.
2. Add content to the file using `echo`:
echo "# My Git Practice Project" > README.md
*Explanation*: `echo` outputs the string to the terminal. Using `>` redirects the output to the file `README.md`, overwriting its content if it already exists.
3. Check the status of your repository:
git status
*Explanation*: `git status` displays the state of the working directory and the staging area. It shows which changes have been staged, which haven't, and which files aren't being tracked by Git.
4. Add the file to the staging area:
git add README.md
*Explanation*: `git add` adds changes in the working directory to the staging area, preparing them to be included in the next commit.
### Step 3: Commit Changes
1. Commit the staged changes:
git commit -m "Initial commit with README"
*Explanation*: `git commit` captures a snapshot of the project's currently staged changes. The `-m` option allows you to add a commit message inline.
### Step 4: View Information About Your Repository
1. List files tracked by Git:
git ls-files
*Explanation*: `git ls-files` lists all the files that are being tracked by Git.
2. View the commit history:
git log
*Explanation*: `git log` shows the commit history for the repository. By default, it displays the commits in reverse chronological order.
3. View a simplified commit history:
git log --oneline
*Explanation*: `git log --oneline` shows a simplified view of the commit history, displaying each commit on a single line. This includes the commit hash and the commit message.
Explaining the difference between "untracked" and "unstaged" files in Git, followed by a sample project to practice with.
### Untracked vs. Unstaged
- Untracked Files: These are files in your working directory that are not being tracked by Git. They are new files that Git has not been told to keep track of yet. When you create a new file in a Git repository, it starts as untracked.
- Unstaged Files: These are files that are tracked by Git and have been modified but have not yet been added to the staging area using `git add`. Git knows about these files and their changes, but they are not marked for inclusion in the next commit.
### Sample Project for Practice
#### Step 1: Set Up the Project
1. Create a new directory for your project and navigate into it:
mkdir git-practice
cd git-practice
2. Initialize a Git repository:
git init
#### Step 2: Create and Work with Files
1. Create a new untracked file:
echo "Hello, World!" > hello.txt
At this point, `hello.txt` is untracked.
2. Check the status of your repository:
git status
You will see `hello.txt` listed under "Untracked files".
3. Stage the file:
git add hello.txt
4. Commit the staged file:
git commit -m "Add hello.txt"
#### Step 3: Modify and Track Changes
1. Modify the file:
echo "Hello, Git!" >> hello.txt
2. Check the status again:
git status
You will see `hello.txt` listed under "Changes not staged for commit". This means the file is unstaged.
3. Create another untracked file:
echo "This is a new file." > newfile.txt
4. Check the status once more:
git status
You will see `hello.txt` under "Changes not staged for commit" and `newfile.txt` under "Untracked files".
#### Step 4: Practice Commands
1. Stage the changes to `hello.txt`:
git add hello.txt
2. Check the status:
git status
Now `hello.txt` should be listed under "Changes to be committed", and `newfile.txt` remains untracked.
3. Commit the staged changes:
git commit -m "Update hello.txt"
4. Stage and commit the new file:
git add newfile.txt
git commit -m "Add newfile.txt"
### Summary of Commands
- `git init`: Initializes a new Git repository.
- `git status`: Displays the state of the working directory and the staging area.
- `git add <file>`: Adds changes to the staging area.
- `git commit -m "<message>"`: Records changes to the repository.
- `echo "text" > file.txt`: Creates a file with the specified text.
- `echo "text" >> file.txt`: Appends text to a file.
### Practice Summary
1. Create a new file (`hello.txt`), which will be untracked.
2. Stage and commit `hello.txt`.
3. Modify `hello.txt`, making it unstaged.
4. Create another new file (`newfile.txt`), which will be untracked.
5. Stage and commit the changes to `hello.txt`.
6. Stage and commit `newfile.txt`.
By practicing these steps, you’ll get a better understanding of the difference between untracked and unstaged files and become more comfortable with basic Git operations.