Git 101
Tags: github, git, bitbucket
Published: 2022-12-14 11:13:00

---
title: Git 101
published: true
date: 2022-12-14 11:01:17 UTC
tags: github,git,gitlab,bitbucket
canonical_url: https://medium.com/@salonii13/git-101-35ecc6c7cf7b
---

Let's understand Git with its basic commands.

![git](https://cdn-images-1.medium.com/max/1000/0*iFApAuNplslAErbr)

Hello geeks, 🙋🏻‍♀️

This article will list the Git commands you need in the daily development lifecycle while working with any version control systems.

Version Control Systems like GitHub, Gitlab, BitBucket etc., use Git commands to manage code repositories.

#### Before reading this article, you must know

1. What is Git, Git how does it work
2. Version control systems (VCS)
3. Repositories

#### Let's understand Git commands one by one.

1. Setup

- Download Git from [here](https://git-scm.com/downloads), according to your OS.

2. create/ initialize a git repository

`git init` , this command will initialize your folder as a git repository.

3. check out the existing repository

`git clone /path/to/repository` , this command will create a copy of your remote repository in your local system.

If your repository is hosted privately on VCS, it will ask you for your username and personal access token. (Asking for passwords is deprecated.)

You can check your VCS doc to find out where to get the personal access token .

4. Local vs remote repositories

your local repository consists of three "trees" maintained by git. Git first one is your `Working Directory` , which holds the actual files. The second one is the `Index` which acts as a staging area, and finally, the `HEAD` which points to the last commit you've made.

Your commits will be stored locally until and unless you push it.

5. add files and commit code changes

After making changes in your local repo, you need to stage those changes. And for that, the command is `git add <filename>` .

Similarly, you can use `git add .` , add all the files that have changes.

after that, you need to fire `git commit -m “commit message”` , which will commit your changes to `HEAD` . Yet, your changes remain to go on the remote repo.

6. Push changes to remote repositories

Your changes are now in the `HEAD` of your local working copy. To send those changes to your remote repository, execute `git push origin <branch\_name>` .

If you have not cloned an existing repository and want to connect your repository to a remote server, you need to add it with `git remote add origin <remote_url>` . Now you can push your changes to the selected remote server

7. Branches

Branches are used to develop isolated features(independent from each other) and work with other developers on the same repository.

So, work on different branches and merge them into a hosted branch once the work is done.

Create a new branch named "develop" and switch to it using  
`git checkout -b develop` .

to `delete branch git branch -d feature_x` .

If you are creating a branch locally, it will not be available for others unless you push it. Use `git push origin <branch>` to push the branch to your remote repository.

8. Pull and merge

To fetch the latest changes on your remote repo, `git fetch` .

To update your current branch with the latest commits, `use git pull` .

To get another branch's code into your branch `git merge <branch>` .

If you and your colleague have worked on the same line in the same file, there will be merge conflicts in your file. You need to take either your changes or your colleague's changes, or you can take both. Then you need to again stage the file with `git add <filename>` . And it will be marked as merged.

Before merging changes, you can also preview them by using  
`git diff <source_branch> <target_branch>`

9. Tagging

It's recommended to create tags for software releases. You can create a new tag named `1.0.0` by executing `git tag 1.0.0 <commit_id` .

That's all.

These are all the basic commands required to start using Git with VCS.

Bye. 👋🏼

Keep Coding, Keep committing, and Keep pushing 💻

#### About me👩🏻‍💻

I am a Backend Developer at [DhiWise](https://www.dhiwise.com/?utm_campaign=blog&utm_source=devto&utm_medium=cms&utm_term=saloni_pod2&utm_content=git).

You can follow me on [LinkedIn](https://www.linkedin.com/in/saloni-saraiya/) and [Twitter](https://twitter.com/salonii131) for more tech updates.