--- id: git-workflow-vscode title: Git & Pull Request Workflow sidebar_label: Git & Pull Requests --- # 📌 Git & Pull Request Workflow Git is a **distributed version control system** commonly used in software development. This guide explains the **basic usage of Git**, the **workflow with Pull Requests**, and the **integration with Visual Studio Code**. ## 📝 Prerequisites - Installed Git ([Download here](https://git-scm.com/)) - GitHub, GitLab, or Bitbucket account (for remote repositories) - Basic knowledge of Terminal or CLI - **Optional:** [Visual Studio Code](https://code.visualstudio.com/) for an easier Git workflow --- ## 🔧 Step 1: Installing Git on Windows, Linux & macOS ### 1️⃣ Installing Git on Windows 1. Download Git from [git-scm.com](https://git-scm.com/). 2. Run the installer and follow the setup instructions. 3. Choose the default terminal as "Git Bash" or "Command Prompt." 4. Verify Git installation: ```sh git --version ``` ### 2️⃣ Installing Git on Linux (Debian/Ubuntu) ```sh sudo apt update && sudo apt install -y git ``` ### 3️⃣ Installing Git on macOS ```sh brew install git ``` --- ## 🔧 Step 2: Configuring Git After installation, configure Git with your name and email: ```sh git config --global user.name "Your Name" git config --global user.email "your.email@example.com" ``` Verify your Git configuration: ```sh git config --list ``` --- ## 📂 Step 3: Creating & Cloning a Repository ### 1️⃣ Creating a New Repository On GitHub, GitLab, or Bitbucket: - Create a new repository with a **name** and an optional **README file**. ### 2️⃣ Cloning a Repository To clone the repository locally: ```sh git clone https://github.com/USERNAME/REPOSITORY.git cd REPOSITORY ``` --- ## 🔨 Step 4: Making Changes & Committing 1. **Create a new file or modify an existing one:** ```sh echo "Hello World" > hello.txt ``` 2. **Check the status of changes:** ```sh git status ``` 3. **Add changes to the staging area:** ```sh git add hello.txt ``` 4. **Commit the changes:** ```sh git commit -m "Added my first file" ``` --- ## 🔄 Step 5: Pushing Changes to the Remote Repository To push changes to GitHub/GitLab: ```sh git push origin main ``` If working on a new branch: ```sh git push origin feature-branch ``` --- ## 🌿 Step 6: Creating & Using Feature Branches ### 1️⃣ Creating a New Branch: ```sh git checkout -b feature-new-feature ``` ### 2️⃣ Making Changes and Committing: ```sh git add . git commit -m "Added a new feature" ``` ### 3️⃣ Pushing the Branch to Remote: ```sh git push origin feature-new-feature ``` --- ## 🔄 Step 7: Creating a Pull Request (PR) 1. **Go to your repository on GitHub/GitLab.** 2. **Switch to the new branch and click "Create Pull Request."** 3. **Describe the changes and submit the PR.** 4. **Wait for a code review and make any necessary adjustments.** --- ## 🔄 Step 8: Merging a Pull Request Once your PR is approved: - Click **Merge Pull Request**. - Optionally, delete the branch: ```sh git branch -d feature-new-feature git push origin --delete feature-new-feature ``` --- ## 🔄 Step 9: Keeping Your Repository Up-to-Date If the `main` branch has been updated, sync your local repository: ```sh git checkout main git pull origin main ``` If working on a branch, merge `main` into it: ```sh git checkout feature-new-feature git merge main ``` If merge conflicts occur, resolve them and commit again. --- ## 💻 Using Git in Visual Studio Code 1. **Install Visual Studio Code:** Download and install it from [here](https://code.visualstudio.com/). 2. **Enable Git in VS Code:** - Open **VS Code**. - Press `Ctrl + P` (Windows/Linux) or `Cmd + P` (macOS) and type `Git: Enable`. - If Git is not detected, set the Git path in the settings. 3. **Cloning a Repository:** - Open the `Terminal` in VS Code (``Ctrl + ` ``). - Run the following command: ```sh git clone https://github.com/USERNAME/REPOSITORY.git ``` 4. **Making Changes & Committing:** - Modify files in VS Code and save them. - Open the **Source Control** sidebar (`Ctrl + Shift + G`) to see changes. - Click the `+` button to stage changes. - Enter a commit message and click `Commit`. 5. **Pushing Changes:** - Click `Sync` in the **Source Control** tab, or run: ```sh git push origin main ``` 6. **Branching & Pull Requests:** - Click the **current branch** (bottom left in VS Code). - Select **Create New Branch**. - After making changes, push the new branch. - Create a **Pull Request on GitHub/GitLab**, review, and merge. --- ## 🛑 Step 10: Troubleshooting (Common Issues) If Git is not working, check: 1. **Is Git installed?** ```sh git --version ``` 2. **Is Git recognized in the system path? (Windows)** ```sh where git ``` 3. **Is the repository initialized correctly?** ```sh git status ``` 4. **Are the correct credentials for GitHub/GitLab being used?** If `fatal: Authentication failed` appears, check your **SSH keys** or **personal access tokens**. 5. **Merge conflicts?** If `CONFLICT (content)` appears, open the affected files in **VS Code**, resolve conflicts, and commit again. --- ## ✅ Conclusion Now you are ready to use **Git**, efficiently work with **Pull Requests**, and integrate **Git with Visual Studio Code**. For further reading, check the official documentation: - [GitHub Docs](https://docs.github.com/en) - [GitLab Docs](https://docs.gitlab.com/) - [Atlassian Git Tutorials](https://www.atlassian.com/git/tutorials) **Happy Coding! 🚀**