Git Tips and Fixes

These are some tips and fixes you can use when working with Git.

Here is the short version to for quick copy/paste. With all of the git commands, paste one line at a time and press enter each time.

git checkout main
git pull
git checkout -b yourBranch

Do your work making sure to Commit your changes. Do the following on your branch:

git push
OR, if it was the first time
git push --set-upstream origin yourBranch

Then move to the main branch to merge.

git checkout main
git merge yourBranch
git push

Long Version

This is the process with detailed info.

Step 1: Get the Most Recent Version of the Main Branch

It’s important to first make sure you are up-to-date with the main branch on GitHub. Start by making sure you’re on the main branch.

git checkout main

Then pull in the latest changes from GitHub

git pull

Step 2: Create Your New Branch

This is the branch where you will do your work. In the code below replace branchName with the name of your branch.

git checkout -b branchName

Note: the branch name can’t have spaces. It should be short and descriptive. When on a team people often add their name or initials.

Step 3: Do your work

Work on this branch, making sure to commit your changes.

You can use the Codespaces (VS Code) interface to do your commits as before.

Step 4: Push your branch to GitHub

When you have made your changes and committed them, you can push your branch up to GitHub.

git push

NOTE: if this a brand new branch and you have not pushed it before you will see the following error message.

fatal: The current branch basic-tag-collection has no upstream branch.
To push the current branch and set the remote as upstream, use

–set-upstream origin nameOfYourBranch

To have this happen automatically for branches without a tracking
upstream, see ‘push.autoSetupRemote’ in ‘git help config’.

To fix it, copy the part of the error message that starts with git push and paste it into the Terminal and press enter.

git push --set-upstream origin yourBranchName

Some browsers may ask your permission before allowing the paste.

Now you should be able to see the new branch on GitHub.

Step 5: Merge your branch into Main

There are two ways to do this: (1) in Codespaces/VS Code or (2) on GitHub

1: Change back to the main branch

git checkout main

2: Merge your branch

git merge yourBranchName

3: Push your changes to main to GitHub

git push

1: Go to your repository on GitHub

2: If you just made your changes, you should see a note on the GitHub main repository page with a button to Compare & pull request

3: Click the button

4: A pull request will open with a title that will be what you wrote when you made the commit on your branch. There will also be a space for a comment (optional).

If you are able to merge with no issues you will see a note that reads Able to merge. These branches can be automatically merged.

Click the Create pull request button.

5: GitHub will check if there are any conflicts. If not you will see a Merge pull request button. Click the button to complete the merge request.

6: Complete the merge. There will be another button Confirm merge. Click that button.

You should be done at this point.

If you have been working on your branch for a while and your teammates have also been working and updating main, you will get further away and more likely to have merge conflicts. You can ease this by getting the latest changes from GitHub and merging to your branch.

Checkout the main branch

git checkout main

If there are changes you don’t have then Git will say you are behind. Something like “Your branch is behind ‘origin/main’ by 3 commits” Pull the changes

git pull

Go back to your branch

git checkout yourBranch

Merge main into your branch

git merge main

If your main branch has gotten too different from the main branch on GitHub, then you won’t be able to successfully do git pull in Codespaces. This shows how to fix that by overwriting the main in your Codespace with the main from GitHub.

git checkout main
git fetch
git reset --hard HEAD

Leave a comment