Git Repositories & Branching
Slowly over time, I've realised that the best way to make progress in CS / Data Science research is to maintain a GitHub repository with clean documentation and code.
Code is everything. If your code is messy, then your brain is messy and your results are messy as well. Plus there's always this "what if there's a small bug in my code?" doubt that lingers in your mind incessantly. Hence, to counter that, maintain a GitHub repository and commit to it ONLY when you're absolutely sure that your code is correct. This way you have a "ground-truth" as to what's real.
Enough philosophy.
Sometimes, one branch isn't enough when you're collaborating with a bunch of people or you're working on different features or you just want to "archive" a specific "version" of the system...
Recently I was working on a benchmarking system. The repository had only one main branch. And that was enough for a while. Two of us were working on the project and we both ensured that the filetree was clean and that we were writing readable commit messages.
Then, a couple of collaborators joined the project and started to make commits to the main branch. This messed up the file structure of the branch and the "system" that was built in the main got obscured by a bunch of other things that were meant to perform different functions. The working system got buried under unrelated code.
That's when I decided to create a branch from the main and name it benchmarking-v1 and moved the entire working system to this new branch (there were a few other bugs I had to fix).
So, here's the process without any further ado:
Run a
git statusjust to check which branch you're on and whether you're upto date with theoriginor not.To create and switch to a new branch, run
git switch -c branch-namein my case this wasgit switch -c benchmarking-v1. This creates a new branch namedbenchmarking-v1and moves you onto it. The-cmeans “create”.
- It is important to note the following here:
- The new branch starts from your current location. So if you are currently on
main, thenbenchmarking-v1will start from the currentmaincommit. - Your uncommitted changes usually come with you onto the new branch. So if you already made code changes before creating the branch, this command is usually safe and useful.
- The new branch starts from your current location. So if you are currently on
You can verify your current branch with:
git branch --show-current.Make whatever changes you need to make and follow the normal Git Workflow from here on out:
- Git Add:
git add . - Git Commit:
git commit -m "benchmarking-v1 branch first commit" - Git Push:
git push -u origin benchmarking-v1-usets the upstream branch i.e. your local branchbenchmarking-v1becomes connected to the GitHub branchorigin/benchmarking-v1.
And there you have it folks, this is how you can easily create and switch to a new branch.
To list existing branches do git branch. Here's my output:
* benchmarking-v1
benchmarking-v2
main
(END)
To go back to the main branch, you just have to run git switch main.
Hopefully now you shouldn't be afraid of branches.