Unlock the Full Potential of Git
Discover the Top Must-Know Commands with Examples.
Join the DZone community and get the full member experience.
Join For FreeGit is one of the most popular version control systems used by developers worldwide. As a software developer, you must be well-versed in Git and its commands to manage code efficiently, collaborate with other team members, and keep track of changes. While there are many Git commands available, not all are equally important. In this article, I’ll cover the top Git commands that every senior-level developer should know.
“A Git pull a day keeps the conflicts away”
Git Init
The git init
command initializes a new Git repository. This command is used to start tracking changes in your project. As soon as you run this command, Git creates a new .git directory, which contains all the necessary files to start using Git for version control. Once initialized, Git tracks all changes made to your code and creates a history of your commits.
$ git init
Initialized empty Git repository in /path/to/repository/.git/
Git Clone
The git clone
command creates a copy of a remote Git repository on your local machine. This is a great way to start working on a new project or to collaborate with others on existing projects. When you run this command, Git downloads the entire repository, including all branches and history, to your local machine.
$ git clone https://github.com/username/repository.git
Git Add
The git add
command adds new or modified files to the staging area, which prepares them to be committed. The staging area is a temporary storage area where you can prepare your changes before committing them. You can specify individual files or directories with this command. Git tracks changes in three stages — modified, staged, and committed. The add command moves changes from the modified stage to the staged stage. To add all changes in the current directory to the staging area, run:
$ git add file.txt
git add .
Git Commit
The git commit
command creates a new commit with the changes you've added to the staging area. A commit is a snapshot of your repository at a specific point in time, and each commit has a unique identifier. The git commit
command records changes to the repository. A commit includes a commit message that describes the changes made. To commit changes to the repository, run the following command:
$ git commit -m "Added new feature"
Git Status
The git status
command shows you the current state of your repository, including any changes that have been made and which files are currently staged for commit. It tells you which files are modified, which files are staged, and which files are untracked.
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: file.txt
Git Log
The git log
command shows you the history of all the commits that have been made to your repository. You can use this command to see who made changes to the repository and when those changes were made along with their author, date, and commit message.
$ git log
commit 5d5b5e5dce7d1e09da978c8706fb3566796e2f22
Author: John Doe <john.doe@example.com>
Date: Tue Mar 23 14:39:51 2023 -0400
Added new feature
git log --graph
: Displays the commit history in a graph format.git log --oneline
: Shows the commit history in a condensed format.git log --follow
: Follows the history of a file beyond renames.
Git Diff
The git diff
command shows you the differences between the current version of a file and the previous version. This command is useful when you want to see what changes were made to a file. When you run this command, Git shows you the changes that were made between two commits or between a commit and your current working directory. This will show you the differences between the current branch and the “feature_branch” branch.
$ git diff feature_branch
Git Branch
The git branch
command shows you a list of all the branches in the Git repository. You can use this command to see which branch you are currently on and to create new branches. This command is used to create, list, or delete branches.
$ git branch feature-1
Git Checkout
The git checkout
command is used to switch between branches. You can use this command to switch to a different branch or to create a new branch.
$ git checkout feature-1
Git Merge
The git merge
command is used to merge changes from one branch into another. This command is useful when you want to combine changes from different branches. When you run this command, Git combines the changes from two branches and creates a new commit.
$ git merge feature-1
Git Pull
The git pull
command is used to update your local repository with changes from a remote repository. This command is used to download changes from a remote repository and merge them into your current branch. When you run this command, Git combines the changes from the remote repository with your local changes and creates a new commit.
$ git pull origin master
Git Push
The git push
command is used to push your changes to a remote repository. This command is useful when you want to share your changes with others. When you run this command, Git sends your commits to the remote repository and updates the remote branch.
$ git push origin master
Git Remote
This command is used to manage the remote repositories that your Git repository is connected to. It allows you to add, rename, or remove remote repositories.
git remote rm
: Removes a remote repository.git remote show
: Shows information about a specific remote repository.git remote rename
: Renames a remote repository.
Git Fetch
This command is used to download changes from a remote repository to your local repository. When you run this command, Git updates your local repository with the latest changes from the remote repository but does not merge them into your current branch.
$ git fetch origin
Git Reset
This command is used to unstaged changes in the staging area or undo commits. When you run this command, Git removes the changes from the staging area or rewinds the repository to a previous commit.
$ git reset file.txt
Git Stash
This command is used to temporarily save changes that are not yet ready to be committed. When you run this command, Git saves your changes in a temporary storage area and restores the repository to its previous state.
$ git stash save "Work in progress"
“Why did the Git user become a magician? Because they liked to git stash and make their code disappear”
Git Cherry-Pick
This command is used to apply a specific commit to a different branch. When you run this command, Git copies the changes from the specified commit and applies them to your current branch.
$ git cherry-pick 5d5b5e5dce7d1e09da978c8706fb3566796e2f22
Git Rebase
This command is used to combine changes from two branches into a single branch. When you run this command, Git replays the changes from one branch onto another branch and creates a new commit.
$ git rebase feature-1
Git Tag
This command is used to create a tag for a specific commit. A tag is a label that marks a specific point in your Git history.
$ git tag v1.0.0
Git Blame
This command is used to view the commit history for a specific file or line of code. When you run this command, Git shows you the author and date for each line of code in the file.
$ git blame file.txt
“Git: making it easier to blame others since 2005”
Git Show
This command is used to view the changes made in a specific commit. When you run this command, Git shows you the files that were changed and the differences between the old and new versions.
$ git show 5d5b5e5dce7d1e09da978c8706fb3566796e2f22
Git Bisect
This command is used to find the commit that introduced a bug in your code. When you run this command, Git helps you narrow down the range of commits to search through to find the culprit.
$ git bisect start
$ git bisect bad HEAD
$ git bisect good 5d5b5e5dce7d1e09da978c8706fb3566796e2f22
Git Submodule
This command is used to manage submodules in your Git repository. A submodule is a separate Git repository included as a subdirectory of your main Git repository.
$ git submodule add https://github.com/example/submodule.git
Git Archive
This command is used to create a tar or zip archive of a Git repository. When you run this command, Git creates an archive of the repository that you can save or send to others.
$ git archive master --format=zip --output=archive.zip
Git Clean
This command is used to remove untracked files from your working directory. When you run this command, Git removes all files and directories that are not tracked by Git.
$ git clean -f
Git Reflog
This command is used to view the history of all Git references in your repository, including branches, tags, and HEAD. When you run this command, Git shows you a list of all the actions that have been performed in your repository.
Git Config
This command is used in Git to configure various aspects of the Git system. It is used to set or get configuration variables that control various Git behaviors. Here are some examples of how to use git config
:
- Set user information:
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"
The above commands will set your name and email address globally so that they will be used in all your Git commits.
- Show configuration variables:
git config --list
The above command will display all the configuration variables and their values that are currently set for the Git system.
- Set the default branch name:
git config --global init.defaultBranch main
The above command will set the default branch name to “main”. This is the branch name that Git will use when you create a new repository.
Git Grep
This command in Git searches the contents of a Git repository for a specific text string or regular expression. It works similarly to the Unix grep
command but is optimized for searching through Git repositories. Here's an example of how to use git grep
:
Let’s say you want to search for the word “example” in all the files in your Git repository. You can use the following command:
git grep example
This will search for the word “example” in all the files in the current directory and its subdirectories. If the word “example” is found, git grep
will print the name of the file, the line number, and the line containing the matched text. Here's an example output:
README.md:5:This is an example file.
index.html:10:<h1>Example Website</h1>
In this example, the word “example” was found in two files: README.md
and index.html
. The first line of the output shows the file name, followed by the line number and the line containing the matched text.
You can also use regular expressions with git grep
. For example, if you want to search for all instances of the word "example" that occur at the beginning of a line, you can use the following command:
git grep '^example'
This will search for all instances of the word “example” that occur at the beginning of a line in all files in the repository. Note that the regular expression ^
represents the beginning of a line.
Git Revert
This command is used to undo a previous commit. Unlike git reset
, which removes the commit from the repository, git revert
creates a new commit that undoes the changes made by the previous commit. Here's an example of how to use git revert.
Let’s say you have a Git repository with three commits:
commit 1: Add new feature
commit 2: Update documentation
commit 3: Fix bug introduced in commit 1
You realize that the new feature you added in commit 1 is causing issues and you want to undo that commit. You can use the following command to revert commit 1:
git revert <commit-1>
This will create a new commit that undoes the changes made by commit 1. If you run git log
after running git revert
, you'll see that the repository now has four commits:
commit 1: Add new feature
commit 2: Update documentation
commit 3: Fix bug introduced in commit 1
commit 4: Revert "Add new feature"
The fourth commit is the one created by git revert
. It contains the changes necessary to undo the changes made by commit 1.
Git RM
This command is used to remove files from a Git repository. It can be used to delete files that were added to the repository, as well as to remove files that were previously tracked by Git. Here's an example of how to use git rm
-
- Remove a file that was added to the repository but not yet committed:
git rm filename.txt
This will remove filename.txt
from the repository and stage the deletion for the next commit.
- Remove a file that was previously committed:
git rm filename.txt
git commit -m "Remove filename.txt"
The first command will remove filename.txt
from the repository and stage the deletion for the next commit. The second command will commit the deletion.
- Remove a file from the repository but keep it in the working directory:
git rm --cached filename.txt
This will remove filename.txt
from the repository but keep it in the working directory. The file will no longer be tracked by Git, but it will still exist on your local machine.
- Remove a directory and its contents from the repository:
git rm -r directoryname
This will remove directoryname
and its contents from the repository and stage the deletion for the next commit.
In conclusion, these frequently used Git commands are essential for every software professional who works with Git repositories regularly. Knowing how to use these commands effectively can help you streamline your workflow, collaborate with your team more effectively, and troubleshoot issues that may arise in your Git repository.
In conclusion, these 25 Git commands are essential for every software engineer who work with Git repositories regularly. Knowing how to use these commands effectively can help you streamline your workflow, collaborate with your team more effectively, and troubleshoot issues that may arise in your Git repository.
If you enjoyed this story, please share it to help others find it! Feel free to leave a comment below.
Thanks for your interest. Connect with me on LinkedIn.
Published at DZone with permission of Shivam Bharadwaj. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments