New to working with a team on git and github? Start here
TL;DR Don't commit to main
, always use a branch, then create a pull request
Although it's tempting to use the GUI on github.com or on the github desktop app, the sooner you start using the command line for all git commands, the sooner you will be comfortable with it. Believe it or not, you actually have more control when using the command line and are able to do more advanced git commands. So it might be intimidating at first, but in the long run it will be faster and easier.
- Github desktop (or similar app for read-only visualization)
- iTerm (for command line git operations)
- Local is your computer. You will use the command line to navigate between folders and projects, and run git commands.
- Remote is github.com. Everyone on your team has access to all code and branches there.
- To make sure that your local is connected to the remote, run
git remote -v
in the command line, and it should show something similar to
origin https://github.com/jwicksnin/docs.git (fetch)
origin https://github.com/jwicksnin/docs.git (push)
- Your repo has one branch automatically:
main
- Never commit to
main
- always use a branch - You can see which branches are available locally by running
git branch
- In order to create a new branch, run
git checkout -b your-new-branch-name-here
- Your branch name should have no spaces and use
-
or_
or camelCase - Make all of your commits on your branch (not
main
), and when your feature is done, you can create a PR. See thePull Requests
section below.
- Cleaner and understandable git history
- Easier to reason about when merging PRs
- To learn more about rebasing and merging, see this article
-
On your local branch, not
main
, make your commits for your feature -
If you need the most recent changes on
main
(for example, your teammate merged some code that your branch depends on, or you are ready to create a PR), run the followinggit checkout main git pull origin main
Now your local
main
is up to date with the remotegit checkout your-local-branch git rebase main
This "replays" your branch's commits on top of what is in
main
-
If you run into merge conflicts, don't panic. This is normal and usually easier to resolve than it seems. And VS Code makes it easy to choose which code to keep during a merge conflict. You can search your codebase for
>>>>>
and it will find all the merge conflicts. VS Code will label each conflict and you can remove the change you don't want. -
Once all the merge conflicts are resolved and there are no more
>>>>>
in your codebase, you can run the followinggit add . git rebase --continue
This updates your commit with the changes from main. You might have multiple merge conflicts when rebasing onto
main
, and that is ok. Each one is associated with a different commit that you are replaying on top ofmain
. -
One you have replayed all your commits on top of
main
, you can rungit log
and you will see all your commits, followed by all the commits onmain
. -
Now you have the most recent changes in
main
on your local branch and can continue working or create a PR when you're ready.
-
See
Branches
andRebase and Merge
above -
Once your local branch has all of your feature changes and you have rebased onto
main
, you are ready to create a PR -
In your local branch, run
git push origin your-new-branch
which pushes your local branch to the remote. -
Go to github.com and select Pull Requests. You can select
your-new-branch
to be merged intomain
. Create the PR and assign any of your teammates to review it. -
Once your teammate has reviewed and approved your PR, go to your PR on github.com and merge your branch into
main
. If it objects that your banch is X commits behindmain
, you will need to follow the process underRebase and Merge
above to get all the changes inmain
into your branch, then push to the remote again. -
Once your PR is merged, update your local repo by running
git checkout main git pull origin main
Your local
main
branch is always a copy of what is on the remote. If there are any issues when pulling it down, you should check if you have any local changes onmain
and remove them.