Git workflow
The Git workflow refers to a set of practices and processes that developers use to collaborate on code using Git, a distributed version control system. Here’s a step-by-step overview of a typical Git workflow:
1. Clone Repository:
- The first step is to clone the remote repository to your local machine.
- Command: git clone <repository-url>
2. Create a Branch:
- Create a new branch for the feature or bug fix you are working on. This keeps your changes isolated from the main codebase.
- Command: git checkout -b <branch-name>
3. Make Changes:
- Edit the code, add new features, or fix bugs on your branch.
4. Stage Changes:
- After making changes, you need to stage them before committing. Staging lets you pick and choose what changes you want to include in the next commit.
- Command: git add . (stages all changes) or git add <file-name> (stages specific files)
5. Commit Changes:
- Commit the staged changes with a descriptive message explaining what you have done.
- Command: git commit -m "Your commit message"
6. Push Changes:
- Push your branch to the remote repository so that others can see and review your changes.
- Command: git push origin <branch-name>
7. Create a Pull Request (PR):
- On the hosting service (like GitHub, GitLab, or Bitbucket), create a pull request to merge your changes into the main branch. This initiates a code review process.
- This step is usually done through the web interface of the hosting service.
8. Review Process:
- Collaborators review your code, suggest changes, and approve the pull request if everything looks good. You may need to make additional commits to address review feedback.
9. Merge the Pull Request:
- Once the pull request is approved, it can be merged into the main branch. This can often be done through the web interface or via command line.
- After merging, delete the branch to keep the repository clean.
10. Update Local Repository:
- Sync your local repository with the main branch to keep it up to date with the latest changes.
- Commands: git checkout main and git pull origin main
### Common Commands Recap:
- git clone <repository-url>: Clone a repository to your local machine.
- git checkout -b <branch-name>: Create and switch to a new branch.
- git add . or git add <file-name>: Stage changes.
- git commit -m "Your commit message": Commit changes with a message.
- git push origin <branch-name>: Push the branch to the remote repository.
- git pull origin main: Pull the latest changes from the main branch.
This workflow helps manage changes in a collaborative environment, ensuring that the main codebase remains stable and that contributions are reviewed and tested before integration.