• Articles
  • Tutorials
  • Interview Questions

Basic 18 Git Commands List with Examples

Tutorial Playlist

Git Commands

Git is a free, open-source distributed version control system tool designed to handle 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, let’s talk about the top 18 Git commands that are useful for working with Git.

Watch this Git commands Video Tutorial on Youtube:

Youtube subscribe

Top Git Commands for Local and Remote Repositories

Here is the GIT Commands List with Examples :

1. git init

Usage: git init [repository name]

We have to 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.

Git Command

2. git add

Usage (i): git add [file(s) name]

This will add the specified file(s) into the Git repository, the staging area, where they are already being tracked by Git and now ready to be committed.

Git Command

Usage (ii): git add . or git add *

This will take all our files into the Git repository, i.e., into the staging area.

Git Command

We can use this command as git add -A as well.

Note: We will have to commit our files after we add them to the staging area.

Prepare yourself for the industry by going through these Top DevOps Interview Questions and Answers!

Get 100% Hike!

Master Most in Demand Skills Now !

3. git commit

 Usage: git commit -m “message”

This command records or snapshots files permanently in the version history. All the files, which are there in the directory right now, are being saved in the Git file system.

Git Command

 

4. git status

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 has to be committed.

Git Command

Grab the Best DevOps Course in New York to get a clear understanding of DevOps!

5. git remote

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:

Step 1:
(1) Login to the GitHub account if the account already exists (If not, sign up on github.com)
(2) Click on New

Git Command

Step 2: Now, we have to create a new repository. Provide a name to our repository, select the privacy of the repository as Public, and then click on Create repository

Git Command

This GIT Cheat Sheet by Intellipaat is very useful and handy. Give it a read.

Once we are done with filling up the new repository form, we would land on a page as follows:

Git Command


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 the remote commands in our repository that we have just created.

Git Command

Learn more about DevOps in this DevOps Training in Sydney to get ahead in your career!

 

6. git push

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 can be synced with the remote repository on Github.

Git Command

 

7. git clone

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 file by clicking on Clone or Download and copying the link and pasting it on the terminal with the git clone command. This will import the files of a project from the remote repository to our local system.

(1) The below screenshot shows from where to copy the link:

Git Command

To create a local folder, we have to use the following command:

mkdir [directory- name]
cd [directory- name]
git clone [URL]

Now, paste the copied link along with the git clone command as shown below:

Git Command

Note: Here, we don’t have to use the git remote add origin command because we have already cloned the remote repository in the local directory. Now, if we push any new file, it knows where it has to go.

Read on: What is GitLab?

8. git branch

Usage (i): git branch [name-of-the-branch]

When multiple developers are collaborating on a project or repository, branches become essential for managing different workspaces. Using this command, we can create a new branch (for example, ‘branch1’). This allows developers to work independently on their respective branches, making changes and commits without affecting the main branch or other branches.

Git Command

Usage (ii): git branch -D [name-of-the-branch]

Likewise, to delete a branch, we utilize the “git branch -D” command. This enables us to remove a specific branch (e.g., ‘name-of-the-branch’) that is no longer needed, cleaning up the repository and reducing clutter.

Git Command

9. git checkout

Usage (i): git checkout [name-of-the-new-branch]

This command allows us to switch to an existing branch within our repository. It facilitates navigating to the desired branch, enabling us to add new files, make changes, and commit those files within that specific branch.

Git Command

Usage (ii): git checkout -b [name-of-the-new-branch]

This command serves a dual function. Firstly, it creates a new branch with the given name (for example, ‘branch2’). Secondly, it immediately switches our working environment to that newly created branch. This allows us to seamlessly begin working within the newly created branch, making it convenient to add files, make modifications, and commit changes exclusively within that branch.

Git Command

Interested in becoming a DevOps expert? Go for this DevOps Course in Toronto and excel in your career!

10. git log

Usage (i): git log

The “git log” command is handy when we want to examine the detailed log of every commit in our repository. By executing this command, we can view the log specific to the branch we are currently in. Additionally, we can use “git log -3” to display the last three logs.

Git Command

Usage (ii): git log –graph

For a visual representation of the commit history, we can utilize “git log –graph”. This option presents the commit-graph, showcasing the branching and merging of commits

Git Command

Usage (iii): git log –graph –pretty=oneline

To further customize the output, we can use “git log –graph –pretty=oneline”. This format displays the commit graph along with a concise one-line description for each commit.

Git Command

11. git stash

Usage (i): 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.

Git Command

Usage (ii): git stash -u

This command is used when we want to stash the untracked files.

Git Command

Usage (iii): git stash pop

This command is used when we are back on our branch and want to retrieve the code.

Git Command

become a devops architect

12. git revert

Usage: git revert [commit id]

The git revert command can be considered as an ‘undo’ command. However, it does not work as the 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.

Git Command

13. git diff

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 the git status and git log commands to analyze the current state of our Git repository. We use git log to get the details of commit IDs.

Let’s compare the working directory with the index as shown below:

Git Command

14. git merge

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 it finds a common base commit, it will create a commit sequence.

Git Command

15. git rebase

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 is composed of entirely new commits.

The git rebase command performs an automatic git checkout <branch> before doing anything else. Otherwise, it remains on the current branch.

Git Command

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.

Note: We don’t rebase public history. We should never rebase commits once they are pushed to a public repository. Why because the rebase would replace the old commits with the new ones, and it would appear that a part of our project history got abruptly vanished.

16. git fetch

Usage: git fetch

When we use the command git fetch, Git gathers any commit from the target branch that does not exist in our current branch and stores it in our local repository. However, it does not merge it with our current branch.

Git Command

In situations where we want to keep our repository up to date but are concerned that updating our files might lead to issues, a specific technique comes to the rescue. To integrate the commits into our master branch, we use the merge feature. This feature actively retrieves all the branches from the repository and then proceeds to download all the required commits and files from another repository. It ensures that our repository remains current while mitigating the risk of potentially breaking our ongoing work.

17. git reset

Usage: git reset –hard [SOME-COMMIT]

We use this command to return the entire working tree to the last committed state.

Git Command

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 the 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 the ‘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:

  • Make our current branch (typically master) back to point <SOME-COMMIT>
  • Make the files in our working tree and the index (“staging area”) the same as the versions committed at <SOME-COMMIT>

18. git pull

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 Command

Get in touch with Intellipaat for a comprehensive DevOps Training and be a certified DevOps Engineer!

Git and Its Popularity

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 we are part of the software developers’ world, it is expected that we know Git. So, having a good grasp of common Git commands is very beneficial for us to get into a lucrative career.

Git Command

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 are Git commands?’ and ‘What do Git commands do?

Watch this DevOps Project tutorial:


Youtube subscribe

If you’re seeking a more comprehensive grasp of various version control systems along with practical exercises, consider enrolling in the DevOps Certification Training Course offered by Intellipaat. Feel free to share your thoughts in the comments section regarding the usefulness of this beginner’s guide to Git commands.

Course Schedule

Name Date Details
AWS Certification 27 Apr 2024(Sat-Sun) Weekend Batch
View Details
AWS Certification 04 May 2024(Sat-Sun) Weekend Batch
View Details
AWS Certification 11 May 2024(Sat-Sun) Weekend Batch
View Details