DEV Community

Alen Varazdinac
Alen Varazdinac

Posted on

Basic Git Commands Guide And FREE Cheat Sheet

Here you will learn some basic Git commands and in the end, I am providing you with a free cheat sheet that you can download and use when necessary.

GIT WORKFLOW

Git is the most widely used open-source version control system that allows you to track changes made to files. Companies and programmers usually use Git to collaborate on developing software and applications.

A Git project consists of three major sections: the working directory, the staging area, and the git directory.

The working directory is where you add, delete, and edit the files. Then, the changes are staged (indexed) in the staging area. After you commit your changes, the snapshot of the changes will be saved into the git directory.

BASIC GIT COMMANDS

Here are some basic git commands you need to know.

CONFIGURATION

Config - Used to set user-specific configuration values like email, username, file format, etc.

git config --global user.name [name]
git config --global user.email [email]
Enter fullscreen mode Exit fullscreen mode

Config List - To check all of your configuration settings.

git config --list
Enter fullscreen mode Exit fullscreen mode

CREATE

Init - Creates a new local git repository in the current directory.

git init
Enter fullscreen mode Exit fullscreen mode

You can also create a repository within a new directory by specifying the project name.

git init [project-name]
Enter fullscreen mode Exit fullscreen mode

Clone - Used to copy a repository from a remote server.

git clone username@host:/path/to/repository
Enter fullscreen mode Exit fullscreen mode

STAGING FILES

Add - Used to add files to the staging area. You can add all files at once with the command shown below.

git add .
Enter fullscreen mode Exit fullscreen mode

Or you can specify exact file names to add in the staging area.

git add [file-name-1] [file-name-2]
Enter fullscreen mode Exit fullscreen mode

To remove files from the repository use the command below.

git add rm --cached [file-name] 
Enter fullscreen mode Exit fullscreen mode

Reset - To reset the file to the last commit's state use the following command.

git reset
Enter fullscreen mode Exit fullscreen mode

Status - Displays the list of changed files together with the files that are yet to be staged or committed.

git status
Enter fullscreen mode Exit fullscreen mode

COMMIT FILES

Commit - Creates a snapshot of the changes and save it to the git directory.

git commit -m "Write commit message here."
Enter fullscreen mode Exit fullscreen mode

Reset - This command will reset the index and the working directory to the last git commit's state.

git reset --hard HEAD
Enter fullscreen mode Exit fullscreen mode

Commit amend - Convenient way to modify the most recent commit.

git commit --amend -m "Commit message"
Enter fullscreen mode Exit fullscreen mode

PULL & PUSH

Push - Used to send local commits to the master branch of the remote repository. Replace [master] with the branch where you want to push your changes when you're not intending to push to the master branch.

git push origin [master]
Enter fullscreen mode Exit fullscreen mode

Pull - Merges all the changes present in the remote repository to the local working directory.

git pull
Enter fullscreen mode Exit fullscreen mode

Remote - Connect local repository to a remote server.

git remote add origin [remote-url]
Enter fullscreen mode Exit fullscreen mode

To delete a connection with the remote repository use command below.

git remote rm [repository-name]
Enter fullscreen mode Exit fullscreen mode

BRANCHING

Branch - List all the present branches in the repository.

git branch
Enter fullscreen mode Exit fullscreen mode

Or you can delete a branch with the next command.

git branch -d [branch-name]
Enter fullscreen mode Exit fullscreen mode

Checkout - Creates branches and helps you to navigate between them. For example, the following basic command creates a new branch and automatically switches you to it.

git checkout -b [branch-name]
Enter fullscreen mode Exit fullscreen mode

To switch branches use the command below.

git checkout [branch-name]
Enter fullscreen mode Exit fullscreen mode

Merge - Used to merge a branch into the active one.

git merge [branch-name]
Enter fullscreen mode Exit fullscreen mode

DOWNLOAD FREE CHEAT SHEET

INSTAGRAM

HTTP STATUS CODES CHEAT SHEET

Top comments (0)