GIT - Day-4
### Step-by-Step Guide: Sample Project for Git and GitHub
#### Scenario Overview
In this project, you'll practice setting up a GitHub account, creating a repository, generating a Personal Access Token (PAT), creating a local Git repository, connecting it to the GitHub repository, push changes, and managing branches both locally and remotely.
---
### Step 1: Create a GitHub Account and Repository
1. Create a GitHub Account:
- Go to [GitHub](https://github.com) and sign up for a new account if you don’t have one.
2. Create a Repository on GitHub:
- Log in to your GitHub account.
- Click on the "+" icon in the upper right corner and select "New repository."
- Name your repository, e.g., `sample-project`.
- Optionally, you can add a README file (we'll create one locally later).
- Click "Create repository."
---
### Step 2: Create a Personal Access Token (PAT) on GitHub
1. Purpose of a Personal Access Token:
- A PAT is an alternative to using a password when performing Git operations via the command line. It is used for authentication to allow secure access to your GitHub repositories.
2. Create a Personal Access Token:
- Go to your GitHub profile and click on "Settings."
- Scroll down to "Developer settings" and click on "Personal access tokens" > "Tokens (classic)."
- Click "Generate new token."
- Provide a note (e.g., `My first PAT`) and set the expiration (e.g., 90 days).
- Select the `repo` scope to give full control over private repositories.
- Click "Generate token."
- Important: Copy the token and store it securely, as you won't be able to see it again.
---
### Step 3: Set Up a Local Git Repository and Connect to GitHub
1. Create a Local Git Repository:
- Open your terminal (or command prompt) and navigate to your project directory:
mkdir sample-project
cd sample-project
- Initialize a Git repository:
git init
2. Create a README File:
- Create a simple README file:
echo "# Sample Project" > README.md
3. Stage and Commit the File:
- Add the file to the staging area and commit:
git add README.md
git commit -m "Initial commit with README"
4. Connect to the GitHub Repository:
- Add the remote repository URL (replace `your-username` with your GitHub username):
git remote add origin https://github.com/your-username/sample-project.git
5. Pu the Code to GitHub:
- Pu the local repository to the remote repository on GitHub:
git pu -u origin main
- When prompted, use your GitHub username and paste the Personal Access Token as the password.
---
### Step 4: Validate the Remote Branch
1. List All Branches:
- Check the branches on the remote repository:
git branch -r
- You Could see `origin/main`.
2. Verify on GitHub:
- Go to your GitHub repository page and ensure that the `main` branch is visible and contains the committed files.
---
### Step 5: Delete the Local Branch
1. Create and Switch to a New Branch:
- Create a new branch and switch to it:
git checkout -b feature-branch
2. Push the Feature Branch to GitHub:
- Push the new branch to GitHub:
git push -u origin feature-branch
3. Switch Back to the `main` Branch:
- Switch back to the `main` branch:
git checkout main
4. Delete the Local Branch:
- Delete the local `feature-branch`:
git branch -d feature-branch
---
### Step 6: Delete the Remote Branch
1. Delete the Remote Branch:
- Remove the `feature-branch` from the remote repository:
git pu origin --delete feature-branch
2. Verify Deletion on GitHub:
- Go to your GitHub repository page and check that the `feature-branch` is no longer listed under branches.
---
By following these steps, you'll gain practical experience in setting up and managing GitHub repositories, working with local Git repositories, and handling branches both locally and remotely.
expand the previous scenario to include multiple branches, allowing you to practice more advanced Git operations, such as merging and resolving conflicts.
### Scenario Overview
In this extended project, you'll create multiple branches to simulate a collaborative workflow. You'll practice creating and merging branches, resolving conflicts, and managing branches both locally and remotely.
---
### Step 1: Create a GitHub Account and Repository (As Before)
1. Create a GitHub Account:
- Go to [GitHub](https://github.com) and sign up for a new account if you don’t have one.
2. Create a Repository on GitHub:
- Log in to your GitHub account.
- Click on the "+" icon in the upper right corner and select "New repository."
- Name your repository, e.g., `sample-multi-branch-project`.
- Click "Create repository."
---
### Step 2: Create a Personal Access Token (PAT) on GitHub (As Before)
1. Create a Personal Access Token:
- Go to your GitHub profile, navigate to "Settings," then "Developer settings" > "Personal access tokens" > "Tokens (classic)."
- Generate a new token with the `repo` scope.
- Copy and securely store the token.
--
### Step 3: Set Up the Project Locally and Connect to GitHub
1. Create a Local Git Repository:
- Open your terminal (or command prompt) and navigate to your project directory:
mkdir sample-multi-branch-project
cd sample-multi-branch-project
- Initialize a Git repository:
git init
2. Create and Commit an Initial File:
- Create a README file:
echo "# Sample Multi-Branch Project" > README.md
- Add and commit the file:
git add README.md
git commit -m "Initial commit with README"
3. Connect to the GitHub Repository:
- Add the remote repository URL:
git remote add origin https://github.com/your-username/sample-multi-branch-project.git
4. Push the Code to GitHub:
- Push the local repository to GitHub:
git push -u origin main
- Authenticate using your GitHub username and PAT.
---
### Step 4: Create Multiple Branches Locally and Push to GitHub
1. Create and Switch to a `dev` Branch:
- Create a `dev` branch:
git checkout -b dev
- Make some changes:
echo "Development work" > dev.txt
git add dev.txt
git commit -m "Add development work in dev branch"
- Push the `dev` branch to GitHub:
git push -u origin dev
2. Create and Switch to a `feature-a` Branch from `dev`:
- Create `feature-a` branch:
git checkout -b feature-a
- Make some changes:
echo "Feature A work" > feature-a.txt
git add feature-a.txt
git commit -m "Add work on Feature A"
- Push the `feature-a` branch to GitHub:
git push -u origin feature-a
3. Create and Switch to a `feature-b` Branch from `dev`:
- Create `feature-b` branch:
git checkout -b feature-b
- Make some changes:
echo "Feature B work" > feature-b.txt
git add feature-b.txt
git commit -m "Add work on Feature B"
- Push the `feature-b` branch to GitHub:
git push -u origin feature-b
---
### Step 5: Merge Branches and Resolve Conflicts
1. Merge `feature-a` into `dev`:
- Switch to the `dev` branch:
git checkout dev
- Merge `feature-a`:
git merge feature-a
- Resolve any conflicts if necessary, then commit the merge:
git add .
git commit -m "Merge feature-a into dev"
2. Merge `feature-b` into `dev`:
- Merge `feature-b` into `dev`:
git merge feature-b
- If there are conflicts, resolve them and commit the merge:
git add .
git commit -m "Merge feature-b into dev"
3. Push the Updated `dev` Branch to GitHub:
- Push the merged changes:
git push origin dev
---
### Step 6: Validate and Clean Up Branches
1. List Remote Branches:
- List all branches on GitHub:
git branch -r
- You should see `origin/main`, `origin/dev`, `origin/feature-a`, and `origin/feature-b`.
2. Delete Local Feature Branches:
- Delete `feature-a` and `feature-b` locally:
git branch -d feature-a
git branch -d feature-b
3. Delete Remote Feature Branches:
- Delete `feature-a` and `feature-b` from GitHub:
git push origin --delete feature-a
git push origin --delete feature-b
4. Verify Deletion on GitHub:
- Go to your GitHub repository and ensure that `feature-a` and `feature-b` branches are no longer listed.
---
### Step 7: Merge `dev` into `main` and Final Cleanup
1. Merge `dev` into `main`:
- Switch to the `main` branch:
git checkout main
- Merge the `dev` branch:
git merge dev
- Resolve any conflicts, if necessary, then commit the merge:
git add .
git commit -m "Merge dev into main"
2. Push the Final `main` Branch to GitHub:
- Push the `main` branch with merged changes:
git push origin main
3. Delete the `dev` Branch Locally and Remotely:
- Delete the `dev` branch locally:
git branch -d dev
- Delete the `dev` branch from GitHub:
git push origin --delete dev
4. Verify Final State on GitHub:
- Ensure only the `main` branch remains on GitHub, with all changes merged.
---
This scenario covers the full lifecycle of a project with multiple branches, from creation to merging and cleanup. It helps you practice core Git operations, including managing branches both locally and remotely, and resolving conflicts.
### Sample Project for Practicing `git clone`, `git pull`, and `git fetch`
#### Overview
In this project, you'll simulate a collaborative workflow where multiple team members work on a shared GitHub repository. You'll practice cloning a repository, fetching updates, and pulling the latest changes from the remote repository.
---
### Step 1: Understanding the Commands
1. `git clone`:
- What it does: `git clone` is used to create a copy of an existing Git repository from a remote server (like GitHub) to your local machine. It copies all the files, branches, and commit history from the remote repository.
- Scenario: You join a new project, and you need to set up the project on your local machine by cloning the repository from GitHub.
2. `git pull`:
- What it does: `git pull` is a combination of `git fetch` and `git merge`. It fetches changes from the remote repository and immediately tries to merge them into your current branch. If there are any conflicts, you need to resolve them before completing the merge.
- Scenario: You’ve been working on a project, but before you start new work, you need to pull the latest changes from the remote repository to ensure you’re up to date.
3. `git fetch`:
- What it does: `git fetch` retrieves updates from the remote repository but does not automatically merge them into your working branch. It updates your local copy of the remote branches, allowing you to review changes before merging.
- Scenario: You want to see what changes have been made in the remote repository without immediately merging them into your local branch.
---
### Step 2: Set Up the GitHub Repository
1. Create a GitHub Repository:
- Go to GitHub and create a new repository named `collaborative-project`.
- Add a README file, a `.gitignore` file, and a license if needed.
- Click "Create repository."
2. Add Some Initial Files:
- On GitHub, add a few files (e.g., `index.html`, `style.css`, `script.js, or readme.txt`), or make some initial commits to simulate the repository having existing work.
---
### Step 3: Clone the Repository
1. Clone the Repository to Your Local Machine:
- Open your terminal or command prompt.
- Navigate to the directory where you want to clone the repository.
- Run the `git clone` command:
git clone https://github.com/your-username/collaborative-project.git
- This will create a directory named `collaborative-project` containing all the files and history from the remote repository.
2. Verify the Cloned Repository:
- Navigate into the cloned repository:
cd collaborative-project
- List the files to ensure everything is there:
ls
- Check the remote URL:
git remote -v
- This should show the URL of the GitHub repository.
---
### Step 4: Simulate Updates from Another Developer
1. Make Changes on GitHub:
- Imagine another developer (or yourself in another browser) makes changes directly on GitHub.
- For example, add a new file `new-feature.txt` or modify an existing file like `index.html`.
- Commit the changes directly on GitHub.
--
### Step 5: Fetch Changes Without Merging (`git fetch`)
1. Fetch Updates from the Remote Repository:
- While still in your cloned repository on your local machine, fetch the latest changes:
git fetch origin
- This command downloads updates from the remote repository without merging them into your current branch.
2. Check the Fetched Changes:
- After fetching, you can check which branches were updated:
git branch -r
- To see the changes in the fetched branch, you can compare it with your current branch:
git diff origin/main
---
### Step 6: Pull Changes and Merge (`git pull`)
1. Pull Updates from the Remote Repository:
- After reviewing the fetched changes, pull the latest updates:
git pull origin main
- This will fetch the changes and merge them into your current branch (`main` in this case).
- If there are conflicts, Git will prompt you to resolve them.
2. Resolve Any Conflicts:
- If conflicts occur, open the conflicting files, resolve the differences, and then complete the merge:
git add .
git commit -m "Resolved merge conflicts"
---
### Step 7: Simulate a Scenario with Multiple Developers
1. Clone the Repository Again in a Different Directory:
- Simulate another developer by cloning the repository in a different directory or on a different machine:
git clone https://github.com/your-username/collaborative-project.git developer-2
cd developer-2
2. Make Changes in the Second Clone:
- Add a new file or modify an existing file in the second clone, then commit the changes:
echo "Some new content" > new-feature.txt
git add new-feature.txt
git commit -m "Add new feature"
3. Push Changes to the Remote Repository:
- Push the changes to GitHub from the second clone:
git push origin main
4. Return to the First Clone and Pull the Latest Changes:
- Go back to the first clone and pull the latest changes:
cd ../collaborative-project
git pull origin main
- This will bring the changes made by the "second developer" into the first clone.
---
This scenario helps you understand the differences between `git clone`, `git fetch`, and `git pull`, and provides a practical context for using these commands in a collaborative environment.
### Project: Handling a File Conflict Between Local and Remote Repositories
#### Overview
In this project, you'll simulate a scenario where a file with the same name but different content is created in both your local repository and the remote repository. You'll then attempt to push your local changes to the remote repository, which will result in a conflict. This scenario will help you practice resolving conflicts and understanding how Git handles these situations.
### Step 1: Set Up the GitHub Repository
1. Create a GitHub Repository:
- Go to GitHub and create a new repository named `conflict-demo`.
- Add a README file to initialize the repository, then click "Create repository."
2. Clone the Repository to Your Local Machine:
- Open your terminal (or command prompt) and clone the repository:
git clone https://github.com/your-username/conflict-demo.git
- Navigate into the cloned repository:
cd conflict-demo
### Step 2: Create a File in the Remote Repository
1. Create a File Directly on GitHub:
- Go to the `conflict-demo` repository on GitHub.
- Click "Add file" > "Create new file."
- Name the file `conflict.txt`.
- Add some content to the file, for example:
This content was added directly on GitHub.
- Scroll down and commit the new file by clicking "Commit new file."
2. Verify the File on GitHub:
- Ensure that the `conflict.txt` file now exists in the repository on GitHub with the content you added.
### Step 3: Create a Different Version of the Same File Locally
1. Create a Different Version of `conflict.txt` Locally:
- In your local `conflict-demo` repository, create the `conflict.txt` file with different content:
echo "This content was added locally." > conflict.txt
- Add and commit the file locally:
git add conflict.txt
git commit -m "Add conflict.txt with local content"
2. Attempt to Push the Local Changes to GitHub:
- Try to push your changes to the remote repository:
git push origin main
3. Observe the Conflict:
- Git will reject the push because the `conflict.txt` file already exists in the remote repository with different content. You'll see an error message suggesting you need to pull first.
### Step 4: Resolve the Conflict
1. Pull the Remote Changes to Your Local Repository:
- Pull the changes from the remote repository:
git pull origin main
- Git will attempt to merge the changes and will detect a conflict in the `conflict.txt` file.
2. Resolve the Conflict:
- Open the `conflict.txt` file in a text editor. You’ll see conflict markers like this:
<<<<<<< HEAD
This content was added locally.
=======
This content was added directly on GitHub.
>>>>>>> origin/main
- Edit the file to resolve the conflict. You can choose to keep one version, combine both, or make other modifications. For example:
This content was added locally.
This content was added directly on GitHub.
- After resolving the conflict, save the file.
3. Commit the Resolved Conflict:
- Add the resolved file to the staging area and commit the changes:
git add conflict.txt
git commit -m "Resolve conflict in conflict.txt"
4. Push the Resolved Changes to GitHub:
- Push your changes to the remote repository:
git push origin main
### Step 5: Verify the Final State
1. Check the Remote Repository on GitHub:
- Go back to the `conflict-demo` repository on GitHub.
- Verify that the `conflict.txt` file has been updated with the resolved content.
2. Check the Commit History:
- On GitHub, look at the commit history to see the merge commit that resolved the conflict.
This scenario helps you understand how Git handles conflicts when the same file has different content in the local and remote repositories. By practicing this, you can get comfortable with resolving conflicts in real-world situations.
### Project: Handling a File Conflict with `git pull --rebase`
#### Overview
This project will guide you through a scenario where you create a file with the same name but different content in both your local repository and the remote repository. You will then use `git pull --rebase` to integrate the changes from the remote repository, which will result in a conflict. This scenario will help you practice resolving conflicts during a rebase.
### Step 1: Set Up the GitHub Repository
1. Create a GitHub Repository:
- Go to GitHub and create a new repository named `rebase-conflict-demo`.
- Add a README file to initialize the repository, then click "Create repository."
2. Clone the Repository to Your Local Machine:
- Open your terminal (or command prompt) and clone the repository:
git clone https://github.com/your-username/rebase-conflict-demo.git
- Navigate into the cloned repository:
cd rebase-conflict-demo
### Step 2: Create a File in the Remote Repository
1. Create a File Directly on GitHub:
- Go to the `rebase-conflict-demo` repository on GitHub.
- Click "Add file" > "Create new file."
- Name the file `rebase-conflict.txt`.
- Add some content to the file, for example:
This content was added directly on GitHub.
- Scroll down and commit the new file by clicking "Commit new file."
2. Verify the File on GitHub:
- Ensure that the `rebase-conflict.txt` file now exists in the repository on GitHub with the content you added.
### Step 3: Create a Different Version of the Same File Locally
1. Create a Different Version of `rebase-conflict.txt` Locally:
- In your local `rebase-conflict-demo` repository, create the `rebase-conflict.txt` file with different content:
echo "This content was added locally." > rebase-conflict.txt
- Add and commit the file locally:
git add rebase-conflict.txt
git commit -m "Add rebase-conflict.txt with local content"
2. Attempt to Push the Local Changes to GitHub:
- Try to push your changes to the remote repository:
git push origin main
- Git will reject the push because the `rebase-conflict.txt` file already exists in the remote repository with different content. You'll see an error message suggesting you need to pull first.
### Step 4: Resolve the Conflict with `git pull --rebase`
1. Pull the Remote Changes with Rebase:
- Instead of a regular `git pull`, you'll rebase your changes on top of the latest remote changes:
git pull --rebase origin main
- Git will attempt to rebase your changes, but since there is a conflict in the `rebase-conflict.txt` file, Git will pause and notify you of the conflict.
2. Resolve the Conflict:
- Open the `rebase-conflict.txt` file in a text editor. You’ll see conflict markers like this:
<<<<<<< HEAD
This content was added directly on GitHub.
=======
This content was added locally.
>>>>>>> YOUR_COMMIT_HASH
- Edit the file to resolve the conflict. For example, you could combine both versions:
This content was added locally.
This content was added directly on GitHub.
- After resolving the conflict, save the file.
3. Continue the Rebase:
- After resolving the conflict, you need to stage the resolved file:
git add rebase-conflict.txt
- Then, continue the rebase process:
git rebase --continue
- Git will finish applying your changes on top of the latest remote changes.
4. Push the Resolved Changes to GitHub:
- Finally, push your changes to the remote repository:
git push origin main
### Step 5: Verify the Final State
1. Check the Remote Repository on GitHub:
- Go back to the `rebase-conflict-demo` repository on GitHub.
- Verify that the `rebase-conflict.txt` file has been updated with the resolved content.
2. Check the Commit History:
- On GitHub, review the commit history to see the result of the rebase and the conflict resolution.
This scenario helps you understand how to handle conflicts during a rebase using `git pull --rebase`, giving you practical experience with one of the more complex aspects of Git.