GIT - Day-2
sample project to practice using `git diff` and `git diff --staged` commands.
### Step 1: Set Up the Project
1. Create a new directory for your project and navigate into it:
mkdir git-diff-practice
cd git-diff-practice
2. Initialize a Git repository:
git init
### Step 2: Create and Commit Initial Files
1. Create a new file:
echo "Initial content" > file1.txt
2. Stage the file:
git add file1.txt
3. Commit the file:
git commit -m "Initial commit with file1.txt"
### Step 3: Modify the File and Use `git diff`
1. Modify `file1.txt`:
echo "Additional content" >> file1.txt
2. View the differences between the working directory and the last commit:
git diff
*Explanation*: `git diff` shows the changes between the working directory and the last commit. This will display the changes made to `file1.txt`.
### Step 4: Stage the Changes and Use `git diff --staged`
1. Stage the modified file:
git add file1.txt
2. View the differences between the staging area and the last commit:
git diff --staged
*Explanation*: `git diff --staged` shows the changes between the staging area and the last commit. This will display the changes that have been staged for the next commit.
### Practice Summary
1. Create a new file (`file1.txt`) and add initial content.
2. Initialize a Git repository and make the first commit.
3. Modify the file (`file1.txt`) and view the differences using `git diff`.
4. Stage the changes to `file1.txt` and view the differences using `git diff --staged`.
### Explanation of Commands
- `git init`: Initializes a new Git repository.
- `echo "text" > file.txt`: Creates a file with the specified text.
- `git add <file>`: Adds changes to the staging area.
- `git commit -m "<message>"`: Records changes to the repository.
- `echo "text" >> file.txt`: Appends text to a file.
- `git diff`: Shows changes between the working directory and the last commit.
- `git diff --staged`: Shows changes between the staging area and the last commit.
By following these steps, you'll gain hands-on experience with `git diff` and `git diff --staged`, helping you understand how to track and compare changes in your Git repository.
sample project to practice using `git rm`, and `git rm --cached` commands.
### Step 1: Set Up the Project
1. Create a new directory for your project and navigate into it:
mkdir git-rm-practice
cd git-rm-practice
2. Initialize a Git repository:
git init
### Step 2: Create and Commit Initial Files
1. Create a couple of new files:
echo "File 1 content" > file1.txt
echo "File 2 content" > file2.txt
2. Stage and commit the files:
git add file1.txt file2.txt
git commit -m "Initial commit with file1.txt and file2.txt"
### Step 3: Practice Using `git rm`
1. Remove `file1.txt` from both the working directory and the staging area:
git rm file1.txt
2. Check the status to see that `file1.txt` is staged for removal:
git status
3. Commit the change:
git commit -m "Remove file1.txt"
4. List the files tracked by Git:
git ls-files
*Explanation*: `git ls-files` lists all the files that are being tracked by Git.
### Step 5: Practice Using `git rm --cached`
1. Create and add a new file:
echo "File 3 content" > file3.txt
git add file3.txt
2. Check the status:
git status
3. Remove `file3.txt` from the staging area but keep it in the working directory:
git rm --cached file3.txt
4. Check the status again to see `file3.txt` is no longer staged but is still in the working directory:
git status
### Practice Summary
1. Create and commit initial files (`file1.txt` and `file2.txt`).
2. Remove a file (`file1.txt`) using `git rm` and commit the change.
3. List tracked files using `git ls-files`.
4. Add a new file (`file3.txt`), then remove it from the staging area using `git rm --cached`.
### Explanation of Commands
- `git init`: Initializes a new Git repository.
- `echo "text" > file.txt`: Creates a file with the specified text.
- `git add <file>`: Adds changes to the staging area.
- `git commit -m "<message>"`: Records changes to the repository.
- `git rm <file>`: Removes a file from both the working directory and the staging area.
- `git ls-files`: Lists all files tracked by Git.
- `git rm --cached <file>`: Removes a file from the staging area but keeps it in the working directory.
By following these steps, you'll gain hands-on experience with `git rm`, `git ls-files`, and `git rm --cached`, helping you understand how to manage files in your Git repository.
sample project to practice using the `.gitignore` file in Git.
### Step 1: Set Up the Project
1. Create a new directory for your project and navigate into it:
mkdir git-ignore-practice
cd git-ignore-practice
2. Initialize a Git repository:
git init
### Step 2: Create and Commit Initial Files
1. Create some files and directories:
echo "Initial content" > file1.txt
echo "Some content" > file2.log
mkdir temp
echo "Temporary data" > temp/tempfile.txt
2. Stage and commit the files:
git add .
git commit -m "Initial commit with file1.txt, file2.log, and temp directory"
### Step 3: Create a `.gitignore` File
1. Create a `.gitignore` file:
touch .gitignore
2. Edit the `.gitignore` file to ignore certain files and directories:
echo "*.log" > .gitignore
echo "temp/" >> .gitignore
*Explanation*:
- `*.log` ignores all `.log` files.
- `temp/` ignores the entire `temp` directory.
3. Check the status of your repository:
git status
You should see that `file2.log` and `temp/tempfile.txt` are now ignored by Git.
### Step 4: Verify and Commit the `.gitignore` File
1. Add the `.gitignore` file to the staging area:
git add .gitignore
2. Commit the `.gitignore` file:
git commit -m "Add .gitignore to ignore .log files and temp directory"
### Step 5: Practice Ignoring New Files
1. Create new files and directories that match the patterns in `.gitignore`:
echo "Another log file" > anotherfile.log
mkdir temp2
echo "Temporary data 2" > temp2/tempfile2.txt
2. Check the status of your repository again:
git status
You should see that `anotherfile.log` is ignored due to the `*.log` pattern, and `temp2/tempfile2.txt` is tracked because `temp2/` is not included in `.gitignore`.
### Practice Summary
1. Create a new Git repository and commit initial files.
2. Create and configure a `.gitignore` file to ignore specific files and directories.
3. Verify the ignored files using `git status`.
4. Commit the `.gitignore` file to the repository.
5. Create new files and directories to test the ignore patterns.
### Explanation of Commands and Concepts
- `.gitignore`: A file used to tell Git which files or directories to ignore. The patterns in the `.gitignore` file follow specific rules to match files and directories.
- `git status`: Displays the state of the working directory and the staging area, showing which changes are staged, unstaged, or untracked.
- `git add <file>`: Adds changes to the staging area.
- `git commit -m "<message>"`: Records changes to the repository.
- `touch <file>`: Creates an empty file.
By following these steps, you'll gain hands-on experience with creating and using a `.gitignore` file, helping you manage which files and directories Git tracks in your repository.
sample project to practice using `git revert`, `git log`, and `git show` commands.
### Step 1: Set Up the Project
1. Create a new directory for your project and navigate into it:
mkdir git-revert-practice
cd git-revert-practice
2. Initialize a Git repository:
git init
### Step 2: Create and Commit Initial Files
1. Create a new file:
echo "Initial content" > file1.txt
2. Stage and commit the file:
git add file1.txt
git commit -m "Initial commit with file1.txt"
3. Modify the file and commit the change:
echo "Second line of content" >> file1.txt
git add file1.txt
git commit -m "Add second line to file1.txt"
4. Modify the file again and commit the change:
echo "Third line of content" >> file1.txt
git add file1.txt
git commit -m "Add third line to file1.txt"
### Step 3: Practice Using `git log`
1. View the commit history:
git log
*Explanation*: `git log` shows the commit history, including the commit hashes, author, date, and commit messages.
### Step 4: Practice Using `git show`
1. View details of the most recent commit:
git show
*Explanation*: `git show` without any arguments shows the details of the most recent commit, including the changes made.
2. View details of a specific commit (replace `<commit-hash>` with an actual commit hash from the `git log` output):
git show <commit-hash>
*Explanation*: `git show <commit-hash>` shows the details of the specified commit.
### Step 5: Practice Using `git revert`
1. Revert the most recent commit:
git revert HEAD
*Explanation*: `git revert HEAD` creates a new commit that undoes the changes of the most recent commit.
2. View the commit history again:
git log
*Explanation*: The commit history will now include the revert commit.
### Practice Summary
1. Create and commit initial files (`file1.txt`).
2. Make additional changes to `file1.txt` and commit those changes.
3. View the commit history using `git log`.
4. View details of commits using `git show`.
5. Revert a commit using `git revert`.
### Explanation of Commands
- `git init`: Initializes a new Git repository.
- `echo "text" > file.txt`: Creates a file with the specified text.
- `git add <file>`: Adds changes to the staging area.
- `git commit -m "<message>"`: Records changes to the repository.
- `git log`: Shows the commit history.
- `git show`: Shows details of a specific commit.
- `git revert <commit>`: Creates a new commit that undoes the changes of the specified commit.
By following these steps, you'll gain hands-on experience with `git revert`, `git log`, and `git show`, helping you understand how to manage and inspect commits in your Git repository.
project to practice using `git reset` with its different options: `--hard`, `--soft`, and `--mixed`.
### Step 1: Set Up the Project
1. Create a new directory for your project and navigate into it:
mkdir git-reset-practice
cd git-reset-practice
2. Initialize a Git repository:
git init
### Step 2: Create and Commit Initial Files
1. Create a new file:
echo "Initial content" > file1.txt
2. Stage and commit the file:
git add file1.txt
git commit -m "Initial commit with file1.txt"
3. Modify the file and commit the change:
echo "Second line of content" >> file1.txt
git add file1.txt
git commit -m "Add second line to file1.txt"
4. Modify the file again and commit the change:
echo "Third line of content" >> file1.txt
git add file1.txt
git commit -m "Add third line to file1.txt"
### Step 3: Practice Using `git reset --soft`
1. View the commit history:
git log
2. Reset the HEAD to the previous commit (soft reset):
git reset --soft HEAD~1
*Explanation*: `git reset --soft HEAD~1` moves the HEAD pointer to the previous commit and keeps the changes from the reset commit staged.
3. Check the status to see the staged changes:
git status
4. Recommit the staged changes:
git commit -m "Recommit after soft reset"
### Step 4: Practice Using `git reset --mixed`
1. Modify the file again and commit the change:
echo "Fourth line of content" >> file1.txt
git add file1.txt
git commit -m "Add fourth line to file1.txt"
2. Reset the HEAD to the previous commit (mixed reset):
git reset --mixed HEAD~1
*Explanation*: `git reset --mixed HEAD~1` moves the HEAD pointer to the previous commit and unstages the changes from the reset commit.
3. Check the status to see the unstaged changes:
git status
4. Stage and recommit the changes:
git add file1.txt
git commit -m "Recommit after mixed reset"
### Step 5: Practice Using `git reset --hard`
1. Modify the file again and commit the change:
echo "Fifth line of content" >> file1.txt
git add file1.txt
git commit -m "Add fifth line to file1.txt"
2. Reset the HEAD to the previous commit (hard reset):
git reset --hard HEAD~1
*Explanation*: `git reset --hard HEAD~1` moves the HEAD pointer to the previous commit and discards the changes from the reset commit.
3. Check the status to see there are no changes:
git status
4. View the content of the file to confirm changes were discarded:
cat file1.txt
### Practice Summary
1. Create and commit initial files (`file1.txt`).
2. Make additional changes to `file1.txt` and commit those changes.
3. Perform a soft reset to move the HEAD to the previous commit while keeping changes staged.
4. Perform a mixed reset to move the HEAD to the previous commit while unstaging changes.
5. Perform a hard reset to move the HEAD to the previous commit and discard changes.
### Explanation of Commands
- `git init`: Initializes a new Git repository.
- `echo "text" > file.txt`: Creates a file with the specified text.
- `git add <file>`: Adds changes to the staging area.
- `git commit -m "<message>"`: Records changes to the repository.
- `git log`: Shows the commit history.
- `git reset --soft <commit>`: Moves the HEAD pointer to the specified commit and keeps the changes from the reset commit staged.
- `git reset --mixed <commit>`: Moves the HEAD pointer to the specified commit and unstages the changes from the reset commit.
- `git reset --hard <commit>`: Moves the HEAD pointer to the specified commit and discards the changes from the reset commit.
- `cat <file>`: Displays the content of the file.
By following these steps, you'll gain hands-on experience with `git reset` in its different modes (`--hard`, `--soft`, and `--mixed`), helping you understand how to manage commits and changes in your Git repository.
explore the differences between `git revert` and `git reset` with examples.
### `git revert`
`git revert` creates a new commit that undoes the changes from a previous commit. It doesn't change the commit history but adds a new commit to reverse the specified changes.
#### Example:
1. Set up the project:
mkdir git-revert-example
cd git-revert-example
git init
echo "Initial content" > file.txt
git add file.txt
git commit -m "Initial commit"
2. Make a couple of changes and commit them:
echo "Second line" >> file.txt
git add file.txt
git commit -m "Add second line"
echo "Third line" >> file.txt
git add file.txt
git commit -m "Add third line"
3. Revert the second commit (the commit that added the third line):
git log --oneline
# Find the commit hash for the commit that added the third line, e.g., abc123
git revert abc123
4. View the commit history:
git log --oneline
The `git revert` command will create a new commit that undoes the changes made in the specified commit. The commit history will remain linear and intact.
### `git reset`
`git reset` changes the current branch's commit history and can modify the staging area and working directory based on the mode used (`--soft`, `--mixed`, `--hard`).
#### Example:
1. Set up the project:
mkdir git-reset-example
cd git-reset-example
git init
echo "Initial content" > file.txt
git add file.txt
git commit -m "Initial commit"
2. Make a couple of changes and commit them:
echo "Second line" >> file.txt
git add file.txt
git commit -m "Add second line"
echo "Third line" >> file.txt
git add file.txt
git commit -m "Add third line"
3. View the commit history:
git log --oneline
4. Reset to the first commit:
- Soft Reset:
git reset --soft HEAD~2
git log --oneline
git status
The `git reset --soft` command will move the HEAD pointer to the specified commit but keep all changes staged. The commit history will be rewritten, but no changes are lost.
- Mixed Reset:
git reset --mixed HEAD~1
git log --oneline
git status
The `git reset --mixed` command will move the HEAD pointer to the specified commit and unstage all changes. The commit history will be rewritten, and the changes will be in the working directory but not staged.
- Hard Reset:
git reset --hard HEAD~1
git log --oneline
git status
The `git reset --hard` command will move the HEAD pointer to the specified commit and discard all changes. The commit history will be rewritten, and all changes from the reset commit will be lost.
### Summary of Differences
- `git revert`:
- Creates a new commit that undoes changes from a specified commit.
- Keeps the commit history linear and intact.
- Useful for undoing changes in a collaborative environment without rewriting history.
- `git reset`:
- Moves the HEAD pointer to a specified commit and optionally modifies the staging area and working directory.
- Can rewrite commit history and potentially discard changes.
- Useful for undoing commits in a private branch or before sharing with others.
- Modes:
- `--soft`: Keeps changes staged.
- `--mixed`: Keeps changes in the working directory.
- `--hard`: Discards all changes.
By following these examples, you'll gain a clear understanding of how to use `git revert` and `git reset` in different scenarios.
project to practice using `git branch` and `git checkout` & git merge.
### Step 1: Set Up the Project
1. Create a new directory for your project and navigate into it:
mkdir git-branch-checkout-practice
cd git-branch-checkout-practice
2. Initialize a Git repository:
git init
### Step 2: Create and Commit Initial Files
1. Create a new file:
echo "Initial content" > file1.txt
2. Stage and commit the file:
git add file1.txt
git commit -m "Initial commit with file1.txt"
### Step 3: Create and Switch Branches
1. Create a new branch named `feature-branch`:
git branch feature-branch
2. List all branches:
git branch
*Explanation*: The current branch is indicated with an asterisk `*`. You should see `main` (or `master`) and `feature-branch`.
3. Switch to the new branch:
git checkout feature-branch
4. Make changes on `feature-branch`:
echo "Feature branch content" >> file1.txt
git add file1.txt
git commit -m "Add feature branch content to file1.txt"
5. Switch back to the main branch:
git checkout main
6. Check the content of the file on `main` branch:
cat file1.txt
*Explanation*: You should only see the initial content because the changes made in `feature-branch` are not yet in `main`.
### Step 4: Create Another Branch and Make Changes
1. Create a new branch named `bugfix-branch` and switch to it:
git checkout -b bugfix-branch
2. Make changes on `bugfix-branch`:
echo "Bugfix branch content" >> file1.txt
git add file1.txt
git commit -m "Add bugfix branch content to file1.txt"
3. Switch back to the main branch:
git checkout main
4. Check the content of the file on `main` branch:
cat file1.txt
*Explanation*: Again, you should only see the initial content.
### Step 5: Merge Branches
1. Merge `feature-branch` into `main`:
git merge feature-branch
2. Check the content of the file after the merge:
cat file1.txt
*Explanation*: You should see the initial content followed by the content added in `feature-branch`.
3. Merge `bugfix-branch` into `main`:
git merge bugfix-branch
4. Check the content of the file after the second merge:
cat file1.txt
*Explanation*: You should see the initial content followed by the content from both `feature-branch` and `bugfix-branch`.
### Summary of Commands
1. Create and switch branches:
- `git branch <branch-name>`: Creates a new branch.
- `git checkout <branch-name>`: Switches to the specified branch.
- `git checkout -b <branch-name>`: Creates a new branch and switches to it.
2. Make changes and commit them:
- `echo "text" >> file.txt`: Adds text to the file.
- `git add <file>`: Adds changes to the staging area.
- `git commit -m "<message>"`: Records changes to the repository.
3. Merge branches:
- `git merge <branch-name>`: Merges the specified branch into the current branch.
### Explanation of Commands
- `git init`: Initializes a new Git repository.
- `echo "text" > file.txt`: Creates a file with the specified text.
- `echo "text" >> file.txt`: Appends text to the file.
- `git add <file>`: Adds changes to the staging area.
- `git commit -m "<message>"`: Records changes to the repository.
- `git branch <branch-name>`: Creates a new branch.
- `git branch`: Lists all branches.
- `git checkout <branch-name>`: Switches to the specified branch.
- `git checkout -b <branch-name>`: Creates a new branch and switches to it.
- `git merge <branch-name>`: Merges the specified branch into the current branch.
- `cat <file>`: Displays the content of the file.
By following these steps, you'll gain hands-on experience with `git branch` and `git checkout`, helping you manage branches effectively in your Git repository.