DEV Community

Cover image for I can't Git no, satisfaction
Vincent Baylon
Vincent Baylon

Posted on

I can't Git no, satisfaction

As developers, we break and fix code all day. But there might be a time when you completely mess your code up. That's when a VCS (Version Control System) comes in handy. A VCS keeps track of all the changes you make to your code, as long as you are committing often. The most popular VCS we use is Git. So if you get to a point where you can't save your code, you can use your VCS and revert back to any past commit.

Git ctrl-z meme

A git workflow I really like is the Feature Branch Workflow. The idea behind this workflow is that each feature development will be done on dedicated branches. This allows each developer to work on their own feature without worrying about other developers or disturbing the main branch.

Another advantage of this workflow is being able to utilize pull requests. To merge your branch to the main branch, you initiate a pull request. This allows other developers to look over your code, discuss, even ask you to make changes and then finally sign-off that it's ready to be merged to the main branch.

. . .

Feature Branch Workflow

With this workflow, you will be making a branch for every feature you are developing. Then when you complete the feature, you will submit a pull request to have it added to the main branch. This might seem tedious, why not just have your own dedicated branch where you work on different features? Performing pull requests and merging each feature will make it easier to review code and resolve merge conflicts as there won't be hundreds of lines of code to sort through. Here's how this workflow will look.

First

you want to make sure you have the latest code to create your branch.

git checkout main
git pull origin
git reset --hard origin/main
Enter fullscreen mode Exit fullscreen mode

This switches your local repo to the main branch, pulls the latest code from origin, then resets the HEAD of your local copy to match origins.

Then

create a new branch for the feature you are building.

git checkout -b login-page
Enter fullscreen mode Exit fullscreen mode

This creates a branch called login-page and switches you to it at the same time.

And then

push your new branch to your remote repository so you will have a backup and if you are collaborating, other devs will have access to your changes.

git push -u origin login-page
Enter fullscreen mode Exit fullscreen mode

The -u flag sets this branch as your remote tracking branch. Meaning you can just type git push to push up your changes.

As

you work, remember to commit often so you can revert back if needed.

git status
git add .
git commit -m "descriptive commit message"
git push
Enter fullscreen mode Exit fullscreen mode

Finally

when you're done with your feature, do a final git push and create a pull request on your remote repository to start the process of getting your branch merged into the main.
Github pull request

Github open pull request
It will tell you if you are able to merge into the main so you can take care of any merge conflicts.

Github pull request interface
Once you create the pull request, other devs can make comments, request changes, and eventually merge pull request which will merge your branch into the main.

Github pull request merged
Now your branch can be deleted and you can start on your next feature!

Conclusion

I really like this workflow because it keeps the main branch clean. If you aren't using a git workflow with your team I recommend trying this out as it will help prevent/minimize the git issues we all run into. Happy coding!

Top comments (0)