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.
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>
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>
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
To get changes from a repository:
git pull origin master
If you ran:
git add <files>
git add .
then you realized that you made a mistake and you don't want to actually add those files, just run:
git reset <files>
This will undo adding specific
files. If you want to undo adding all files:
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 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.
To edit the last commit or the last commit message, run:
git commit --amend -m "new message"
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 <files>
To create a new branch:
git branch <NEW_BRANCH>
To switch from one branch to another:
git checkout <BRANCH_NAME>
To create a new branch and switch to it immediately, you can run:
git checkout -b <BRANCH_NAME>
To delete a branch:
git branch -d <BRANCH_NAME>
To merge a branch to another, switch to the branch you want to merge to then run:
git merge <BRANCH_NAME>
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:
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 <STASH_NAME>
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
To set the email:
git config user.email "YOUR_EMAIL"
To set the name:
git config user.name "YOUR_NAME"
To remove files from a Git Repository:
git rm <files>
To remove those files only from Git without removing them locally:
git rm --cached <files>
To remove directories just add the