Git Workflow Tutorial
Git is a popular version control system used to manage and collaborate on projects. This tutorial covers a typical Git workflow, including creating branches, making commits, merging changes, and handling pull requests. This workflow can be used for both solo and team projects.
Prerequisitesβ
-
Git: Ensure Git is installed on your system.
git --version -
GitHub or GitLab Account: Optional but recommended for remote collaboration.
1. Setting Up a New Git Repositoryβ
Initialize a new Git repository in your project directory:
git init
To connect your local repository to a remote repository on GitHub or GitLab:
git remote add origin <repository-url>
2. Basic Workflow for Making Changesβ
Step 1: Checking the Statusβ
Check the current status of your repository and see any changes:
git status
Step 2: Adding Files to Stagingβ
To stage specific files, use:
git add <file-name>
To stage all changes:
git add .
Step 3: Committing Changesβ
Commit the staged changes with a message:
git commit -m "Describe your changes here"
Step 4: Pushing Changes to Remoteβ
To push changes to the remote repository:
git push origin <branch-name>
3. Branching in Gitβ
Branches allow you to work on features or bug fixes without affecting the main codebase.
Creating a New Branchβ
Create a new branch:
git branch <branch-name>
Switch to the new branch:
git checkout <branch-name>
Or create and switch to a new branch in one command:
git checkout -b <branch-name>
Viewing Branchesβ
List all branches in the repository:
git branch
4. Merging Branchesβ
Merging a Branch into Mainβ
Switch to the main branch first:
git checkout main
Then, merge the feature branch into main:
git merge <branch-name>
Resolving Merge Conflictsβ
If there are conflicts during the merge, Git will notify you. Open the conflicting files, make the necessary changes, and then add and commit the resolved files:
git add <file-name>
git commit -m "Resolve merge conflicts"
5. Pulling Changes from Remoteβ
Before starting new work, itβs a good practice to pull the latest changes from the remote repository:
git pull origin <branch-name>
6. Using Pull Requests (PR)β
A pull request allows you to review and discuss code changes before they are merged into the main branch. In GitHub or GitLab:
-
Push your feature branch to the remote repository:
git push origin <branch-name> -
Open a pull request on GitHub or GitLab.
-
Team members can review the PR, suggest changes, and approve it.
-
Once approved, the branch can be merged into the main branch.
7. Rebasingβ
Rebasing is used to keep your feature branch updated with the main branch.
Example: Rebase a Feature Branchβ
Switch to your feature branch:
git checkout <feature-branch>
Rebase with the main branch:
git rebase main
If there are conflicts, resolve them, then continue the rebase:
git add <file-name>
git rebase --continue
8. Summary of Git Commandsβ
| Command | Description |
|---|---|
git init | Initialize a new Git repository |
git add <file> | Add file to staging |
git commit -m "<message>" | Commit changes with a message |
git push origin <branch> | Push changes to remote repository |
git branch <branch-name> | Create a new branch |
git checkout <branch-name> | Switch to a branch |
git merge <branch-name> | Merge a branch into the current branch |
git pull origin <branch-name> | Pull changes from remote |
git rebase <branch> | Rebase with another branch |
git status | Check the status of your repository |
git log | View commit history |
Summaryβ
This tutorial introduced the basics of a Git workflow, including:
- Setting up a Git repository and connecting it to a remote.
- Creating and merging branches.
- Using pull requests for collaborative code reviews.
- Keeping feature branches up-to-date with rebase.
By following this workflow, you can effectively manage version control for both solo and team projects.
Content Reviewβ
The content in this repository has been reviewed by chevp. Chevp is dedicated to ensuring that the information provided is accurate, relevant, and up-to-date, helping users to learn and implement programming skills effectively.
About the Reviewerβ
For more insights and contributions, visit chevp's GitHub profile: chevp's GitHub Profile.