Git Basics Simply Explained For Beginners
Tags: beginners, git, programming
Published: 2021-04-02 11:00:14

_This article was originally published on [my personal blog](https://blog.shahednasser.com/git-basics-for-beginners/)_

Learning Git is very important for every developer. And for many beginners, it can be very confusing, as well.

In this article, I'll go over the basic Git commands you'll need to know as a beginner in a simple manner.

---

### Cloning a Repository

This one is simple in itself. You just created a repository and you want to clone it on your machine. Just run:

```bash
git clone &lt;REPOSITORY_GIT_URL&gt;
```

---

### Adding a Repository Into an Existing Project

Let's say you already are working on a project and decided later on to create a repository for it. How do you add the repository to it?

```
git init
git add .
git commit -m "Initial Commit"
git remote add origin &lt;REPOSITORY_GIT_URL&gt;
```

If the repository you are adding is empty, you can just run:

```
git push origin master
```

If, however, the repository has some files in it, I suggest you run this first:

```
git pull origin master
```

Then run the`push`command.

---

### Get Changes from Repository

To get changes from a repository:

```
git pull origin master
```

---

### Undo Add Command

If you ran:

```
git add &lt;files&gt;
```

or

```
git add .
```

then you realized that you made a mistake and you don't want to actually add those files, just run:

```
git reset &lt;files&gt;
```

This will undo adding specific`files`. If you want to undo adding all files:

```
git reset
```

---

### Undo Latest Commit

You made a commit, then realized something is wrong with it. To undo it, simply run:

```
git reset ~HEAD
```

You can also run:

```
git revert HEAD
```

The difference is that`git revert`will add a new commit that reverts the latest commit. It's probably more helpful if you've pushed the commit that you want to undo.

---

### Edit Latest Commit

To edit the last commit or the last commit message, run:

```
git commit --amend -m "new message"
```

---

### Remove Local Changes

If you made changes locally and you don't want them anymore for one reason or another, you can revert them back to their latest version in your Git Repository by running:

```
git checkout .
```

To remove local changes of specific files instead of all changes you can run:

```
git checkout &lt;files&gt;
```

---

### Create a Branch

To create a new branch:

```
git branch &lt;NEW_BRANCH&gt;
```

---

### Switch Branches

To switch from one branch to another:

```
git checkout &lt;BRANCH_NAME&gt;
```

---

### Create a New Branch And Switch To It

To create a new branch and switch to it immediately, you can run:

```
git checkout -b &lt;BRANCH_NAME&gt;
```

---

### Delete a Branch

To delete a branch:

```
git branch -d &lt;BRANCH_NAME&gt;
```

---

### Merge Branches

To merge a branch to another, switch to the branch you want to merge to then run:

```
git merge &lt;BRANCH_NAME&gt;
```

---

### "Stash" Your Local Changes

Sometimes you might have changes locally, but you are not ready to commit them. If you need to work on something else in the meantime, go back to the original state of the repository, or change branches without losing changes, you can "stash" those changes for later:

```
git stash
```

Then, when you want to pull out those changes again, just run:

```
git stash pop
```

Running this command will apply the latest changes you put in the stash and then remove it from the stash.

If you have a lot of changes that you've "stashed", you can check them by running:

```
git stash list
```

Then, you can apply a stash from the list:

```
git stash apply &lt;STASH_NAME&gt;
```

---

### Set Your Email and Name In The Configurations

You can do this on a global scope and on a repository's scope. These commands will work on a global scope just by adding the`--global`option.

To set the email:

```
git config user.email "YOUR_EMAIL"
```

To set the name:

```
git config user.name "YOUR_NAME"
```

---

### Remove Files From a Git Repository

To remove files from a Git Repository:

```
git rm &lt;files&gt;
```

To remove those files only from Git without removing them locally:

```
git rm --cached &lt;files&gt;
```

To remove directories just add the`-r` option.