Introduction to GIT Commands
GIT is a very popular version controlling method and a source code management tool which is used to keep track of all the changes that have been made in the source code of the program. It has made the life of software engineers quite easy as it is used to track only the changes in one or more than one file. It is used to collaborate among the software engineers as every engineer is working on their code within their branch and can push their code to merge into the master branch, thereby creating a complete code solution. The goals as to why it has been among the very popular tools are due to its integrity, speed and that it supports all the non-linear workflows of a distributed manner. The GIT directory present on every computer is in itself a complete repository which is not the case with many client-server systems operating today. While using with GIT bash, there are some commands which should be known to you. In this post, we are going to discuss those commands.
Basic GIT Commands
The basic commands are as follows.
- git config: It is used to set the name of the author and the email address which you want your commitment to addressing.
git config –global user.email “[email address]”
- git init: It is used to start a new git repository. This is generally used at the beginning.
git init [repo name]
- git clone: This command is used to clone or copy a repository from a URL. This URL generally is a bitbucket server, a stash or any other version control and source code management repository holding service.
git clone [URL]
- git add: It is used to add a file to the staging area. Instead of choosing a single file name, you can also choose to give all filenames with an *.
git add (filename),
git add *
- git commit –m: It is used to snapshot or record a file in its version history permanently.
git commit –m [type in a message]
Giving a message text at the end of the commit command helps in identifying the details about the commit code.
- git commit –a: This commit command is used to commit any such file which has been added as a result of the git add command. It is also responsible for committing any other files to which you have brought a change to since then.
git commit -a
- git diff: As the name suggests, this command is used to display all the differences between the files until the changes have not yet been staged.
- git diff –staged: It is used to display all the differences between staging area files and the latest version, which might be present.
git diff -staged
- git diff [first branch] [second branch]: This is a very effective command as it is used to display the differences present between the two branches. Generally, a single developer will be working on his individual branch, which will then be combined into a master branch.
git diff [first branch] [second branch]
- git reset [file]: This command, as the name suggests, is used to unstage a file. Even though it unstages the file, still the contents of the file have stayed intact.
git reset [file]
Intermediate GIT Commands
The intermediate commands are as follows.
- Git reset [commit]: It is used to undo all the changes that have been incorporated as a part of a commit after a specified commit has taken place. This helps in saving the changes locally on the computer.
git rest [commit]
- Git reset –hard [commit]: This command is used to discard all the history and takes us to the last specified commit.
git reset –hard [commit]
- Git status: This is one of the most frequently used as this is used to list down all the files which are ready to be committed.
- Git rm: As in the Unix, rm is used to remove; in the same way, rm is used to delete the file from the present working directory and is also used to stage the deletion process.
git rm [file]
- Git log: This is used for listing down the version history for the current working branch.
- git log –follow: This is similar to that of git log with the additional difference that it lists the version history for a particular file, which often includes the renaming of the file also.
git log –follow [file]
- git show: This is used to display the metadata and all the content related changes of a particular commit.
git show [commit]
- git tag: This is used to give particular tags to the code commits.
git tag [commitID]
- git branch: Git branch command is used to list down all the branches that are locally present in the repository.
- Git branch [branch-name]: This is used to create a new branch.
Git branch [branch-name]
The advanced commands are as follows.
- Git branch –d [branch name]: It is used to delete the current branch name specified.
git branch –d [branch name]
- Git checkout: It is helpful in switching from one branch to another.
git checkout [branch-name]
Tips and Tricks
A file which is most commonly ignored is a ~/.gitconfig file which is the git’s global configuration file and contains quite useful commands. Many settings can be modified/altered as per your wish by making use of this command. You can turn particular command options off or on based on your choice and can also set the aliases.
Another crucial point to mention is about the visualization of the commit-graph, which comes in very handy when you are working on some project which consists of a lot of branching structures.
The best way to memorize these commands is by making frequent use of them. Don’t worry; if you don’t have an official project, you can clone any repo from the stash and start working on GIT to get hands-on experience and a nice flavor.
This has been a guide to GIT Commands. Here we have discussed concept, basic, intermediate as well as advanced GIT Commands along with tips and tricks to use effectively. You may also look at the following article to learn more –