DEV Community

Cover image for Git Mastery Cheatsheet: Level Up Your Git Game
Ahmed_hamdy
Ahmed_hamdy

Posted on

2 1 1 1

Git Mastery Cheatsheet: Level Up Your Git Game

**Introduction to Git
**Git is a powerful distributed version control system that lets you track changes, collaborate seamlessly, and maintain a robust project history. Whether you're new to Git or an experienced developer, this cheatsheet provides quick, essential references for both basic and advanced Git commands to enhance your workflow.

Basic Git Commands
Initialization
Initialize a new Git repository in your current directory:

git init
Enter fullscreen mode Exit fullscreen mode

This command creates a hidden

.git
directory that starts tracking your project.

Cloning a Repository
Clone an existing repository from a remote source:

git clone <repository_url>
Enter fullscreen mode Exit fullscreen mode

This command downloads the repository to your local machine.

Staging Changes
Add files or directories to the staging area:

git add <file_or_directory>
Enter fullscreen mode Exit fullscreen mode

Stage all modified files:

git add .
Enter fullscreen mode Exit fullscreen mode

Committing Changes
Commit your staged changes with a message describing the update:

git commit -m "Your commit message"
Enter fullscreen mode Exit fullscreen mode

Checking Repository Status
See the current state of your repository, including staged, modified, and untracked files:

git status
Enter fullscreen mode Exit fullscreen mode

Viewing Differences
View changes between your working directory and the staging area:

git diff

Enter fullscreen mode Exit fullscreen mode

See differences between the staging area and the last commit:

`


git diff --cached

`
Branching
Manage branches to work on multiple features simultaneously:

Create a new branch:

git branch <branch_name>
Enter fullscreen mode Exit fullscreen mode

Switch to an existing branch:


git checkout <branch_name>
Enter fullscreen mode Exit fullscreen mode

Create and switch to a new branch in one step:

git checkout -b <branch_name>

Enter fullscreen mode Exit fullscreen mode

Merge a branch into the current branch:

git merge <branch_name>

Enter fullscreen mode Exit fullscreen mode

Pushing and Pulling Changes
Push your commits to a remote repository:

git push <remote> <branch>

Enter fullscreen mode Exit fullscreen mode

Pull updates from the remote repository to your local branch:

git pull <remote> <branch>

Enter fullscreen mode Exit fullscreen mode

Viewing Commit History
List the commit history to review past changes:

git log

Enter fullscreen mode Exit fullscreen mode

Advanced Git Commands
Reverting Changes
Undo changes by reverting a specific commit. This creates a new commit that reverses the changes:

git revert <commit_hash>

Enter fullscreen mode Exit fullscreen mode

Resetting Commits
Undo commits with different levels of rollback:

Soft reset (keeps changes staged):

git reset --soft HEAD~1

Enter fullscreen mode Exit fullscreen mode

Mixed reset (unstages changes):

git reset --mixed HEAD~1

Enter fullscreen mode Exit fullscreen mode

Hard reset (discards all changes):

git reset --hard HEAD~1

Enter fullscreen mode Exit fullscreen mode

Rebasing
Reapply your commits on top of another branch:

git rebase <branch_name>

Enter fullscreen mode Exit fullscreen mode

Stashing Changes
Temporarily save uncommitted changes to work on something else:

Stash changes:

git stash

Enter fullscreen mode Exit fullscreen mode

Reapply stashed changes:

git stash apply

Enter fullscreen mode Exit fullscreen mode

Cherry-Picking
Apply a specific commit from one branch onto the current branch:

git cherry-pick <commit_hash>

Enter fullscreen mode Exit fullscreen mode

Git Hooks
Customize Git's behavior with hooks. Place executable scripts in the .git/hooks directory (e.g., pre-commit, post-merge) to run custom actions before or after Git events.

Git Aliases
Create shortcuts for commonly used Git commands:

git config --global alias.<alias_name> '<command>'

Enter fullscreen mode Exit fullscreen mode

Example:

git config --global alias.co 'checkout'

Enter fullscreen mode Exit fullscreen mode

Git Workflows
Adopt one of these common workflows to manage your project:

Centralized Workflow: Single main branch for direct commits.
Feature Branch Workflow: Separate branches for new features or fixes, merged back when complete.
Gitflow Workflow: Structured branching model with dedicated branches for development (develop), releases (release), and features.
Conclusion
Mastering Git is essential for effective version control and collaboration. Use this cheatsheet as your quick reference guide to become more proficient with Git, streamline your workflow, and manage your projects with confidence.

Connect with me on LinkedIn and follow my work on GitHub.

Happy coding!

Top comments (0)

👋 Kindness is contagious

If this post resonated with you, feel free to hit ❤️ or leave a quick comment to share your thoughts!

Okay