Git Hygiene

Betsy Groton
4 min readApr 22, 2021

Good Git habits for new-ish developers

Photo by Mélissa Jeanty on Unsplash

Hello hello! Learning how to use Git can be…intimidating. But having good Git hygiene is super important, especially when you’re working on a team or in a professional setting. By starting with good habits, you’ll be in a much better position down the line. Let me try to make it a little easier for you.

In my coding bootcamp (the Grace Hopper Program at Fullstack Academy), we were working with a partner constantly. I wrote down my git workflow on the very first day of class and it has yet to leave me astray!

Let’s start with the basics: Getting a repo from Github (or another site) onto your local environment.

  • If you want to work off a specific repository, simply: git clone <url from Github>
  • If you want to copy the repository to your Github, fork the repo and then: git clone <url from Github>
  • Next, if you’re working with a partner (they should follow the same steps above), you need to add them to your repository: git remote add <partner name> <partner url>
  • To check that they’ve been added, use: git remote -v and you should see their name appear

Great! We’re ready to rumble. Next, let’s talk about making commits and pushing to Github.

  • To check the “status” of your repo (i.e. if you have changes you need to add or if you have items staged/ready to commit): git status
  • To add all files changed: git add .
  • To check you’ve added everything: git status (everything should be green)
  • To type your wonderfully semantic commit: git commit -m “<message here>” Side note: usually try to label my commits with “feat”, “fix”, “chore”, “test”, but that’s a personal preference. No matter what, you want to have descriptive commit messages so you know exactly what you changed in each commit!
  • To push your commit to Github: git push origin main
  • *If you’re working with a partner and it’s time to switch drivers, the second partner would now do: git pull <partner name> main

Ok let’s get a little more complicated. Let’s talk about branches! Branches may seem complicated in the beginning, but once you get the hang of them, you’ll be making branches left and right.

  • To look at all your branches: git branch -a
  • To make a new branch: git branch <branch name>
  • To switch to your branch: git checkout <branch name>

And that’s pretty much it! Remember to create your branch from the main branch (otherwise things might start getting a little trickier).

Ok, last topic: Merge conflicts! During my time in Grace Hopper, whenever I worked on a team and we encountered a merge conflict, we would always merge the branch into main and work together using the Github UI to solve the conflict. This ended up being a rather messy process, which usually resulted in breaking parts of the app we were building, or (worst-case scenario) losing code that someone worked on for 3 days. It was not pretty and it was not efficient.

After working as a software engineer in a professional setting, I now know a better way to solve merge conflicts — on my own and without trying to merge my branch into the main branch! Here’s how it goes: when I make a pull request and I see I have a merge conflict, I switch to my main branch. I pull the main branch to make sure it’s updated with the most recent changes. I switch back to my other branch and I merge the main branch into this branch. This is where I resolve the conflicts in my vscode/code editor. Once I’ve resolved the conflicts (and run the code/tests, of course), I make a commit and push it to my branch. On Github, my conflicts should be gone and I should be able to make a seamless/conflict-free merge! By doing it this way, I know I am going to merge fully working code on my branch into the main branch.

Here are the merge conflict resolution steps broken down (pretend I’m working on a branch called betsys-branch):

  • To switch to the main branch: git checkout main
  • To pull most recent main changes: git pull origin main
  • To switch back to my branch: git checkout betsys-branch
  • To merge main with my conflict-y branch: git merge main

*Your console will tell you that you’ve got a conflict and which files you need to go to in order to fix the conflict. Time to get editing! Once you’re done with your edits, running the code, and running the tests:

  • To commit your changes: git commit -m “chore: solved merge conflicts”

*Side note: sometimes my console ends up in VIM — if yours does this, you can type “i” and then escape key (esc) and then “:wq” … then you should be able to make your commit

  • To push changes: git push origin betsys-branch

Great! Now you’re a Git expert ;) Hope you enjoyed my Git tips! Happy coding!

--

--

Betsy Groton

Hey, I’m Betsy! I’m a full-stack software developer. I also love traveling, running, rollerskating, and puppies.