Git is a free, open-source distributed version control system tool designed to handle from small to very large projects with speed and efficiency. It has steadily grown from just being a preferred skill to a must-have skill for multiple job roles today. Git has become an essential part of our everyday development process.
In this Git commands tutorial, we will talk about top 18 Git commands that are frequently used while working with Git.
Here are those Git commands which will be covered:
So, let’s get started!
Usage: git init [repository name]
Navigate to our project directory and type the command git init to initialize a Git repository for our local project folder. Git will create a hidden .git directory and use it for keeping its files organized in other subdirectories.
Usage: git add [file(s) name]
This will add the specified file(s) into the Git repository, i.e., into the staging area where they are already being tracked by Git and now ready to be committed.
Usage: git add . or git add *
This will take all our files into the Git repository, i.e., into the staging area.
We can use this command as ‘git add -A’ as well.
Remember, we will have to commit our file after we add it into the staging area.
Usage: git commit -m “message”
This command records or snapshots the file permanently in the version history. All the files which are there in the directory right now are being saved in the git file system.
Usage: git status
This command will show the modified status of an existing file and the file addition status of a new file, if any, that have to be committed.
Usage: git remote add origin “[URL]”
Once everything is ready on our local system, we can start pushing our code to the remote (central) repository of the project. For that, follow the below steps:
(1) Login to the GitHub account if the account already exists (If not, sign up on github.com)
(2) Click on New
Step 2: Now, we have to create a new repository. Provide a name to our repository, select the privacy of the repository as Public, click on Create repository
Once we are done with filling up the new repository form, we would land on a page as follows:
Step 3: Click on the copy icon on the right side of the URL box of the Github repository to copy the link and paste it as shown below:
git remote add origin “URL”
Now, we are ready to operate remote commands in our repository that we have just created.
Usage: git push origin [branch name]
Suppose, we have made some changes in the file and want to push the changes to our remote repository on a particular branch. By using the command ‘git push’, the local repository’s files will be synced with the remote repository on Github.
Usage: git clone [URL]
Suppose, we want to work on a file that is on a remote Github repository as another developer. How can we do that? We can work on this by clicking on Clone or Download and copying the link and pasting it on the terminal with the git clone command, which will import the files of a project from the remote repository to our local system.
The below screenshot shows from where to copy the link:
mkdir [directory- name]
cd [directory- name]
git clone [URL]
Now, paste the copied link along with the git clone command as shown below:Remember, here we don’t have to use the git remote add origin command because we have already cloned the remote repository in this local directory. Now, if we push any new file, it knows where it has to go.
Usage: git branch [name-of-the-branch]
So far, we saw how we can work on Git. Now, imagine, multiple developers working on the same project or repository! To handle the workspace of multiple developers, we use branches. To create a branch (say, the ‘name-of-the-branch’ is ‘branch1’), use this command.
Usage: git branch -D [name -of-the-branch]
Similarly, to delete a branch, we use the git branch -D command.
Usage: (i) git checkout [name-of-the-new-branch]
We use this command to navigate to an existing branch, add new files, and commit the files
Usage: (ii) git checkout -b [name-of-the-new-branch]
We use this command to create and navigate to that particular branch (say, the ‘name-of-the-new-branch’ is ‘branch2’) at the same time.
Usage: git log
This command is used when we want to check the log for every commit detail in our repository. Remember, it will show the log of the branch we are in. We can check the last 3 logs by giving the command like git log -3
Usage: git stash
This command can be used when we want to save our work without staging or committing the code to our git repository and want to switch between branches.
Usage: git stash -u
This command is used when we want to stash untracked files.
Usage: git stash pop
This command is used when we are back on our branch and want to retrieve the code.
Usage: git revert [commit id]
The git revert command can be considered an ‘undo’ type command. However, it is not a traditional undo operation. It figures out how to invert the changes introduced by the commit and appends a new commit with the resulting inverse content.
Usage: git diff [commit-id-of-version-x] [commit-id-of-version-y]
Diffing is a function that takes two input datasets and outputs the changes between them. The git diff command is a multi-use Git command which when executed runs a diff function on Git data sources. These data sources can be commits, branches, files, and more. The git diff command is often used along with git status and git log commands to analyze the current state of a Git repository. Use git log to get the details of commit IDs.
Let’s compare the working directory with index as shown below:
Usage: git merge [another-file-name]
This command will combine multiple sequences of commits into one unified history. In the most frequent use cases, git merge is used to combine two branches. The git merge command takes two commit pointers, usually the branch tips, and finds a common base commit between them. Once Git finds a common base commit, it will create a commit sequence.
Usage: git rebase [base]
Rebase is the process of moving and combining a sequence of commits to a new base commit. Rebasing is changing the base of our branch from one commit to another, making it appear as if we’ve created our branch from a different commit. Internally, Git accomplishes this by creating new commits and applying them to the specified base. It’s very important to understand that even though the branch looks the same, it’s composed of entirely new commits.
git rebase performs an automatic git checkout <branch> before doing anything else. Otherwise, it remains on the current branch.
Consider a situation where we have branched off from the master and have created a feature branch, but the master branch is still having more commits. We want to get the updated version of the master branch in our feature branch, keeping our branch’s history clean, so that it appears as if we are working on the latest version of the master branch.
Remember, don’t rebase public history. We should never rebase commits once they’ve been pushed to a public repository. The rebase would replace the old commits with new ones, and it would appear that part of our project history abruptly got vanished.
Usage: git fetch
When we use the command git fetch, Git gathers any commits from the target branch that do not exist in our current branch and stores them in our local repository. However, it does not merge them with our current branch.
This is particularly useful when we need to keep our repository up to date but are working on something that might break if we update our files. To integrate the commits into our master branch, we use merge. It fetches all of the branches from the repository. It also downloads all the required commits and files from the other repository.
Usage: git reset –hard [SOME-COMMIT]
We use this command to return the entire working tree to the last committed state.
This will discard commits in a private branch or throw away the uncommitted changes!
Here we have executed a ‘hard reset’ using the –hard option. Git displays the output indicating that HEAD is pointing to the latest commit. Now, when we check the state of the repo with git status, Git will indicate that there are no pending changes (if any prior addition of a new file or modification of an existing file is done before using ‘git reset –hard’ command). Our modifications to an existing file, if not committed, and the addition of a new file, if not staged, will be destroyed. It is critical to take note that this data loss cannot be undone.
If we do git reset –hard [SOME-COMMIT], then Git will:
Usage: git pull origin master
The git pull command first runs ‘git fetch’ which downloads the content from the specified remote repository and then immediately updates the local repo to match the content.
Git is one of the most important version control systems that has experienced a significant growth rate and popularity over the years. Git plays a key role in both developers’ and non-developers’ world. If you are part of the software developers’ world, it is expected that you know Git. So, having a good grasp of Git is very beneficial for you to get into a lucrative career.
Even Microsoft has started leveraging Git very recently. This opens up more opportunities for the developers’ community. Hence, it is the right time to learn Git.
Hopefully, you have understood Git and got answers to the questions: ‘What is Git Commands?’ and ‘What does Git Commands do?
To have a deeper level of understanding of different version control systems with hands-on exercise, you can also refer to this DevOps Certification Training Course provided by Intellipaat.
Download Interview Questions asked by top MNCs in 2019?