DEV Community

Cover image for Git Basics Simply Explained For Beginners
Shahed Nasser
Shahed Nasser

Posted on • Originally published at blog.shahednasser.com

Git Basics Simply Explained For Beginners

This article was originally published on my personal blog

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:

git clone <REPOSITORY_GIT_URL>
Enter fullscreen mode Exit fullscreen mode

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 <REPOSITORY_GIT_URL>
Enter fullscreen mode Exit fullscreen mode

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

git push origin master
Enter fullscreen mode Exit fullscreen mode

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

git pull origin master
Enter fullscreen mode Exit fullscreen mode

Then run thepushcommand.


Get Changes from Repository

To get changes from a repository:

git pull origin master
Enter fullscreen mode Exit fullscreen mode

Undo Add Command

If you ran:

git add <files>
Enter fullscreen mode Exit fullscreen mode

or

git add .
Enter fullscreen mode Exit fullscreen mode

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

git reset <files>
Enter fullscreen mode Exit fullscreen mode

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

git reset
Enter fullscreen mode Exit fullscreen mode

Undo Latest Commit

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

git reset ~HEAD
Enter fullscreen mode Exit fullscreen mode

You can also run:

git revert HEAD
Enter fullscreen mode Exit fullscreen mode

The difference is thatgit revertwill 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"
Enter fullscreen mode Exit fullscreen mode

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 .
Enter fullscreen mode Exit fullscreen mode

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

git checkout <files>
Enter fullscreen mode Exit fullscreen mode

Create a Branch

To create a new branch:

git branch <NEW_BRANCH>
Enter fullscreen mode Exit fullscreen mode

Switch Branches

To switch from one branch to another:

git checkout <BRANCH_NAME>
Enter fullscreen mode Exit fullscreen mode

Create a New Branch And Switch To It

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

git checkout -b <BRANCH_NAME>
Enter fullscreen mode Exit fullscreen mode

Delete a Branch

To delete a branch:

git branch -d <BRANCH_NAME>
Enter fullscreen mode Exit fullscreen mode

Merge Branches

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

git merge <BRANCH_NAME>
Enter fullscreen mode Exit fullscreen mode

"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
Enter fullscreen mode Exit fullscreen mode

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

git stash pop
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Then, you can apply a stash from the list:

git stash apply <STASH_NAME>
Enter fullscreen mode Exit fullscreen mode

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--globaloption.

To set the email:

git config user.email "YOUR_EMAIL"
Enter fullscreen mode Exit fullscreen mode

To set the name:

git config user.name "YOUR_NAME"
Enter fullscreen mode Exit fullscreen mode

Remove Files From a Git Repository

To remove files from a Git Repository:

git rm <files>
Enter fullscreen mode Exit fullscreen mode

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

git rm --cached <files>
Enter fullscreen mode Exit fullscreen mode

To remove directories just add the-r option.

Top comments (0)