Step-by-Step Guide to Create a Pull Request Using Git Commands
Creating a pull request (PR) is a fundamental part of the collaborative development process. Here's a detailed guide to creating a PR using both Git commands and repository hosting platforms like GitHub:
1. Clone the Repository
If you haven't already cloned the repository to your local machine, do so with the following command:
$ git clone <repository-url>
Replace <repository-url> with the URL of the repository you want to work on.
2. Navigate to the Project Directory
Once the repository is cloned, navigate to the project directory:
$ cd <repository-name>
Replace <repository-name> with the name of the cloned repository.
3. Create a New Branch
Create a new branch for your changes. Use a descriptive name for the branch to indicate what you're working on:
$ git checkout -b <branch-name>
For example:
$ git checkout -b feature/add-user-authentication
4. Make Changes
Make the necessary changes to the codebase using your preferred code editor. Once done, save your changes.
5. Check Status of Changes
Check the status of your changes to see what files have been modified:
$ git status
6. Add Changes to Staging
Add the modified files to the staging area:
$ git add <file-name>
To add all changed files at once, use:
$ git add .
7. Commit Your Changes
Commit the changes with a descriptive commit message:
$ git commit -m "<your-commit-message>"
For example:
$ git commit -m "Added user authentication functionality"
8. Push the Branch to Remote Repository
Push your branch to the remote repository:
$ git push origin <branch-name>
For example:
$ git push origin feature/add-user-authentication
9. Create a Pull Request Using GitHub/GitLab/Bitbucket
After pushing the branch, go to the repository's hosting platform (e.g., GitHub, GitLab, Bitbucket). Follow these steps:
- Navigate to the repository in your browser.
- Click on the "Pull Requests" tab.
- Click the "New Pull Request" button.
- Select your branch as the source branch and the branch you want to merge into (usually
mainordevelop) as the target branch. - Add a title and description for the pull request.
- Click "Create Pull Request."
10. Create a Pull Request Using Git Commands
If your repository hosting platform supports creating pull requests via the command line, you can use the following steps:
- Install the platform's CLI tool (e.g.,
ghfor GitHub CLI,glabfor GitLab CLI).
- For GitHub CLI:
$ gh auth login
- Follow the prompts to authenticate your account.
- Create the pull request directly from your terminal:
- For GitHub CLI:
$ gh pr create --base <base-branch> --head <branch-name> --title "<your-title>" --body "<your-description>"
- Example:
$ gh pr create --base main --head feature/add-user-authentication --title "Add User Authentication" --body "This PR adds user authentication functionality."
- For GitLab CLI:
$ glab mr create --source <branch-name> --target <base-branch> --title "<your-title>" --description "<your-description>"
- Confirm and submit the pull request via the CLI prompts.
11. Wait for Review
Your PR will now be available for review by the repository maintainers. They may provide feedback or request changes before merging.
12. Make Additional Changes if Required
If changes are requested, update your local branch, commit the changes, and push them again:
$ git add .
$ git commit -m "<update-message>"
$ git push origin <branch-name>
The new commits will automatically reflect in the pull request.
13. Merge the Pull Request
Once the PR is approved, it can be merged. This step is typically done by a repository maintainer unless you have permissions to do it yourself.
By following these steps, you can successfully create and manage a pull request using both Git commands and hosting platform tools.
