Git Commands

Git is a popular version control system, used to track changes in source code and support collaboration across teams of developers. While it’s primarily used in software development, Git can be used to track changes in any type of computer file.

Git was originally created for use in the development of the Linux operating system, but is now supported on Windows, Mac OS and other popular platforms. Git is open source and free to use.

Once installed, Git is used through the command line, via a Windows Command Prompt or Terminal in Mac OS.

The following is a list of common commands you can use to try out some of Git’s core features.

git config

This command configures values for variables such as a user name, email address, gpg key, file formats, and more.

git config --global user.name "My Username"
git config --global user.email "[email protected]"

 

git init

Creates a new git repository, in a new or existing project.

git init
Initialized empty Git repository in /home/user_name/GIT/.git/

 

git pull

Fetches files from the remote repository and merges with your working directory.

git pull origin

 

git push

Sends locally created and stored changes to the remote repository, before advancing the branch.

git push origin master

 

git clone

Creates a copy of an existing git repository from a remote location. The command also adds the remote location so it can be accessed in the future.

git clone [email protected]:user/mytest.git

 

git add

Adds file changes from your local working directory to your index.

git add .

git rm

Removes files from your git index and working directory, ensuring that subsequent changes will not be tracked.

git rm myfile.doc

 

git commit

Creates a new commit object pointing to changes made in your index, then sets the branch to point to the new commit.

git commit -m ‘committing added changes’
git commit -a -m ‘committing all changes, equals to git add and git commit’

 

git status

Compares and displays the status of files held in the index with those held in your working directory. The command lists files that are currently untracked (stored only in your working directory and not in the index), modified (tracked, but not yet updated in the index) and staged (added to the index, ready for committing).

git status

# On branch master #
# Initial commit #
# Untracked files: #
# (use "git add <file>..." to include in what will be committed) #

 

git remote

Displays all remote versions of your repository.

git remote origin

 

git branch

Lists existing git branches. Use the -a parameter to also list remote branches. The command creates a new branch if a name is provided.

git branch -a * master remotes/origin/master

 

git merge

Merges one or more branches into your current branch. The command also automatically creates a new commit if there are no conflicts.

git merge newbranchversion

 

git gc

Used to optimize and tidy your git repository.

git gc

Counting objects: 8, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (5/5), done.
Writing objects: 100% (8/8), done.
Total 8 (delta 1), reused 0 (delta 0)

 

git prune

Removes objects that are no longer pointed to by any object in any reachable branch.

git prune

 

git reset

Resets your working directory and Git index to the state of the last commit.

git reset --hard HEAD

 

git tag

Adds a tag to a Git commit, allowing easy labeling of versions and other descriptive text.

git tag -a v1.0 -m 'this is version 1.0'

 

git log

Displays commits on a branch with additional details, such as author and date.

git log commit

64f673e8a0d895bb37ff7da40e294b61a99a2ff Author: User <[email protected]> 
Date: Sat Jul 23 23:02:23 2015 +0200 first commit

 

git archive

Creates an archive file (zip or tarball) with the contents of a single tree from your repository.

git archive --format=zip master^ README >file.zip

 

For further questions, or if you need help, please open a support ticket from your HostPapa Dashboard. Follow this link to learn how.

Related Articles