Introduction to GIT Commands
GIT is a very popular version controlling method and a source code management tool which is used to keep a 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 files. 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.
- git config: This command is used to set the name of the author and the email address which you want your commit to address.
Use: git config –global user.email “[email address]”
- git init: It is used to start a new git repository. This command is generally used at the beginning.
Use: 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.
Use: 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 *.
Use: git add (filename),
git add *
- git commit –m: This command is used to snapshot or record a file in its version history, permanently.
Use: 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 to commit any other files which you have brought a change to, since then.
Use: 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.
Use: git diff
- git diff –staged: This command is used to display all the differences between staging area files and the latest version which might be present.
Use: git diff -staged
4.5 (1,391 ratings)
- 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.
Use: 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.
Use: git reset [file]
Intermediate GIT Commands:
- Git reset [commit]: This reset command 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.
Use: git rest [commit]
- Git reset –hard [commit]: This command is used to discard all the history and takes us to the last specified commit.
Use: git reset –hard [commit]
- Git status: This is one of the most frequently used commands as this is used to list down all the files which are ready to be committed.
Use: git status
- 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.
Use: git rm [file]
- Git log: This command is used for listing down the version history for the current working branch.
Use: git log
- git log –follow: This command 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.
Use: git log –follow [file]
- git show: This command is used to display the metadata and all the content related changes of a particular commit.
Use: git show [commit]
- git tag: This command is used to give particular tags to the code commits.
Use: git tag [commitID]
- git branch: Git branch command is used to list down all the branches that are locally present in the repository.
Use: git branch
- Git branch [branch-name]: This command is used to create a new branch.
Use:Git branch [branch-name]
Advanced GIT Commands
- Git branch –d [branch name]: This command is used to delete the current branch name specified.
Use: git branch –d [branch name]
- Git checkout: This command is helpful in switching from one branch to another.
Use: 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 basic as well as advanced and some immediate Commands. You may also look at the following article to learn more –