20+ Git Commands Extensive list - ByteScout
  • Home
  • /
  • Blog
  • /
  • 20+ Git Commands Extensive list

20+ Git Commands Extensive list

Git is an open-source distributed variant system tool created to manage various projects with agility and adaptability. It was designed by Linus Torvalds in the year 2005 to improve Linux Kernel. Git has the functionality, administration, defense, and docility that most people need. You may now use Git with services like Github and Gitlab for creating free Git-based repositories with your code inside and then track all the changes.

Technical SEO

The following is the list of important Git commands:

  1. git config – configs default settings for Git like your name and email that will be stored in Git and will be attached to changes you have made.
  2. git init – initializes new empty repository in the folder and starts tracking changes
  3. git add – adds new file (or files by mask) to be tracked for the current set of changes
  4. git clone – clones existing git repository from URL. With this command, you can download and copy the existing project from Github, Gitlab or from any other Git hosting
  5. git commit – saves the current set of changes (including ones you have added with “git add”) into the local history
  6. git status – shows the list of files in the current set of changes and indicates their current status (per file)
  7. git diff – shows exact changes that were made in the new version of the file vs old version of the file
  8. git branch – creates a new “branch” with the current set of files. This is useful if you want to try something but not in the main “master” branch
  9. git merge – merges changes
  10. git reset – resets currently made changes
  11. git rm – removes file or files from changes
  12. git checkout – restores file or files to the state how they were last time synced from remote Git
  13. git pull – pulls new changes from remote Git repository
  14. git push – sends new changes to the remote Git repository
  15. git remote – displays. the list of remote Git repositories set for the current repository
  16. git log – shows the list of changes made in the local history
  17. git archive – creates zip archive from files in the current repository
  18. git gc – optimizes current git repository
  19. git fsck – checks the integrity of the current git repository
  20. git stash – saves altered files
  21. git prune – removes “lost” objects
  22. git clean – removes new files that were added into the repository but not tracked yet

1. git config

This command is one of the first git commands you should run after installing Git on your computer. It is used to configure your personal. profile and things such as username, email, domain and much more. The following example is displaying the use of git config to set the username and its email.

Example:
git config –global user.name “jeff”
git config –global user.email “jeff@example.com”

2. git init

This is one of the git basic commands that you should use for a new Git repository. It starts a git repository and formulates the initial .git directory in a new or a previously created project. For example, the following command initializes the empty Git repository.

git init
Initialized empty Git repository in /home/myfolder/GITFOLDER/.git/

3. git add

This command is one of the most used git commands. This command adds a file or files in the indexed working directory. For example, the following command is adding project1 in the indexed working directory.

git add project1

git add *.js – adds all *.js files in the current folder into tracking for changes in Git repository

4. git clone

This command is the most widely used git command in the git commands list because you can use it to automatically download remote Git repository into your computer. It generates a copy of the Git repository from an old source. The command will also attach the primary spot as a detached location so users are capable to retrieve from it again and push. For example,

git clone git@github.com:jeff/mydir.git

git clone http://git@github.com:bytescout/ByteScout-SDK-SourceCode.git – this will download and save ByteScout samples Git repository into “ByteScout-SDK-SourceCode” folder

5. git commit

The “commit” in git commit is applied to save the changes to the git repository. It takes all of the modifications addressed in the index, generates a fresh commit object aiming to it, and inserts the git branch to the new commit. For, example, the following command is using git commit.

git commit -m ‘saving all the changes’
git commit -a -m ‘saving all changes, similar to git add and git commit’

6. git status

This Git command displays the status of records in the index against the functioning directory. It displays files that are not tracked, altered, tracked but not updated, and already in the queue for committing. The following example will display status.

git status

Result:

# On branch master #
# Initial commit #
# Untracked files: #

7. git diff

This command displays the file discrepancies. In other words, this command displays the differences which are still not staged. With the help of this command, the differences between the two branches can also be displayed. The git branch is just a movable pointer.  For example,

git diff

8. git branch

This is the most significant command for git list branches. It displays current branches, such as distant branches if extension ‘-a’ is mentioned. It can also produce a new git push branch if a branch name is given: For example

git branch -a * master remote1/source/master

9. git merge

This is one of the common git commands. As the name suggests, it can merge one or more git branches into your existing branch. It also implicitly forms a new commit if there are no disputes. The git merge command allows to use the self-governing lines of development produced by git branch and combine them into a separate branch. It is important to note that all of the commands merge into the existing branch. For example, the following command is displaying the use of merge command.

git merge mybranch_2

10. git reset

The git reset is one of the useful git commands. This command resets the index and current working directory to the condition of your last commit. This is just like a rollback but up to the last commit. The git reset is an important command that is utilized to undo local modifications to the nature of a Git repository. Git reset works on the three trees. These trees are the Commit History ( HEAD ), the Staging Index, and the Working Directory.

For example, the following command resets the myfile.css up to the last commit.

git reset myfile.css

11. git rm

This is one of the most useful git cmd commands. This command is used to delete the file from the current directory and stages the removal. For example, the following command removes the testfile.txt from the current directory.

git rm testfile.txt

git rm *.log – removes all *.log files from tracking

 

12. git checkout

This is one of the most important git command-line tools. The checkout command is used to switch from one git branch to another. For example, the following command is displaying the use of checkout command to switch to branch_3

git checkout branch_3

13. git pull

The git pull request is one of the most used git commands. It retrieves and merges variations on the distant server to the current working directory. For example, the following command is displaying how to retrieve the variations on a remote server.

git pull http://git@github.com:bytescout/ByteScout-SDK-SourceCode.git

14. git push

The git push command is one of the most important git commands. This command is used to push all the updated or remodeled bounded objects to the remote git repository and develop its git branches. For example, the following command is displaying the use of push.

git push origin: branch_3

15. git remote

This is one of the most useful git commands. It is used to display all the remote variant of the git repository. A remote repository in Git is a public repository that developers utilize to swap their changes. For example, the following command is displaying the effective use of git remote command.

git remote origin

16. git log

The git log command is one of the most crucial commands. It is used to display all the commits on a git branch and display the analogous details. The Git Log command enables the user to observe data about earlier commits that have transpired in a project. The purest variant of the log command displays the commits that drive up to the status of the checked out git branch. The following example is displaying the use of git log command.

git log commit

17. git archive

The git archive is one of the common git commands. This command is used to create a zip file that involves the data of a private tree from the git repository. It also generates an archive of the defined setup comprising the tree structure for the selected tree and addresses it out to the conventional output. The following example is displaying the use of git archive command.

git archive –format=zip master^ MYFILE >file.zip

18. git gc

This command is one of the top git commands. It works as a garbage collector. This command is used to collect garbage from the git repository. The great thing about this command is that it is also used to optimize the repository. It should be run periodically. The following example is displaying the use of git gc

git gc

After running the above command it produces the following result:

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

19. git fsck

This command is one of the most crucial git commands. It is also known as a File System ChecK. The name is just like that of Unix fsck command. It is also used to validate a file system. The git fsck command will conduct an integrity analysis of the Git file system and recognize infected objects. For example, the following command will perform the integrity analysis.

git fsck

20. git stash

The git stash command is one of the most useful git commands. This command is used to tentatively saves all the altered traced files. In other words, stashing is responsible for taking the modified tracked files and staged modifications and saves it on a stack of incomplete developments that the user can apply at any time. The following example is displaying the use of git stash

git stash save

21. git prune

This command is one of the most useful git commands. It is used to eliminate objects that are no longer aimed by any object in any branch. The git prune command is an in-house service that sweeps up orphaned objects. These objects are those that are difficult to access by any refs. Prune is recognized as a garbage accumulation command and is a subordinate command of the git gc. The following example is displaying the use of git prune command.

git prune

22. git clean

This command is the command that helps to clean from non-tracked files in your Git repo. Sometimes if you have lot of files generated during compilation or work of your application you may need to have a way to quickly remove them to avoid accidental addition of these files into the main Git repository/

git clean -n – will simulate the removal of all the untracked files in your repository

git clean -n "*.log" – will simulate removal of all the untracked *.log files

git clean -f "*.log" – will remove all the untracked *.log files from the current repository

 

The above git commands cheat sheet is crucial for any developer.  One of the most important benefits of Git is its branching abilities. Unlike centralized variant control networks, Git branches are standard and simple to merge. This helps the branch workflow successful with many Git users. Feature branches present a remote environment for each modification to the codebase.

The feature branch workflow also gives adaptability when preferences shift. For example, if you’re midway through a release period and you want to delay one characteristic instead of different time-crucial one, it can also be done.

 

About the Author

ByteScout Author

Prasanna Peshkar

Prasanna is an independent cybersecurity consultant and technical writer, focusing on penetration testing and vulnerability assessment. He provides penetration testing services to a wide variety of clients, including financial institutions, brokerage firms, professional regulators, manufacturing companies and transportation companies.

 

 

 

prev
next