Sitemap

Working as a Team in GitHub: Reversing Problematic Commits

What do you do when you accidentally save, stage, and push up edits on a group project in your local main branch, and find yourself in conflict with the remote main?

--

If you’ve ever worked on a group project using GitHub, you have likely found that branch management can be a challenging and anxiety-provoking task. It takes a lot of focus and intentionality to ensure you are testing and developing code in a way that will not clash with things your teammates are testing and developing at the same time. If you’re not careful, you can accidentally make changes in the wrong branch and cause some conflicts that could affect the fate of your application.

What is GitHub?

“GitHub is a code hosting platform for version control and collaboration.” This means that GitHub allows you to store repositories online, give other people collaborator access, and track committed changes (versions) over the development life of the project.

What are Branches?

In a GitHub repository, the main branch is the core branch. (Honestly it’s more of a trunk if you’re visualizing a tree.) Basically, it is where all approved updates are committed to the official version of the project.

Now if you have ever been on a group Google Sheet trying to make edits at the same time as other people, you may have witnessed the chaos that can occur as multiple users try to type into the same cell at the same time. Edits constantly conflict and progress is slow and frustrating.

In GitHub, branches off of the main are a really useful feature that allow you to make your own copy of the main branch and experiment with the code until you get it just the way you want it — all without disrupting the work of your teammates. When you feel comfortable with what you have developed, you can push the edits from your branch up to GitHub and ask to merge them into the main branch to make them official.

How do you know when to make a branch?

For all my Rick and Morty fans out there, I imagine making a branch off the main to be similar to using a Meeseeks box. I need to work on a specific feature. I create a new branch (just like the Meeseeks box creates a new Mr. Meeseeks) and give it a specific task. For example, if I wanted to make a branch to work on styling my comments form, I might type:

<git checkout -b comments-form-styling>

Just like a Mr. Meeseeks, that branch only exists until I complete my task. Once I have gotten the form styling the way I like it, I push my updates up to GitHub and request a merge to the main branch to make my updates part of the official project. Finally, just like a Mr. Meeseeks, that comments-form-styling branch disappears!

Get Becca Rozmajzl’s stories in your inbox

Join Medium for free to get updates from this writer.

If I need to work on a new feature, I create a new branch off the main just like a new Mr. Meeseeks.

What do you do if you committed something to the wrong branch?

It is bound to happen at some point that you will be working on a feature and you will stage and commit your changes in the wrong local branch. If that branch is the main, you may end up with an error message indicating that your attempted commit is in conflict with a newer version of main that exists in the remote repository on GitHub. This can be scary because you definitely don’t want to inadvertently overwrite progress that has been made by you and your teammates.

The best solution I’ve found to this is simply walking back your commits.

<git reset HEAD~1 — soft>

This little line of code non-destructively walks back your commit. Your files and their changes will still be available locally. However, they are now back to “staged” rather than “committed”.

From there, you can choose to un-stage them from the main, switch branches, and re-stage and commit them in your intended branch!

Conclusion

GitHub is a phenomenal resource for tracking the evolution of a project and for facilitating streamlined collaboration between multiple developers. It can be daunting at first to understand how to use it, but once you become more confident with branch management as well as ways to walk back mistakes, you will start to see its true beauty.

--

--