# Git for Beginners: Basics and Essential Commands

Hey Everyone,

In the previous blog, I talked about what is inside **Git**. If you haven’t checked it out yet, give it a read for a better understanding.

Git is a tool to track changes, store history, and allow multiple developers to collaborate safely.

### Now let’s start with the basic Git commands 🚀

To start with the git repository, we need to first initialize it and the command for it is below

```csharp
git init
```

### Checking the Status

It will create a `.git` folder in your project folder.

To see the current state of your project — like which files are untracked or changed — we use:

```less
git status
```

### Staging the Changes

Once you know what has changed, you need to **stage** the files. for that we run below commad

```csharp
git add filename // for single file

git add . // for all files
```

### Committing the changes

After staging, you need to **capture a snapshot** by committing:

```csharp
git commit -m "" // for message
```

The `-m` flag is used to write a commit message that describes what changed.

The basic workflow looks like this:

working in directory → staging → commit

### Branching

To create a new in your repository, you will run the following command

```csharp
git branch branchname // for creating a branch
git checkout branchname // for changing your branch to new branch you created

// there is also a shortcut to perform the above two steps in one step
git checkout -b branchname // this will create and checkout to the created branch
```

### Stashing

Suppose you were working on something but you have to change it on different branch for that you have commit it or remove all change

```csharp
git stash
```

it will temporarily saves your uncommitted changes and cleans your working directory so you can switch branches safely.

### Commit History

To know the history of the commits, we run

```csharp
git log
```

This command shows a list of commits in reverse order (newest at the top).

### Difference in Commits

Sometimes you don’t want to just *see* the commit history — you want to see **what actually changed** between commits. For that, Git gives us the `diff` command.

```csharp
git diff
```

This shows what changed compared to the last commit.

### **Pull**

To pull the latest changes from the git repository

```csharp
git pull branchname //git pull origin main
```

### **Push**

To pull the latest changes to the git repository

```csharp
git push
```

Merging

To merge the code from one branch to another branch

```csharp
git merge branchname // this will merge the code from the branchname to your current branch
```

And yeah, these are some basic commands, will probably add more commands. 😊

Thanks for reading, and see you in the next blog!

Peace ✌️ and Happy Learning!
