This is just one way of using git/GitHub. But I have been using this same basic flow for 15 years and I rarely have trouble.
-
Switch to the
mainbranchgit switch main -
Pull the most recent version of
mainfrom GitHubgit pull origin main -
Checkout a new branch to begin my work
git switch -c th/some-new-work-i-am-starting -
Add the new work to the staging area
git add . -
Check the work before committing it to my new branch
git diff --staged -
Commit the work to the branch
git commit -m "A brief message explaining what I'm changing" -
Push the branch to GitHub
git push origin th/some-new-work-i-am-starting -
Open a draft Pull Request on GitHub for the new branch so my team can see what I'm working on
-
Flag the Pull Request as "Ready for Review"
Sometimes you will need to deal with merge conflicts. Here's how I do that.
-
Checkout the branch that cannot be merged with main.
git switch th/some-branch-with-a-problem -
Fetch the latest changes to the main branch.
git fetch -
Rebase the branch with the issue.
git rebase origin/main -
Usually, git tells you that the rebase is incomplete because of a conflict. Resolve conflicts by opening each file that is flagged with
CONFLICTin the error message from git. -
One by one, find the sections of the file where the conflict occurred and edit the file - removing the old changes and keeping the correct changes.
-
After all the files with conflicts are repaired, add the files to the staging area to tell git that the conflicts are resolved.
git add . -
Now continue the rebase process.
git rebase --continue -
If the rebase completes without any
CONFLICTmessages, push the rebased branch up to GitHub and the merge conflict on GitHub should disappear.git push origin th/some-branch-with-a-problem -f
Sometimes git will throw this error when you are trying to pull (or checkout) a branch but you have made some changes to files locally without committing those changes. git stash is useful here.
-
When you see an error like this...
git checkout main error: Your local changes to the following files would be overwritten by checkout: README.md Please, commit your changes or stash them before you can switch branches. Aborting -
Add the files you've changed to the staging area
git add . -
Stash the changes
git stash -
Try the checkout command again
git checkout main -
Bring the stashed changes back
git stash apply
