Fundamentals 9 min read

Master Git: 20 Essential Commands Every Developer Should Know

This guide introduces the 20 most useful Git commands—from initializing a repository and basic commit workflows to advanced techniques like rebasing, cherry-picking, and bisecting—helping developers of all levels efficiently manage and collaborate on code.

21CTO
21CTO
21CTO
Master Git: 20 Essential Commands Every Developer Should Know

Git is a powerful, open‑source distributed version control system that enables developers to collaborate, track changes, and roll back to previous versions.

This article presents the 20 most effective Git commands for both beginners and experienced engineers.

What Is Git?

Git tracks changes in a codebase over time. Created by Linus Torvalds in 2005, it has become the most widely used version control system worldwide.

Git stores changes in a repository, which is a collection of files and directories that make up a project. When modifications occur, Git records them and allows developers to restore earlier versions as needed.

Why Do Developers Use Git?

Git lets developers track the evolution of a codebase, which is crucial for collaboration. It also enables rollback to previous states when problems arise and supports branching, allowing independent work on features or bug fixes that can later be merged back.

Basic Git Commands

Initialize a Repository

Run the following command in your project directory to create a new Git repository:

$ git init
Initialized empty Git repository in /Users/duluoyi/Documents/git-test/.git/

Add Files to the Staging Area

After initializing, add files to the staging area to start tracking changes:

$ git add <filename>

Commit Changes

Commit the staged files to the repository with a descriptive message:

$ git commit -m "Your commit message"
1 file changed, 1 insertion(+)
create mode 100644 test.txt

View the Log

Show the commit history:

$ git log
Author: 洛逸 <[email protected]>
Date:   Thu May 18 15:46:58 2023 +0800
    post message

Create a Branch

Create a new branch to work independently:

$ git branch <branch-name>
# e.g., git branch development

Switch Between Branches

Change the current branch:

$ git checkout <branch-name>
# e.g., git checkout main
Already on 'main'
git checkout development

Merge a Branch

Merge a branch into the current branch:

$ git merge <branch-name>
# e.g., git merge development

Intermediate Git Commands

Clone a Repository

Copy a remote repository to your local machine:

$ git clone <repository‑url>

Push Changes to a Remote

Upload local commits to a remote repository:

$ git push

Pull Changes from a Remote

Download and integrate changes from a remote repository:

$ git pull

Resolve Merge Conflicts

If Git cannot automatically merge changes, manually edit the conflicted files using a text editor or merge tool.

Rebase a Branch

Move an entire branch onto a new base commit to maintain a linear history:

$ git rebase <branch-name>

Stash Changes

Temporarily hide uncommitted changes to switch contexts:

$ git stash

Advanced Git Commands

Checkout a Specific Commit

Return to a previous commit by its hash:

$ git checkout <commit‑hash>

Amend a Commit

Modify the most recent commit without creating a new one:

$ git commit --amend

Cherry‑Pick

Apply a specific commit from one branch onto another:

$ git cherry-pick <commit‑hash>

Git Bisect

Perform a binary search through commit history to locate a bug:

$ git bisect start
$ git bisect bad
$ git bisect good <commit‑hash>

Git Blame

Identify who last modified each line of a file:

$ git blame <filename>

Git Tag

Create a tag to mark a specific commit, often for releases:

$ git tag <tag‑name> <commit‑hash>

Conclusion

This article covered the most commonly used Git commands, from basic to advanced. Mastering these commands enables developers to navigate repositories, collaborate effectively, and manage codebases efficiently. While these commands are essential, Git offers many more options and parameters worth exploring.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

GitVersion Control
21CTO
Written by

21CTO

21CTO (21CTO.com) offers developers community, training, and services, making it your go‑to learning and service platform.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.