Git can be daunting to beginners, and it took me awhile before I got comfortable with a lot of the commands. I am assuming you have cloned your repository to your local machine. Here is the workflow that I use:
Git workflow for new branch
- Create an issue on GitHub
- Update local
main
branch:git checkout main && git pull
- Create a branch with the format
issue_number-description
(e.g.,git checkout -b 42-new-branch
- Make your changes and stage them (e.g.,
git add newFile.js
) - Check that the appropriate files are staged:
git status
- Commit the changes:
git commit
- (Optional) If, at this stage, your branch not up-to-date with the latest
main
, rebase usinggit rebase origin/main
OR merge usinggit merge origin/main
- Push to remote branch with same name:
git push -u origin 42-new-branch
- Create a Pull Request with, for example,
Closes #42
in the body to link the issue - Merge the branch on GitHub and delete the branch after
- Assuming you are still on your branch, update your local repo with
git pull origin main:main
- Switch to
main
:git checkout main
- Delete your branch:
git branch -d 42-new-branch
Updating your branch while it is still in progress
Sometimes while you are working on a branch, someone uploads some code that fixes something and you want those fixes locally. To update your local repository use the following:
- Commit or stash your changes:
git commit
ORgit stash
- Switch to
main
:git checkout main
- Update
main
:git pull
- Return to your branch:
git checkout 42-my-branch
- Rebase or merge:
git rebase main
ORgit merge main
- (Optional) If you stashed your changes, reapply them:
git stash apply
Note that for steps 5 and 6 you might have to resolve some conflicts. Usually you want to keep your own changes since they are the latest changes. If you are confused, please discuss with your team on which changes to keep.
Happy hacking!