- Introduction
- Installing Git
- Basic Git Concepts
- Essential Git Commands
- Git Branching
- Pull Requests and Issue
- Working with Git in Visual Studio Code (VS Code)
- Best Practices
Git is a powerful version control system that tracks changes to your files, enabling collaboration and history tracking. GitHub is a cloud-based platform that hosts Git repositories, facilitating collaboration among developers.
Before using Git, you need to install it on your computer.
- Windows: Download Git from git-scm.com and follow the installation instructions.
- macOS: Install Git using Homebrew:
brew install git
- Linux: Our server already has git.
A repository (or "repo") is a directory that contains your project files and the complete history of changes made to those files.
The staging area is where you prepare files before committing them. You add changes to the staging area using git add.
A commit is a snapshot of your repository at a specific point in time. Commits include a message describing the changes.
Branches allow you to develop features or fixes in isolation from the main codebase. The default branch is usually main or master.
Initializes a new Git repository in your current directory.
git init
Clones an existing Git repository from a remote location (like GitHub) to your local machine.
git clone <repository-url>
Adds changes from the working directory to the staging area.
git add <file>
# Or add all changes
git add .
Commits the staged changes with a descriptive message.
git commit -m "Your commit message"
Pushes your local commits to a remote repository (e.g., GitHub).
git push origin <branch-name>
Fetches changes from a remote repository without merging them into your working directory.
git fetch
Fetches changes from a remote repository and merges them into your current branch.
git pull origin <branch-name>
Lists all branches in your repository, or creates a new branch.
# List branches
git branch
# Create a new branch
git branch <new-branch-name>
Switches to a different branch.
git checkout <branch-name>
Merges changes from one branch into your current branch.
git merge <branch-name>
Shows the status of changes as untracked, modified, or staged.
git status
Shows the commit history for the repository.
git log
Branch protection is a critical feature that helps maintain the stability and integrity of your main codebase. By protecting branches like main
, you can ensure that:
- Code is Reviewed: All changes are reviewed by at least one other developer before being merged, reducing the chance of introducing bugs.
- Tests are Passed: Automated tests are run, and must pass before code is merged, ensuring that new changes don’t break existing functionality.
- Commit History is Clean: By enforcing practices like “Squash and Merge,” you can keep your commit history clean and easy to navigate.
Create a new branch to develop a feature or fix a bug without affecting the main branch.
git branch <branch-name>
Switch to the branch you want to work on.
git checkout <branch-name>
When your feature or fix is complete, merge the branch back into main or develop.
git checkout main
git merge <branch-name>
Pull requests (PRs) are a key part of a collaborative Git workflow. They allow developers to propose changes to the codebase, which can then be reviewed and discussed before being merged.
- Creating a Pull Request:
- Push your feature branch to the remote repository.
- Go to your repository on GitHub, and you’ll see an option to create a pull request from your branch.
- Provide a clear title and description for your pull request, explaining what changes you’ve made and why.
- Reviewing a Pull Request:
- Other team members can review the code, leave comments, and suggest changes.
- The pull request should only be merged after it has been reviewed and approved.
- Merge the PR using the appropriate method (e.g., "Squash and Merge") to keep the commit history clean.
- Closing a Pull Request If a pull request is no longer needed, it can be closed without merging. This is useful if the changes are no longer relevant or if they’ve been superseded by other work.
- Creating Issues
- Go to the "Issues" tab in your repository.
- Click on "New Issue."
- Provide a descriptive title and a detailed explanation of the issue.
- Assign the issue to a team member, add labels, and set milestones as needed.
- Managing Issues
- Assigning Issues: Issues can be assigned to specific team members, making it clear who is responsible for addressing them.
- Labels and Milestones: Use labels to categorize issues (e.g., bug, enhancement) and milestones to group issues that should be completed by a certain date.
- Linking to Pull Requests: When you open a pull request that addresses an issue, you can link the two. GitHub will automatically close the issue when the pull request is merged.
- Open VS Code.
- Use Ctrl+Shift+P to open the command palette.
- Type Git: Clone and select it.
- Enter the repository URL and choose a local directory.
- Make changes to your files in VS Code.
- Go to the Source Control view by clicking the Git icon in the Activity Bar.
- Stage your changes by clicking the
+
icon next to the files. - Enter a commit message and click the checkmark icon to commit.
- After committing, click the
...
menu in the Source Control view. - Select Push to push your changes to the remote repository.
- To update your local branch with remote changes, click the
...
menu in the Source Control view. - Select Pull to fetch and merge changes.
- To create or switch branches, click on the branch name in the lower-left corner.
- Select Create new branch or choose an existing branch.
- Commit Often: Commit your changes frequently with clear and concise messages.
- Use Branches: Always use branches for new features or fixes.
- Pull Before You Push: Always pull the latest changes from the remote repository before pushing your changes.
- Review Before Merging: Use pull requests and code reviews to ensure quality before merging.