Git Cheat Sheet for Beginners and Intermediates

Git Cheat Sheet for Beginners and Intermediates

Git cheat sheet saves you time when you just can't remember a specific command. It is hard to memorize all the important Git commands as a newbie, most times Senior Developers forget too. This is why you need a reference you can come back to when you get stuck.

In this article, I'd show you the basic Git commands to help you learn Git, and more advanced concepts around Git branches, remote repositories, reverting changes, and more. At the end of this article, I'd also show you some Version Control Best Practices.

Let's get started :)

Cheat Sheet Contents

  • Git Basics
  • Reverting Changes
  • Rewriting Git History
  • Git Branches
  • Remote Repositories
  • git config
  • git log
  • git dif
  • git reset
  • Version Control best practises

I believe you already know git and you probably should be using it, but in case you don't know:

Git is a free and open source distributed version control system designed to handle everything from small to very large projects with speed and efficiency. Git manages your source code history and versions. More details here

Cheat Sheet

commandusage
git initCreates an empty Git repository in the specified directory.
git clone Clones a repository located at onto your local machine.
git add Stages only the specified changes for the next commit. Replace with a to change a specific file.
git add .Stages new files and modifications without deletions
git add -AStages all changes
git add -allEquivalent to git add -A
git add -uStages modifications and deletions without adding new files
git add --updateEquivalent to git add -u
git commit -m ””Commits the staged snapshot. replace with the commit message.
git statusList which files are staged unstaged and untracked.
git logDisplays the entire commit history using the default format.
git diffShows unstaged changes between your index and working directory.
git pullFetchs the remote copy of the current branch.
git pull --rebase Fetchs the remote copy of current branch and rebases it into the local copy. Use git rebase instead of merge to integrate the branches.
git push origin masterPush all of your commits to master branch.
git push --allPush all of your local branches to the specified remote.
git push --tagsTags aren’t automatically pushed when you push a branch or use the --all flag. The --tags flag sends all of your local tags to the remote repo.
git push --forceForces the git push even if it results in a non-fast-forward merge. Do not use the --force flag unless you’re absolutely sure you know what you’re doing.
git revert Creates new commit that undoes all of the changes made in and then applys it to the current branch.
git reset Removes from the staging area but leaves the working directory unchanged - This unstages a file without overwriting any changes.
git clean -nShows which files would be removed from working directory. Use the -f flag in place of the -n flag to execute the clean.
git commit --amendReplaces the last commit with the staged changes and last commit combined. Use with nothing staged to edit the last commit’s message.
git rebase Rebase the current branch onto . can be a commit ID a branch name a tag or a relative reference to HEAD.
git reflogShow a log of changes to the local repository’s HEAD. Add --relative-date flag to show date info or --all to show all refs.
git branchLists all of the branches in your repo.
git branch Creates a new branch with the name .
git checkout -b Creates and check out a new branch named .
git checkout Checkout an existing branch.
git merge Merge into the current branch.
git remote add Creates a new connecti
git log --statInclude which files were altered and the relative number of lineson to a remote repo. After adding a remote you can use as a shortcut for in other commands.
git fetch Fetches a specific from the repo. Leave off to fetch all remote refs.
git pull Fetches the specified remote’s copy of current branch and immediately merge it into the local copy.
git push Pushes the branch to along with necessary commits and objects. Creates named branch in the remote repo if it doesn’t exist.
git config --global user.name Defines the author name to be used for all commits by the current user.
git config --global user.email Defines the author email to be used for all commits by the current user.
git config --global alias. Creates shortcut for a Git command. E.g. alias.p push will set git p equivalent to git push.
git config --system core.editor Set text editor used by commands for all users on the machine. arg should be the command that launches the desired editor (e.g; vi).
git config --global --editOpens the global configuration file in a text editor for manual editing.
git log -Limits the number of git rebase -i E.g. git log -5 will limit to 5 commits.
git log --onelineCondenses each commit to a single line.
git log -pDisplays the full diff of each commit.
git log --statInclude which files were altered and the relative number of lines that were added or deleted from each of them.
git log --author= ””Searchs for commits by a particular author.
git log --grep=””Searchs for commits with a commit message that matches .
git log ..Shows commits that occur between and . Args can be a any kind of revision reference.
git log -- Only display commits that have the specified file.
git log --graph --decorate--graph flag draws a text based graph of commits on left side of commit msgs. --decorate adds names of branches or tags of commits shown.
git diff HEADShows difference between working directory and last commit.
git diff --cachedShows difference between staged changes and last commit
git resetResets the staging area to match most recent commit but leaves the working directory unchanged.
git reset --hardResets the staging area and working directory to match most recent commit and overwrites all changes in the working directory.
git reset Moves the current branch tip backward to resets the staging area to match but leaves the working directory unchanged.
git reset --hard Same as previous but resets both the staging area & working directory to match. Deletes uncommitted changes and all commits after .
git rebase -i Interactively rebase current branch onto . Launches editor to enter commands for how each commit will be transferred to the new base.

Version Control Best Practises

  • Commit Related Changes
  • Don't commit an unfinished work
  • Your commit message should be clear enough and describes changes you made
  • Add screenshots were possible to describe your changes better
  • Ensure you open an issue first before you work on a new feature
  • When pushing a new feature that was not discussed before, add a reason why you made that commit
  • Test your commits before you push
  • Use branches to work on new features
  • Always pull before you push
  • Fix merge conflicts as soon as possible

Conclusion

Git is a tricky tool to get your head around. Knowing the commands is one thing, but knowing how and when to use them is another. This cheat-sheet would come in handy whenever you forget any command. Do share with your friends also, :)

Reference