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 tracking all the changes.
The following is the list of important Git commands:
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”
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 previously created project. For example, the following command initializes the empty Git repository.
git init
Initialized empty Git repository in /home/myfolder/GITFOLDER/.git/
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 the Git repository
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
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 at 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’
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: #
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,
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
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 of the self-governing lines of development produced by the git branch and combines 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 the merge command.
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.
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
This is one of the most important gits 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 the checkout command to switch to branch_3
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
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.
This is one of the most useful git commands. It is used to display all the remote variants 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 the git remote command.
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 the git log command.
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 the git archive command.
git archive –format=zip master^ MYFILE >file.zip
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)
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 the 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.
The git stash command is one of the most useful git commands. This command is used to tentatively save all the altered traced files. In other words, stashing is responsible for taking the modified tracked files and staged modifications and saves them on a stack of incomplete developments that the user can apply at any time. The following example is displaying the use of git stash
This command is one of the most useful git commands. It is used to eliminate objects that are no longer aimed at 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 the git prune command.
git prune
This command is the command that helps to clean from non-tracked files in your Git repo. Sometimes if you have a 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 a different time-crucial one, it can also be done.
Bash commands for GIT Git Bash is a tool that provides an emulation layer for the Git command line interface for Microsoft Windows systems.
BASH stands for Bourne Again Shell, a shell or terminal program allowing written command-line access to an operating system. Bash is one of the most used default shells for Linux and macOS.
A program called Git Bash installs Bash along with a few standard bash functions for Windows. The /usr/bin directory of the Git Bash emulator contains additional commands that come with Git Bash.
On Windows, Git Bash may offer a reasonably feature-rich shell environment. The following shell commands not covered by this document are bundled with Git Bash: SCP, cat, and ssh.
Git Bash offers the entire collection of Git core commands covered throughout this website and the previously discussed Bash commands.
For more information on git clone, git commit, git checkout, git push, and other git operations, visit the relevant documentation pages.
Git config -global user. The Name "[name]" is the syntax to use. git config -global user.email "[email address]" is the syntax to use.
This command sets the author name and email address with each of your commits.
This version control system, which is free and open-source, can effectively manage small to large projects. A source code change tracking tool called Git enables multiple engineers to work together on non-linear development. Git was developed in 2005 by Linus Torvalds for the Linux kernel. Get Linux set up with Git.
Git config —global "Linuxtechi User" user.name Git config —global "linuxtechiuser@linuxtechi.com" user.email
Commands in GIT You must launch a terminal on your computer to execute Git commands (also known as a command prompt, command shell, and command line).
Git is preconfigured as a command line option on Linux and macOS devices. A Git command is not present in Windows, though.
A group of command-line tools called Git was developed to function in a command-line environment resembling a Unix system.
Modern operating systems like Linux and macOS come with both integrated Unix command line terminals. B.602ecause of this, using Linux and macOS together when using Git is complimentary.
This is an SSH login shell for restricted access to Git.
It only allows for executing server-side Git commands that implement the pull/push capability and any custom commands found in the user’s home directory’s git-shell-commands subdirectory.