A little over a year ago, I wrote a short intro to Git. After starting my new job, I’ve found so many new uses for git and it’s become a part of my every day life. So here’s an update, with some more advanced use cases!
git stashis suuuuper useful! Sometimes, you’re not at a part of your code where you want to make a commit, but you also don’t want to lose all of your hard work, and that’s where this comes in! It lets you stash away your code so you can switch to another branch.
git stash listgives you a list of the stash entries that you currently have saved.
git stash show [stash]will show you the stashed code. You can either specify a specific stash, or if you don’t specify, it will give you the last stash you made.
git stash popwill apply the last stash that you saved and then it will delete that stash from the list, so be careful when you use this!
git addwas something mentioned in the post, but I’ll talk about some more specific use cases here:
git add .will add all changed or new files to staging
git add -pis a little bit more useful; it interactively shows you chunks of code for you to review before deciding to add to staging. This way, if you accidentally left some comments for yourself in your code, you’ll be able to catch it before you commit.
git branchcan be used to list, create, or delete branches
git rebaseis a super useful tool, though it can get a little hairy and complicated, depending on the magnitude of your project. Rebasing is the process of applying a sequence of commits (often from master) to another base branch. It’s especially useful for when you’re working on a project with other people and there are multiple different branches getting merged into the project.
Tips I’ve Learned
- Rebase often! When I first started rebasing, my code ended up all over the place. Part of it was because I waited far too long to rebase, and by the time I did try to rebase, my local code had branched so far away from what was on master, that it was hard to figure out what code needed to be kept and what code had been changed. If you do them in smaller batches, more often, it’s a lot easier to control. Rebasing, in general can be a difficult concept to grasp, so if you start practicing with smaller chunks of code, that may make the process of learning a little easier.
- Don’t create a branch of a branch of a branch of a… when I first started working with others, I would just branch off the branch I was currently working on. It seemed reasonable to me, because this is my most up to date code, and maybe I’m adding a feature on top of the last feature I just finished. However, it may not be the project’s most up to date code. You always want to try to branch off of master, and to stick as close to master as you can. If you get too far down that branch tree, it will be incredibly difficult to find your way back to the main project.