Here's a guide to using Git for the Let'sDo team, with steps and best practices to help everyone understand how to use Git effectively. This guide will focus on using Git within VS Code.
- Make sure Git is installed on your computer. Download it here if needed.
- Open VS Code.
- Open the terminal (
View > Terminal). - Set your username and email (only required once):
git config --global user.name "Your Name" git config --global user.email "your.email@example.com"
The first step is to download the project repository to your local machine.
- Go to the repository page on GitHub and copy the repository URL.
- In VS Code, open the Command Palette (
Ctrl + Shift + Pon Windows/Linux orCmd + Shift + Pon Mac). - Type Git: Clone and press Enter.
- Paste the repository URL and select the folder where you want to save the project.
- VS Code will prompt you to open the cloned repository in a new window. Choose Open to view the project files in VS Code.
The general Git workflow involves pulling the latest changes, making updates, committing your changes, and then pushing those changes back to GitHub. Here’s how each step works in VS Code:
Before you start working, always pull the latest changes from the repository. This ensures your local copy is up-to-date with everyone else’s work.
- In VS Code, go to the Source Control panel (the icon with three branches, or press
Ctrl + Shift + G). - Click on the three dots (...) at the top, then select Pull.
- Alternatively, open the terminal and type:
git pull
To keep the main branch stable, it’s a good practice to work on separate branches for new features or bug fixes.
- Click on the branch name at the bottom left of VS Code (usually says "main" or "master").
- Select Create New Branch and give it a descriptive name, such as
feature-tasksorbugfix-login.
This will create a new branch where you can work without affecting the main branch.
Now you can start working on your assigned tasks. Make your changes to the code in VS Code.
- Save your changes frequently using
Ctrl + S(orCmd + Son Mac). - VS Code will automatically detect changes in files and show them in the Source Control panel.
Once you’ve completed a task or made significant changes, it’s time to commit your work.
- Go to the Source Control panel.
- Enter a commit message describing your changes (e.g., "Add task creation functionality").
- Click the checkmark (✓) icon at the top of the Source Control panel to commit the changes.
Alternatively, you can use the terminal:
git add .
git commit -m "Add task creation functionality"- Write clear, concise commit messages.
- Use the imperative mood (e.g., "Fix bug" instead of "Fixed bug").
- Be descriptive but brief (e.g., "Implement user authentication flow").
After committing, you need to push your changes to the remote repository on GitHub.
- In the Source Control panel, click the three dots (...) and select Push.
- Alternatively, use the terminal:
git push
-
Commit Small, Logical Changes
Make commits for each small, complete task instead of large, unrelated changes. This makes it easier to track changes and troubleshoot if necessary. -
Pull Before You Push
Always pull the latest changes from the main branch before you start working and before you push your changes. This avoids conflicts and ensures your changes are based on the most recent version of the code. -
Handle Merge Conflicts
If you encounter a conflict when pulling or merging:- VS Code will highlight the conflicts in your files. Decide which changes to keep.
- After resolving conflicts, save the file, stage the changes in the Source Control panel, and then commit again.
- Push your changes after resolving the conflict.
-
Regularly Push Your Work
Push your changes often (at least once a day or after completing a task). This ensures your work is saved on GitHub and is accessible to the team. -
Use Branches for New Features or Fixes
Always create a new branch when working on a new feature or bug fix. Once the feature is completed and tested, merge the branch into the main branch with a pull request on GitHub.