Hello Sunil
Essential Git commands - Basic to advance with examples

Essential Git commands : Basic to advance with examples

When you run the “pull” command, Git will:

  • Pull changes in the current branch made by other developers; and
  • Synchronize your local repository with the remote repository

The “pull” command doesn’t create a new directory with the project name. Git will only pull updates to make sure that your local repository is up to date.

#8 git branch

Branches are, arguably, the greatest feature of Git, and they are very helpful. Thanks to branches, you can actively work on different versions of you projects simultaneously.

The reason why you will use branches lies on the surface. If you have a stable, working application, you don’t want to break it when developing a new feature.

Therefore, it’s best to have two branches: one branch with a stable app and another one for developing features.

Then again, when you complete a feature and it seems to be working, some bug may still be there. And bugs must not appear in a production-ready version. Thus, you will want to have another branch for testing.

git-branches

In the simplest terms, you will use branches to store various versions of your project: a stable app, an app for testing, an app for feature development, and so on.

Managing branches in Git is simple. Let’s first see our current branches by typing the following command:

git branch

git branch - git commands

That’s it: one command, “branch”, will ask Git to list all branches. In our project, you have only one branch – master. But an application under development is far from being complete, and you need to develop new features.

Let’s say you want to add a user profile feature. To create this feature, you need to create a new branch by typing following command:

git branch <new_branch_name>

Create a new branch for user profile feature development in our repo

Let’s run the “git branch” command once more:

By using git branch command again, you can see still you are in the master branch

Note the asterisk to the left of “master”, this asterisk marks the current branch you are in. In other words, if you create a branch and start changing code right away, you will still be editing the previous branch, not the new one.

After you have created a new branch to develop a feature, you need to switch to the new branch before you get to work on a feature.

For switching branches in Git, you won’t use a “switch” command, as you might think. Instead, you will need to use “checkout”:

git checkout <branch_name>

git checkout - git commands

Git also notifies you that you have switched to a different branch: “Switched to branch ‘user-profile'”. Let’s run “git branch” once more to prove that:

Hooray! You can now freely change any file, create and delete files, add files to the staging area, commit files, or even push files to a remote repository. Whatever you do under the user-profile branch won’t affect the master branch.

#9 git merge

But wait! we said you can do whatever you need in a new branch and it won’t change the master branch at all.

But once you finish developing a feature, how can you move it from that development branch to the master? Do you need to copy-paste?

Before we answer the questions for you, let’s first take a look at the flow when adding new branches:

  • Create a new branch to develop a new feature using “git branch <branch_name>”
  • Switch to the new branch from the main branch using “git checkout <branch_name>”
  • Develop the new feature

You are stuck on the third step. What should you do next? The answer is simple: you need to use the “merge” command.

To merge a secondary branch into the main branch (which can be a master, development, or feature branch), first switch back to the main branch. In our case, you should checkout the master branch:

git checkout master

Checkout to master branch from user-profile

The current branch is now changed to master, and you can merge the user-profile branch using the following command:

git merge <branch_name>

Git merge to master branch from user-profile

Keep in mind that you are in the main branch and you are merging another branch into the main – not vice versa!

Now that the user-profile feature is in the master branch, you don’t need the user-profile branch anymore. So let’s run the following command to delete this branch:

git branch -d <branch_name>

Delete user-profile branch using git branch -d command

With the “-d” option, you can delete now unnecessary “user-profile” branch. By the way, if you try to remove the branch you are in, Git won’t let you.

You can not delete user-profile branch while you are inside of it

Let’s mention a simpler command for creating new branches by following command:

git checkout -b <branch_name>

This one command will let you create a new “admin-panel” branch and switch to that branch right away.

#10 Miscellaneous Git commands

This section discusses miscellaneous Git commands which will be useful to perform day-to-day tasks:

git diff

The “git diff” command helps you see the differences between files and directory. If you are making changes in a file, it’s a useful tool to quickly evaluate the changes you made.

Sunil Pradhan

Hi there 👋 I am a front-end developer passionate about cutting-edge, semantic, pixel-perfect design. Writing helps me to understand things better.

Add comment

Stay Updated

Want to be notified when our article is published? Enter your email address below to be the first to know.

Sunil Pradhan

Hi there 👋 I am a front-end developer passionate about cutting-edge, semantic, pixel-perfect design. Writing helps me to understand things better.