Basic GIT Commands – Complete Guide

Do you need to learn some basic commands? You have come to the right place. Read on to discover our handy cheat sheet that you can use as a daily reference.

Let us begin!

Understanding how GIT works

GIT is the open source VCS (version control system) that allows you to track changes made to files. Businesses and programmers often use GIT to collaborate on software and application development.

A GIT project consists of three main sections: the working directory, the preparation area Y the git directory.

The working directory is where files are added, deleted, and edited. The changes are then staged (indexed) in the staging area. After you commit your changes, the snapshot of the changes will be saved to the git directory.

Anyone can use GIT as it is available for , , and . The software can have a steep learning curve, but there are many available to help you.

Basic Git Commands

Here are some basic GIT commands you should know:

  • git start will create a new local Git repository. The following Git command will create a repository in the current directory:

git start

  • Alternatively, you can create a repository inside a new directory by specifying the project name:

git start

  • git clone is used to copy a repository. If the repository is on a remote server, use:

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

  • Conversely, run the following basic command to copy a local repository:

git clone /path/to/repository

  • git add used to add files to the staging area. For example, the following basic Git command will index the temp.txt file:

git add

  • git commit will create a snapshot of the changes and save it to the git directory.

git commit –m “The message that accompanies the commit goes here”

  • Note that committed changes will not make it to the remote repository.

  • git config can be used to set user-specific settings, such as email, username and format type, etc. For example, the following command is used to set an email:
See also  How to Update WordPress: Step by Step Guide

git config –global user.email youremail@example.com

  • The -global option tells Git that you are going to use that email for all local repositories. If you want to use different emails for different repositories, use the following command:

git config –local user.email youremail@example.com

  • git status displays the list of files that have been changed along with files that are about to be staged or committed.

git status

  • git push is used to push local commits to the master branch of the remote repository. Here is the basic structure of the code:

git push origin

  • Replace with the branch you want to push changes to when you don’t want to push changes to the master branch.

  • git checkout creates branches and helps you navigate between them. For example, the following command creates a new one and automatically switches to it:

command git checkout -b

  • To switch from one branch to another, just use:

git checkout

  • git remote allows you to see all remote repositories. The following command will list all connections along with their URLs:

git remote -v

  • To connect the local repository to a remote server, use this command:

git remote add origin

  • On the other hand, the following command will clear a connection to a specified remote repository:

git remote

  • git branch is used to list, create or delete branches. For example, if you want to list all the branches present in the repository, the command should look like this:

git branch

  • If you want to delete a branch, use:

git branch -d

  • git pull merges all the changes that have been made in the remote repository with the local working directory.

git pull

  • git merge is used to merge a branch with another active branch:

git merge

  • git diff is used to list conflicts. To be able to see conflicts with respect to the base file, use:

git diff –base

  • The following command is used to see the conflicts between branches before merging them:

git diff

  • To see a list of all conflicts present use:

git diff

  • git tag marks specific commits. Developers use it to mark release points as v1.0 and v2.0.

git tag 1.1.0

  • gitlog is used to view the history of the repository listing certain details of the commit. When executing the command, you get an output like this:

commit 15f4b6c44b3c8344caasdac9e4be13246e21sadw Author: Alex Hunter Date: Mon Oct 1 12:56:29 2016 -0600

  • git reset it is used to reset the index and the working directory to the last commit state.

git reset – -hard HEAD

  • git rm can be used to remove files from the index and working directory.

git rm filename.txt

  • git stash will momentarily save changes that are not ready to be committed. This way, you can come back to the project later.

git stash

  • git show is used to display information about any git object.

git show

  • git fetch allows the user to search for all objects in a remote repository that are not currently in the local working directory.

git fetch origin

  • git ls-tree allows you to view a tree object along with the name and mode of each item, and the SHA-1 blob value. If you want to see the HEAD, use:

git ls-tree HEAD

  • git cat-file is used to view the type and size information of a repository object. Use the -p option in conjunction with the object’s SHA-1 value to view information about a specific object, for example:

git cat-file –p d670460b4b4aece5915caf5c68d12f560a9fe3e4

  • git grep allows the user to search for specific words and phrases in the commit trees, the working directory, and the staging area. To search for www..com in all files, use:

git grep “www..com”

  • gitk displays the graphical interface for a local repository. Simply run:

gitk

  • git install allows you to browse your local repository in the GitWeb interface. For example:

git instaweb –http=webrick

  • git gc will clean up unnecessary files and optimize the local repository.

git gc

  • git file allows the user to create zip or tar archives that contain the constituents of a single repository tree. For example:

git archive – -format=tar master

  • git prune removes objects that have no incoming pointer.

git prune

  • git fsck performs an integrity check of the git file system and identifies any corrupted objects

git fsck

  • git rebase is used to apply certain changes from one branch to another. For example:

git rebase master

Git Commands Cheat Sheet in .pdf

If you’re just starting out with Git, it can be difficult to remember even the basic commands. That’s why we’ve put together a GIT cheat sheet to help you master the software. Save the file to your devices or print it to always have it ready when you have to remember the Git commands.

conclusion

Learning the basic Git commands will be of great help to developers as they can easily control the source code of projects. It might take you some time to remember all of them, so our GIT cheat sheet might be useful for you.

Practice these GIT commands and make the most of your developing skills! Good luck!

Gustavo is passionate about creating websites. He focuses on the application of SEO strategies at for Spain and Latin America, as well as the creation of high-level content. When he is not applying new WordPress tricks you can find him playing the guitar, traveling or taking an online course.

Loading Facebook Comments ...
Loading Disqus Comments ...