Basic Git Commands Guide And Cheat Sheet

  • by
Basic Git Commands Featured Image

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 consist 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]

Config list – To check all of your configuration settings.

git config --list

Create

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

git init

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

git init [project-name]

Clone – Used to copy a repository from remote server

git clone username@host:/path/to/repository

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 .

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

git add [file-name-1] [file-name-2]

To remove files from repository use the command below.

git add rm --cached [file-name]

Reset – To reset the file to the last commit’s state use the following command.

git reset

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

git status

Commit Files

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

git commit -m "Write commit message here"

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

git reset --hard HEAD

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

git commit --amend -m "Commit message"

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]

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

git pull

Remote – Connect local repository to a remote server.

git remote add origin [remote-url]

To delete a conneciton with remote repository use command below.

git remote rm [repository-name]

Branching

Branch – List all the present branches in the repository.

git branch

Or you can delete a branch with next command.

git branch -d [branch-name]

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]

To switch branches use the command below.

git checkout [branch-name]

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

git merge [branch-name]

DOWNLOAD FREE CHEAT SHEET


Contact me if you have any questions or need a website.