If you are like me, the steps of pushing and pulling onto GitHub can be very confusing and also very tedious. However, this is for a good reason. When we have such a large codebase, any change we make can drastically change how the code works.
To continue further, we have the following things already established:
- You have set up a repository (repo for short) in GitHub
- You cloned the repo into VSCode or IDE of choice
- You are able to make changes to the file you selected
Once you have all of the previous steps completed we can begin editing our file!
Enter the command:
git switch main
This will ensure that your terminal starts at the main branch. This is important because it makes sure that you are not in another branch making changes.
Then enter:
git pull
This will “pull” or extract the file changes from GitHub to your local copy of the file. This ensures that your local copy is up to date with any changes made to the GitHub repository.
To ensure we do not cause any unwanted changes to the main branch, we must make a branch that we apply changes to without any risk:
git switch -c name-of-branch
Now you can edit the file! Honestly, you could have edited after git pull, but this is considered the more proper way of editing code.
Once you're done modifying the file, now we have to “push” (or as I view it uploading it to GitHub):
git add name-of-file
git commit -m "Quick Description of the changes made"
git push -u origin name-of-branch
These commands add the name of the file to the branch with a brief comment of the changes made. Finally, the branch is pushed to GitHub, where you can see the changes visually! Now you should get a message asking to pull and merge the branch to the main branch, which you should do if you are happy with the changes made.
Now our local repo is out of sync! This means we have the updated repo on GitHub but not our local repo. To fix this we must go back to the main branch and pull everything once more. Finally we must delete the branch and we are set:
git switch main
git pull
git branch -d name-of-branch
To make edits, just follow the steps once more!
