0% found this document useful (0 votes)
7 views

githelpdoc

The document provides a comprehensive step-by-step guide for collaborating on a project using Git, covering essential commands for setting up a repository, creating branches, making changes, committing, pushing, and creating pull requests. It includes instructions for reviewing and merging pull requests, keeping local repositories updated, and deleting old branches. The guide aims to facilitate effective collaboration while minimizing merge conflicts among team members.

Uploaded by

charlog24
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

githelpdoc

The document provides a comprehensive step-by-step guide for collaborating on a project using Git, covering essential commands for setting up a repository, creating branches, making changes, committing, pushing, and creating pull requests. It includes instructions for reviewing and merging pull requests, keeping local repositories updated, and deleting old branches. The guide aims to facilitate effective collaboration while minimizing merge conflicts among team members.

Uploaded by

charlog24
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Collaborating on a project with Git involves several steps: creating branches, making

changes, committing them, pushing to the repository, and creating pull requests. Below is
a step-by-step guide to walk you through the entire process:

1. Setting Up the Repository


Make sure you've cloned the repository already:

git clone <repository_url>


cd <repository_name>

To check the status of your repository and see the current branch:

git status
git branch

2. Creating a New Branch


When working on a new feature or bug fix, always create a new branch. This ensures your
changes don’t interfere with the main branch or others' work.

Command:
git checkout -b <branch_name>

• Replace <branch_name> with a descriptive name like add-login-


feature or fix-bug-issue42.
• git checkout -b creates and switches to the new branch.

Verify the Branch:


git branch

The new branch will have a * next to it.

3. Making Changes
Now that you're in your new branch, make the necessary changes in the code using your code
editor.

Check Status After Changes:

To see what has been changed:


git status

4. Adding Changes
To stage the changes you’ve made:

git add <file_name>

• Replace <file_name> with the file(s) you’ve modified.


• To add all changes at once:

git add .

5. Committing Changes
Once the changes are staged, commit them with a meaningful message:

Command:
git commit -m "Describe your changes briefly, e.g., 'Added
login functionality'"

6. Pushing the Branch


Push the branch to the remote repository so others can access it:

Command:
git push origin <branch_name>

• Replace <branch_name> with the name of your branch.

7. Creating a Pull Request


Now that your branch is on GitHub, you can create a pull request (PR).

Steps:

1. Go to the GitHub repository in your browser.


2. Switch to your branch by selecting it from the branch dropdown.
3. GitHub may suggest creating a pull request at the top. If not:
• Click on the Pull Requests tab.
• Click on New Pull Request.

1. Compare your branch with the main branch (or the branch you want to merge into).
2. Add a title and description for your PR to explain your changes.
3. Click Create Pull Request.

8. Reviewing the Pull Request


• Your classmates or project maintainers will review the PR. They may suggest changes
or approve it.
• If changes are requested:

1. Make the edits locally in your branch.


2. Stage and commit the changes (git add and git commit).
3. Push the updated branch again (git push origin <branch_name>).

GitHub will automatically update the pull request with the new changes.

9. Merging the Pull Request


Once approved:

1. Go to the Pull Request on GitHub.


2. Click Merge Pull Request.
3. Confirm the merge.
4. The branch is now merged into the main branch.

10. Keeping Your Local Repo Updated


To avoid conflicts, ensure your local repository is in sync with the main branch:

Command:
git checkout main
git pull origin main

If your branch is behind the main branch, you can rebase or merge the changes into your
branch:

git checkout <your_branch>


git merge main
11. Deleting the Old Branch
After merging, delete the branch to keep the repository clean:

Locally:
git branch -d <branch_name>

Remotely:
git push origin --delete <branch_name>

Example Workflow:

Let’s say you’re working on a login feature:

1. Create a branch: git checkout -b login-feature.


2. Make changes to the code.
3. Stage changes: git add ..
4. Commit changes: git commit -m "Added login functionality".
5. Push to GitHub: git push origin login-feature.
6. Create a pull request on GitHub to merge login-feature into main.
7. Address any feedback, then merge.
8. Update your local main branch: git checkout main && git pull
origin main.

By following this process, you and your classmates can collaborate effectively while
minimizing merge conflicts. Let me know if you need help with any step!
To see all branches in the remote repository, you can use the following command:

git branch -r

This will list all remote branches, such as:

origin/main
origin/problem_image_upload

If you want to create a local branch that tracks the remote problem_image_upload branch,
you need to check it out:

git checkout -b problem_image_upload origin/problem_image_upload

Explanation of the Command:

• git checkout -b: Creates a new local branch and switches to it.
• problem_image_upload: The name of the new local branch.
• origin/problem_image_upload: Specifies the remote branch to track.

After this, running git branch will show:

* problem_image_upload
main

Now you have both branches locally. If you switch back to main or need to work with
another branch, you can use:

git checkout main

Alternative: Fetch All Remote Branches

If you want to fetch all branches from the remote repository, use:

git fetch

This will ensure all remote branches are available locally for tracking. To see both remote
and local branches together:

git branch -a

This will list:

* main
remotes/origin/main
remotes/origin/problem_image_upload

You might also like