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.
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.txtView the Log
Show the commit history:
$ git log
Author: 洛逸 <[email protected]>
Date: Thu May 18 15:46:58 2023 +0800
post messageCreate a Branch
Create a new branch to work independently:
$ git branch <branch-name>
# e.g., git branch developmentSwitch Between Branches
Change the current branch:
$ git checkout <branch-name>
# e.g., git checkout main
Already on 'main'
git checkout developmentMerge a Branch
Merge a branch into the current branch:
$ git merge <branch-name>
# e.g., git merge developmentIntermediate 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 pushPull Changes from a Remote
Download and integrate changes from a remote repository:
$ git pullResolve 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 stashAdvanced 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 --amendCherry‑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.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
21CTO
21CTO (21CTO.com) offers developers community, training, and services, making it your go‑to learning and service platform.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
