DEV Community

Cover image for Git it Right🔥🔥🚀(Git CheatSheet)
Atharva Shirdhankar
Atharva Shirdhankar

Posted on • Edited on

Git it Right🔥🔥🚀(Git CheatSheet)

Alt Text
Git commands cheatsheet📁

The Essentials — When working with git on your own or with others
  • git status - To remind you of where you left off. See a summary of local changes, remote commits, and untracked files.
  • git diff - To see the specific local changes to tracked files. Use --name-only to see changed filenames.
  • git add - To stage changes to your tracked and untracked files. Use -u, -a, and . strategically.
  • git commit - To create a new commit with changes previously added. Use -m and add a meaningful commit message.
  • git push - To send changes to your configured remoterepository, most commonly GitLab or GitHub.

Basic Flow — Daily usage of git, including flags

1. cd to your local project that you want to start versioning with git. You only have to run git init the first time to set up the directory for version tracking.
git init 
git status 
git add --all 
git status 
git commit -m "meaningful initial commit message"
git show 
Enter fullscreen mode Exit fullscreen mode
2. And you begin to hack on your local files, then commit at regular intervals.
git diff 
git commit -a -m "Another commit message. -a performs the add step for you"
git status 
git log --graph --pretty=oneline --abbrev-commit
Enter fullscreen mode Exit fullscreen mode
3. After a while, you have 3 commits that would be more meaningful as a single commit
git log --graph --pretty=oneline --abbrev-commit
git reset --soft HEAD~3 
git diff --cached 
git commit -a -m "Better commit 
message for last 3 commits" 
Enter fullscreen mode Exit fullscreen mode
4. Lastly, you delete some unneeded files in the current directory
git status 
git diff --cached 
git add -u 
git commit -m "Another commit message. -u adds updates, including deleted files"
git status 
git log --graph --pretty=oneline --abbrev-commit
git push origin master
Enter fullscreen mode Exit fullscreen mode
Basic Branching — Branches represent a series of commits.
  • git branch --all - list all local and remote branches
  • git checkout <branch> - change to an existing branch
  • git checkout -b <branch> master - make a branch based off of masterand check it out
  • git checkout master && git merge <branch> - merge branch changes onto master
Important Flags — These are my personal favorites for keeping everything organized.
  • git reset HEAD -- - get back to the last known commit and unstage others
  • git add -u - add only the updated, previously committed files
  • git log --graph --pretty=oneline --abbrev-commit - for a pretty branch history. Create a shell or git alias for easy access,such as git lg
Working with a Remote Repository — Once you get into the flow, you’ll frequently contribute back to larger projects, and possibly managing forks of forks. Here are some tips for doing so.
  • git fetch --all - downloads all commits, files, and references to branches on all remote repositories so you can then git checkout or pull what you want to work on.
  • git pull --rebase <remote> <branch> - Merge all commits since your last common commit from the remote branch without creating a merge commit.
  • git stash - Use this as needed to save uncommitted changes so you can git stash pop them onto a different branch.
  • git stash pop - bring it back
  • git add [-A or . or -- <filename>] - Be intentional about what files you add to your commits, especially if you want to open a request to merge them into an upstream project.
  • git commit -m "commit message" - Most projects have a format they prefer for commit messages. Look at CONTRIBUTING.md files in the project or review previous commits to get an idea of their format.
  • git push origin <branch> - Push your current branch to your remote titled “origin” and branch named
  • git checkout -b <new_branch> - A shortcut for git branch && git checkout branch. It’s great for when you want to experiment with an idea and have a new branch to try it out on that can later be merged or deleted.
  • git checkout master && git pull --rebase - Great to get to the most recent commit for a project you only infrequently follow.
  • git reset --hard origin/master - For when you inevitably get lost in all the git-fu and need to get to a known state. WARNING: this erases all changes, even commits, since the last commit pushed to the remote origin on branch master.
  • git push origin master - For when you inevitably do something right! Send your changes up to your remote titled origin on branch master.
Getting Help
  • git <cmd> -h - great for quick review of command flags
  • git <cmd> --help - to dig into the full man pages of the command

If you like my work do consider buying me a book your small contribution will help be to bring more awesome content.

Buy Me A Coffee

Top comments (6)

Collapse
 
bobbyiliev profile image
Bobby Iliev

Well done for putting this great list of Git commands!

For anyone interested in learning more about Git in general, here is a free opensource eBook:

GitHub logo bobbyiliev / introduction-to-git-and-github-ebook

Free Introduction to Git and GitHub eBook

💡 Introduction to Git and GitHub

This is an open-source introduction to Git and GitHub guide that will help you learn the basics of version control and start using Git for your SysOps, DevOps, and Dev projects. No matter if you are a DevOps/SysOps engineer, developer, or just a Linux enthusiast, you can use Git to track your code changes and collaborate with other members of your team or open source maintainers.

The guide is suitable for anyone working as a developer, system administrator, or a DevOps engineer and wants to learn the basics of Git, GitHub and version control in general.

🚀 Download

To download a copy of the eBook use one of the following links:

📘 Chapters

Collapse
 
star_trooper profile image
Atharva Shirdhankar

That's awesome 🔥🔥🔥🚀.
You have put everything (content) to get started with open source journey at one place⭐.

Collapse
 
seokjeon profile image
Se-ok Jeon

Thx for this! This is really what I wanted. Helped A LOT.
Can I translate in Korean this article? If you don't mind, I wanna share this awesome information in Korean. Surely, There will be a link directing to this original one.

Collapse
 
star_trooper profile image
Atharva Shirdhankar

Sure go ahead 👍

Collapse
 
luismacalderon profile image
Luis Calderón M.

Thanks, useful !

Collapse
 
larsejaas profile image
Lars Ejaas

Funny this one popped up in my Facebook feed just as I am preparing for a tech interview and trying to read up on GIT to get more comfortable with the different commands!
Thanks for the article!