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>
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>
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 thepush
command.
Get Changes from Repository
To get changes from a repository:
git pull origin master
Undo Add Command
If you ran:
git add <files>
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 <files>
This will undo adding specificfiles
. 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 thatgit 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 <files>
Create a Branch
To create a new branch:
git branch <NEW_BRANCH>
Switch Branches
To switch from one branch to another:
git checkout <BRANCH_NAME>
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>
Delete a Branch
To delete a branch:
git branch -d <BRANCH_NAME>
Merge Branches
To merge a branch to another, switch to the branch you want to merge to then run:
git merge <BRANCH_NAME>
"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 <STASH_NAME>
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 <files>
To remove those files only from Git without removing them locally:
git rm --cached <files>
To remove directories just add the-r
option.