Home Git The ultimate Git cheat sheet for developers

The ultimate Git cheat sheet for developers

Git, an essential tool for developers, offers a robust system for version control. Our guide provides a comprehensive exploration of git commands, paving the way for mastery in collaborative coding projects.

by Arun Kumar
Published: Last Updated on
the ultimate git cheat sheet

If you have landed on this page, it’s highly likely that you’re a Git user who is eager to streamline your Git skills. Git is a powerful and indispensable tool for version control, and as someone who has had their fair share of “Oops, what did I just do?!” moments, I can attest to the significance of having a well-curated list of Git commands.

In this comprehensive guide, I’m offering my carefully crafted Git command cheat sheet, consisting of commands that have saved my bacon countless times. Along with that, I’ll be sharing my tip about these commands that I love, and sometimes, what I’d secretly wish was different about them. With this guide, you’ll be able to level up your Git game and take your version control skills to the next level.

Git cheat sheet

1. Setting up and initialization

The initiation phase in Git revolves around setting up your environment. Whether you’re just starting out or cloning an existing repository to collaborate, these commands lay the foundation for your Git journey. Personalizing with your user name and email ensures your commits are easily identifiable, making collaboration smoother.

Command Description Tip
git init Initialize a new git repository. Felt like the opening scene of a movie.
git clone [url] Clone a repository from an existing URL. Always be in the right directory.
git config user.name "[name]" Set a name associated with commits. Set once, and you’re good to go.
git config user.email "[email address]" Set an email for commits. Essential for identifying your commits.
git config --global color.ui auto Enable color-coded terminal. A splash of color for clarity.
git alias [alias-name] [git-command] Create shortcuts for git commands. Time-savers for lengthy commands.
git remote add [name] [url] Connect your repo to a remote one. Like adding a friend’s number to speed dial.
git remote -v List remote connections to repos. Check your external connections.
git remote show [name] Get more info about a remote repo. Dive deeper into a connection’s details.
git remote rename [old-name] [new-name] Rename a remote connection. Because sometimes, we change our minds.
git remote remove [name] Remove a remote connection. Cutting ties for a cleaner setup.

2. Basic snapshotting

Snapshotting in Git is the process of tracking and recording changes in your files. It’s like taking a photo of your code at various stages. These commands are fundamental and frequently used, allowing you to add, commit, and manage the state of your files.

Command Description Tip
git add [file-name] Add a specific file to the staging area. A step closer to immortalizing code.
git commit -m "[message]" Commit changes with a message. Detailed messages are breadcrumbs.
git commit -am "[message]" Add and commit in one step. Efficiency at its best.
git rm [file-name] Remove a file from the working tree. Sometimes, decluttering is necessary.
git mv [old-file-name] [new-file-name] Rename or move files. A clean way to organize.
git stash Temporarily store changes. Like a pause button for your changes.
git stash pop Restore stored changes. Resume where you left off.
git stash list List all stashed changesets. Keep track of your stashes.
git stash drop [stash-id] Remove a specific stashed changeset. Decluttering stashes.
git clean -n Show files that would be removed. Preview before you clean.
git clean -f Remove untracked files. A clean slate.

3. Branching & merging

Think of branching as creating multiple parallel universes of your codebase, each evolving independently. Merging is the art of bringing those parallel lines back into one. These commands help you navigate, manage, and merge these branches, ensuring a fluid development process.

Command Description Tip
git branch List all the branches. Quick glance at your branches.
git branch [branch-name] Create a new branch. New beginnings in code.
git branch -d [branch-name] Delete a branch. Tidy up finished work.
git branch -m [old-name] [new-name] Rename a branch. A fresh identity for evolving work.
git checkout [branch-name] Switch to another branch. Step into another dimension of your project.
git checkout -b [branch-name] Create and switch to a new branch. Two steps in one.
git merge [branch-name] Merge a branch into the current one. Collaborative efforts come together.
git mergetool Use a GUI tool to help during merge conflicts. Visual aid for complex merges.
git rebase [branch-name] Apply commits from one branch onto another. A linear history for cleaner understanding.
git cherry-pick [commit-id] Apply changes from specific commits. Pick only what you need.
git diff [branch-name] See changes between branches. Insight before merging.

4. Inspecting & comparing

Keeping a watchful eye on your project’s history and changes is essential for effective version control. These commands offer a magnifying glass into your code, allowing you to analyze changes, dive deep into logs, and understand the evolution of your project.

Command Description Tip
git status Show modified files. A health check for your repo.
git log Display commit logs. Dive into your project’s history.
git log --oneline Show commit logs in short format. A condensed view for quicker insights.
git log --stat Include statistics of changes in logs. A more detailed view.
git log -p [file-name] Show changes of a specific file over time. Track evolution of a file.
git blame [file-name] Show who changed what in a file. Understand contributions (or find who introduced that bug!).
git diff Show changes between commits, commit and working tree, etc. Compare and contrast your code.
git diff --staged Compare staged changes to the last commit. Pre-insight before a commit.
git tag [tag-name] Create a tag at the current commit. Marking milestones in your project.
git show [tag-name] Show information about a tag. Reflect on key moments in your repo.
git tag -a [tag-name] -m "[message]" Create an annotated tag with a message. Descriptive milestones.

5. Sharing & collaboration

Coding isn’t just a solitary endeavor; it thrives on collaboration. This section is all about pushing code to shared repositories, pulling updates, and synchronizing with remote changes. It’s the bridge between your local environment and the broader world of collaborative coding.

Command Description Tip
git push [remote] [branch-name] Push branch to remote repository. Share your work with the world.
git pull [remote] [branch-name] Fetch and merge from the remote. Stay updated with team’s work.
git push --tags Push all tags to the remote repository. Share milestones with everyone.
git fetch [remote] Fetch changes from a remote repository. Get updates without merging.
git remote prune [remote-name] Remove references to stale branches on remote. Clean up old references.
git push [remote] :[branch-name] Delete a branch on the remote repository. Tidy up remote workspaces.
git bisect start Use binary search to find a commit that introduced a bug. Smart debugging.
git bisect bad [commit-id] Mark a commit as responsible for the bug. Narrow down the problem.
git bisect good [commit-id] Mark a commit as being before the bug. Further narrow the search.
git bisect reset Finish the bisect session. End of the debugging session.
git fork Create a personal copy of another user’s repository. Clone, then evolve independently.

6. Undoing changes

Everyone makes mistakes, even the best developers. What distinguishes a great developer is their ability to quickly identify and fix errors. Fortunately, there are simple commands that can serve as a safety net, allowing you to reverse changes, go back to previous versions, and keep your codebase clean and error-free.

Command Description Tip
git reset [file-name] Unstage a file but retain modifications. Precision in undoing.
git revert [commit-id] Undo a commit by creating a new one. A safe undo button.
git checkout -- [file-name] Discard changes in directory. Like hitting ‘revert to original’.
git reset HEAD~1 Undo the last commit. Quick step back in time.
git reflog Show a log of where your HEAD and branch references have been. A deeper history tool.
git reset --hard [commit-id] Reset your HEAD, index and working tree to another commit. A hard reset for major undos.
git clean -fd Remove untracked files and directories. Clean slate, clean workspace.
git reset --soft [commit-id] Move HEAD to another commit, but keep staged changes. Soft undos, gentle rollbacks.
git commit --amend Modify the last commit. Perfect for forgotten bits.
git restore --source=[commit-id] [file-name] Restore a file to its state at a specific commit. Revisit an older version.
git restore --staged [file-name] Unstage a file without overwriting any changes. Adjust before a commit.

Conclusion

Have you ever found yourself struggling to remember how to use certain git commands? If so, you’re not alone. Luckily, with the help of this comprehensive cheat sheet, navigating git commands will be a breeze. Not only does it provide clear step-by-step instructions, but it also includes some funny tips based on my own experiences. Coding is not just about finding solutions to problems, it’s also about enjoying the journey. So, use this cheat sheet to make your coding journey smoother, and may all of your merges be conflict-free!

You may also like

Leave a Comment

fl_logo_v3_footer

ENHANCE YOUR LINUX EXPERIENCE.



FOSS Linux is a leading resource for Linux enthusiasts and professionals alike. With a focus on providing the best Linux tutorials, open-source apps, news, and reviews written by team of expert authors. FOSS Linux is the go-to source for all things Linux.

Whether you’re a beginner or an experienced user, FOSS Linux has something for everyone.

Follow Us

Subscribe

©2016-2023 FOSS LINUX

A PART OF VIBRANT LEAF MEDIA COMPANY.

ALL RIGHTS RESERVED.

“Linux” is the registered trademark by Linus Torvalds in the U.S. and other countries.