This tutorial will walk you through a standard Git workflow, which includes creating a new branch, checking its status, adding changes, committing them, and finally, pushing them to the remote repository. The workflow also includes how to prepare for a pull request.
Here, we'll be using two remotes: 'origin' represents your fork of the original repository, and 'upstream' represents the original repository itself.
Before creating a new branch, ensure your local main branch is up to date:
git checkout main
git pull upstream main
This updates your local version of the main branch.
To create a new branch:
git checkout -b [branch_name]
Replace [branch_name]
with the name you want for your branch.
After you complete a chunk of work:
- Check the status of your changes:
git status
- Add the changes you want to commit:
git add [file_names]
- Commit your changes with a message:
git commit -m "commit_message"
- Push your changes to the origin remote for safekeeping:
git push origin [branch_name]
Before you open a pull request in a collaborative project, you need to fork the existing repository, create your own remote, create a new branch, and finally open a pull request to merge them.
- In the project on github.com, find the "fork" button, and create a fork of the project.
- To create your own remote, you need to use the following command:
git remote add [remote name] [remote address]
[remote name]
is whatever short name you'd like that's not "origin"[remote address]
is formatted as:https://github.com/[other user]/[reponame].git
- Now follow "preparing a pull request" except instead of origin, use
[remote name]
You only need to do step 1 and 2 once. Any future pull requests should use [remote name]
instead of origin.
Before you open a pull request to merge your branch with the main branch:
- Update your local main branch:
git checkout main git pull upstream main
- Push your updated main branch to the origin remote:
git push origin main
- Checkout the branch you were developing on:
git checkout [branch_name]
- Rebase your branch onto the updated main branch:
git rebase -i main
To give permissions for pushes and pulls:
eval `ssh-agent -s`
ssh-add ~/.ssh/[key_file_name]
Replace [key_file_name]
with the name of your key file.
For more information about keys and ssh'ing, check out my other tutorial and helpful scripts for this: https://github.com/morganrivers/how_to_ssh_key/