Top 10 Commonly Used Git Commands

Commonly used Git commands

In this article, we will review the top 10 most commonly used Git commands. You can’t call yourself a competent DevOps Engineer or IT professional for that matter, without a good working knowledge of Git and what it’s used for. It is a free and open-source version control system (VCS) used by collaborative projects in the software development world.

Table of Contents

Introduction

In a previous article, “Installing and using Git on Linux Machines“, we covered what Git is and how to use it. Today’s focus will be Git commands and specifically, the top 10 Git commands with some examples provided for demonstrative purposes.
Commonly used Git commands - Three Monitor Computer Setup

Image by ThisisEngineering from Pexels

git init

git init: Initializes a new Git repository in the current directory.

git init my-repo: Initializes a new Git repository in a directory named “my-repo”.

git init --bare my-repo.git: Initializes a new bare Git repository in a directory named “my-repo.git”.

git init --template=my-template: Initializes a new Git repository using a custom template.

git clone

git clone: Creates a local copy of a remote Git repository.

git clone https://github.com/myuser/my-repo.git: Clones a repository hosted on GitHub.

git clone git@bitbucket.org:myuser/my-repo.git: Clones a repository hosted on Bitbucket.

git clone https://my-git-server.com/myuser/my-repo.git: Clones a repository hosted on a private Git server.

git add

git add: Adds changes to the staging area.

git add file1.txt: Adds a single file to the staging area.

git add *.txt: Adds all files with a “.txt” extension to the staging area.

git add -p: Interactively add changes to the staging area.

git commit

git commit: Records changes to the repository.

git commit -m "Initial commit": Commits changes with a commit message.

git commit -a: Commits all changes in tracked files.

git commit --amend: Amends the previous commit.

git status

git status: Shows the current status of the repository.

git status: Shows the status of the repository.

git status -s: Shows a shorter, more concise status output.

git status --ignored: Shows ignored files in the status output.

git log

git log: Shows the commit history of the repository.

git log: Shows the full commit history.

git log --oneline: Shows a condensed commit history.

git log --author="John Doe": Shows the commit history of a specific author.

git branch

git branch: Lists, creates, or deletes branches.

git branch: Lists all branches in the repository.

git branch my-branch: Creates a new branch named “my-branch”.

git branch -d my-branch: Deletes the branch named “my-branch”.

git merge

git merge: Merges changes from one branch into another.

git merge my-branch: Merges changes from “my-branch” into the current branch.

git merge --no-ff my-branch: Forces a new merge commit, even if a fast-forward merge is possible.

git merge --abort: Aborts a merge that is in progress.

git remote

git remote: Shows or manages remote repositories.

git remote: Shows the names of all remote repositories.

git remote add origin https://github.com/myuser/my-repo.git: Adds a new remote named “origin”.

git remote remove origin: Removes the remote named “origin”.

git push

git push: Pushes changes to a remote repository.

git push origin master: Pushes changes to the “master” branch on the “origin” remote.

git push --tags: Pushes all tags to the remote repository.

git push --delete origin my-branch: Deletes the “my-branch” branch on the “origin” remote.

Top 10 commonly used Git Commands: Ranked by order of significance

Now that we’ve reviewed the top 10 commonly used git commands, let’s rank them in order of significance or more specifically, by frequency of use and importance in day-to-day Git workflows.

  1. git init: This command is used to initialize a new Git repository in the current directory. It is typically used only once when starting a new project.

  2. git add: This command adds the changes made to the files in the working directory to the staging area, where they can be committed.

  3. git commit: This command creates a new commit with the changes in the staging area. It requires a commit message to be provided.

  4. git push: This command pushes the local changes to the remote repository, updating it with the latest changes. It requires appropriate permissions and access credentials to be set up.

  5. git pull: This command fetches the latest changes from the remote repository and merges them with the local branch.

  6. git status: This command shows the current status of the repository, including the files that have been modified, added or deleted, and whether they are in the staging area or not.

  7. git branch: This command lists all the branches in the repository and allows users to create, delete, and switch between branches.

  8. git merge: This command merges changes from one branch to another, typically used to incorporate changes from feature branches into the main branch.

  9. git clone: This command creates a copy of a remote repository in a local directory, allowing users to work on it locally.

  10. git log: This command displays a history of all the commits made in the repository, along with their commit messages, timestamps, and other details.

Here’s the ranking of Git commands by significance:

  1. git init
  2. git add
  3. git commit
  4. git push
  5. git pull
  6. git status
  7. git branch
  8. git merge
  9. git clone
  10. git log

Conclusion

We’ve reviewed the top 10 commonly used Git commands and ranked them by order of significance or by how frequently they are used in day-to-day Git workflows.

Was this article helpful to you? If so, leave us a comment below. We’d love to hear from you!

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *