Gitting Better

Shreyan DasShreyan Das
4 min read

Alright, with the all the basics on the tips of my fingers, it's time to roll up my sleeves and get down to the nitty-gritties, the nooks and crannies, the corners and crevices of git. It's time to branch out and skill up. And speaking of branches, the most logical next step would be to learn about git branches, don't you agree?

Branches are the inner beauty of git. They are a mechanism that allows you to create a copy of the existing repository and mess around here without inflicting so much as a scratch on the main code. To keep production servers unscathed, to test features in a sandbox, to cross-collaborate with coders in parallel, there could be no better feature!

1. Checking remotes

Before diving into branches, let's take a quick peek at our remotes.

git remote show origin

This would give you a detailed view of different branches that exist on your origin remote url.

For a concise view, use:

git remote -v

2. Change remote url

If remote urls need to be updated, you could set a remote url with an alias using the following command.

git remote set-url origin <new-url.com>

Note: The argument origin is an alias for the remote branch. You could use any other name you like (really, bazinga would do just as well ๐Ÿ˜‹ iukuk).

3. Working on a branch

3.2. Creating a new branch

To create a new branch, use:

git branch test_branch

When a new branch is created, you don't automatically get inside of that branch. You have to get on that branch first if you want to make changes on that branch.

3.3. Switching branches

To change to the new branch, use:

git checkout test_branch

Here's the fun part. Go to town, make a load of mess here. Once you've made all changes you wanted to make, add your changes to the cache (aka staging layer) and commit as you would normally.

Time for the magic, if you go back to the main branch using

git checkout main

you'd find that your main branch was as you left it. Now switch back to your new branch.

git checkout test_branch

3.3. Push changes to a new branch on your remote repository

Supply the name of the new branch instead of main to push changes to your remote repo on a separate branch.

git push origin test_branch

4. Merging branches

4.1. Switch out of your branch

If there are two branches you want to merge, you'll first have to switch out of that branch and into the branch you want to merge those changes into (typically, that would be your main branch). Before you do that, ensure you've committed all changes, and preferably also pushed the changes to your remote repo. Then go into the branch you want to merge these changes with. For the sake of simplicity, I'll assume it's main.

git checkout main

4.2. Update your branch (optional)

It's a good idea to catch this branch up to speed. If others are working on a project with you, it could be that your remote main branch is ahead by a few commits by the time you've finished making your changes. Update your main branch to ensure you don't accidentally overwrite those commits.

git fetch origin main

4.3. Merge the branch

The command is simple.

git merge ignore_test

4.4. Resolve conflicts (if any)

This would be a manual process. Git will tell you if the merge has failed due to any conflicts in different versions. If there are any conflicts, now is the time to resolve them.

4.5. Pushing merged branch to remote

Finally, your changes are ready to be pushed. Add changes and commit those changes on your main branch. Once you're happy, you are set to push changes to the remote url using.

git push origin main

5. Removing the local branch

With your changes merged, the local test_branch has served its purpose. Our friendship with this branch has run its course, and it's now time to say good-bye. You can remove it from your local repository using

git branch -d test_branch

Make sure you've already pushed your changes to the remote branch!

Go ahead, give it a whirl! And while you're out there playing around on GitHub, why don't you also give me a follow? You can find me here:

Until next time!

10
Subscribe to my newsletter

Read articles from Shreyan Das directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Shreyan Das
Shreyan Das