- Published on
Mastering Git Main Level Commands: A Comprehensive Guide
Introduction to Git Command
Git is a distributed version control system that allows multiple people to work on a project simultaneously without interfering with each other. It tracks changes in source code during software development.
Basic Commands:# Initialize a new Git repository
git init
# Clone an existing repository
git clone https://github.com/user/repo.git
# Check the status of your repository
git status
# Add files to staging area
git add .
# Commit changes to the repository
git commit -m "Your commit message"
# Push changes to remote repository
git push origin main
Initialized empty Git repository in /path/to/repo/.git/
Branching in Git
Branching allows you to diverge from the main line of development and continue to do work without messing with that main line.
Create and switch to a new branch:git checkout -b new-branch
Switched to a new branch 'new-branch'
Merging in Git
Merging is used to integrate changes from different branches.
Merge new-branch into main:git checkout main
git merge new-branch
Updating abc123..def456
Fast-forward
file1.txt | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
Rebase Introduction
Rebasing re-applies commits on top of another base tip.
Rebase feature-branch onto main:git checkout feature-branch
git rebase main
First, rewinding head to replay your work on top of it...
Applying: Add new feature
Detach yo' HEAD
A detached HEAD state means Git is not on a branch but on a specific commit.
Checkout a specific commit:git checkout <commit-hash>
Note: switching to 'abc123'.
You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by switching back to a branch.
Relative Refs (^)
Caret (^) is used to reference parent commits.
Move one commit back:git checkout HEAD^
HEAD is now at def456 Previous commit message
Relative Refs (~)
Tilde (~) is used to reference ancestor commits.
Move three commits back:git checkout HEAD~3
HEAD is now at abc123 Three commits ago message
Reversing Changes in Git
Undo changes or revert commits.
Revert a commit:git revert <commit-hash>
[main abc123] Revert "Commit message"
1 file changed, 1 insertion(+), 1 deletion(-)
Cherry-pick Intro
Cherry-pick allows you to apply changes from specific commits to your current branch.
Cherry-pick a commit:git cherry-pick <commit-hash>
[main abc123] Commit message
1 file changed, 1 insertion(+), 1 deletion(-)
Interactive Rebase Intro
Interactive rebase allows you to edit commits before applying them.
Start an interactive rebase:git rebase -i HEAD~3
pick abc123 Commit message
pick def456 Another commit
pick ghi789 Yet another commit
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
Grabbing Just 1 Commit
Apply a single commit from another branch.
Cherry-pick from another branch:git cherry-pick <commit-hash>
[main abc123] Commit message
1 file changed, 1 insertion(+), 1 deletion(-)
Juggling Commits
Reorder or modify commits using interactive rebase.
Rebase interactively to reorder commits:git rebase -i HEAD~3
pick abc123 Commit message
pick def456 Another commit
pick ghi789 Yet another commit
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
Juggling Commits #2
Amend commits or split them.
Amend the last commit:git commit --amend
[main abc123] Commit message
Date: Thu Jul 28 10:00:00 2024 -0700
1 file changed, 1 insertion(+), 1 deletion(-)
Git Tags
Tags mark specific points in history as important.
Create a tag:git tag v1.0
v1.0
Git Describe
Describe is used to find the most recent tag reachable from a commit.
Describe the current commit:git describe
v1.0-2-gabc123
Rebasing Over Multiple Times
Rebase multiple branches or commits.
Rebase multiple commits:git rebase main feature-branch
First, rewinding head to replay your work on top of it...
Applying: Add new feature
Multiple Parents
Multiple parents refer to merge commits.
Merge two branches:git merge feature-branch
Merge made by the 'recursive' strategy.
file1.txt | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
Branch Spaghetti
Managing a complex branch structure.
Visualize branch structure:git log --oneline --graph --all
* abc123 (HEAD -> main) Commit message
| * def456 (feature-branch) Another commit
|/
* ghi789 Previous commit
With these commands, you may successfully manage numerous Git operations. Understanding these commands can help you improve your version control skills and streamline the development process.