Hopefull you have read my previous article on the basics of git. This article will talk about how to use git on the command line. Hopefully this is not too scary, please email me if you have any questions.
Commits
Let's say you're working on the basic pushbot code. You want to add forward/backward movement first, then turning.
# first, write your initial drive code
# after testing that basic driving works:
git add src/drive.cpp
git commit -m "Add basic forward/backward movement"
# later, you add turning functionality
# after testing that turning works:
git add src/drive.cpp
git commit -m "Add left/right turning functions"
Think of commits like save points. Each commit represents a working state of your code. If your turning code breaks something, you can always go back to the previous commit where just driving worked.
Pulling
Before starting any work, you have to sync your local (on your computer) repo with the remote repo (GitHub). To do that, you have to run git pull. Before starting your work for the day always run:
git checkout master
git pull origin master
After running this you will have a up-to-date version of the master branch and you are ready to work.
Common Mistakes
If you forget to pull and make changes run:
git pull origin master
You might see a bunch or warnings or messages about merge conflicts! This happens when git is trying to sync the files from the repo and finds them to have clashing changes before they merge together. To fix them:
- Open the files with conflicts with your editor
- Look for
<<<<<<< HEAD
markers - Decide which code to keep
- Save and commit the fixed files
The nuclear option: You can delete your changes by resetting the branch. This is outside the scope of this basic guide, but you can shoot me an email or ask me in person.
TODO Stashing
TODO Pushing
Branches
Here's a practical example of branches:
Let's say you had a main/master branch that has all of the features for a basic pushbot (driving, turning). Later, your team decides that they want to experiment with a claw design. This would be a good time to make a new branch.
# start from the master/main branch
git checkout master
# create your new branch
git checkout -b feature/clawbot
Now you can safely make commits without affecting the master branch. If you mess up the clawbot branch, you can delete it without breaking the stable part of your code that you know already works.
In summary here are the benefits:
- Safety
- Easy to abandon/delete
- You can work in parallel on a feature while a teammate is doing something else
Here's a final example of a branch workflow:
# create branch
git checkout -b feature/clawbot
# make changes, test changes, add and commit them
git add src/clawbot.cpp
git commit -m "Add clawbot feature"
# push feature branch to share with others on remote repo
git push origin feature/clawbot
# once you're done, you can do a pull request to merge it into master
git checkout master
git merge feature/clawbot