Skip to content

Instantly share code, notes, and snippets.

@BolajiAyodeji
Last active May 7, 2022 05:09
  • Star 33 You must be signed in to star a gist
  • Fork 17 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save BolajiAyodeji/d5393b90650c49ba9ef81493bfca9cda to your computer and use it in GitHub Desktop.
command usage
git init Creates an empty Git repository in the specified directory.
git clone <repository name> Clones a repository located at <repository name> onto your local machine.
git add <directory> Stages only the specified changes for the next commit. Replace <directory> with a <file> to change a specific file.
git add . Stages new files and modifications without deletions
git add -A Stages all changes
git add -all Equivalent to git add -A
git add -u Stages modifications and deletions without adding new files
git add --update Equivalent to git add -u
git commit -m ”<message>” Commits the staged snapshot. replace <message> with the commit message.
git status List which files are staged unstaged and untracked.
git log Displays the entire commit history using the default format.
git diff Shows unstaged changes between your index and working directory.
git pull Fetchs the remote copy of the current branch.
git pull --rebase <remote> 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 master Push all of your commits to master branch.
git push <remote> --all Push all of your local branches to the specified remote.
git push <remote> --tags Tags 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 <remote> --force Forces 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 <commit> Creates new commit that undoes all of the changes made in <commit> and then applys it to the current branch.
git reset <file> Removes <file> from the staging area but leaves the working directory unchanged - This unstages a file without overwriting any changes.
git clean -n Shows which files would be removed from working directory. Use the -f flag in place of the -n flag to execute the clean.
git commit --amend Replaces the last commit with the staged changes and last commit combined. Use with nothing staged to edit the last commit’s message.
git rebase <base> Rebase the current branch onto <base>. <base> can be a commit ID a branch name a tag or a relative reference to HEAD.
git reflog Show 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 branch Lists all of the branches in your repo.
git branch <branch name> Creates a new branch with the name <branch name>.
git checkout -b <branch name> Creates and check out a new branch named <branch name>.
git checkout <branch name> Checkout an existing branch.
git merge <branch> Merge <branch> into the current branch.
git remote add <name> <url> Creates a new connecti
git log --stat Include which files were altered and the relative number of lineson to a remote repo. After adding a remote you can use <name> as a shortcut for <url> in other commands.
git fetch <remote> <branch> Fetches a specific <branch> from the repo. Leave off <branch> to fetch all remote refs.
git pull <remote> Fetches the specified remote’s copy of current branch and immediately merge it into the local copy.
git push <remote> <branch> Pushes the branch to <remote> along with necessary commits and objects. Creates named branch in the remote repo if it doesn’t exist.
git config --global user.name <name> Defines the author name to be used for all commits by the current user.
git config --global user.email <email> Defines the author email to be used for all commits by the current user.
git config --global alias. <alias-name> <git-command> Creates shortcut for a Git command. E.g. alias.p push will set git p equivalent to git push.
git config --system core.editor <editor> Set text editor used by commands for all users on the machine. <editor> arg should be the command that launches the desired editor (e.g; vi).
git config --global --edit Opens the global configuration file in a text editor for manual editing.
git log -<limit> Limits the number of git rebase -i E.g. git log -5 will limit to 5 commits.
git log --oneline Condenses each commit to a single line.
git log -p Displays the full diff of each commit.
git log --stat Include which files were altered and the relative number of lines that were added or deleted from each of them.
git log --author= ”<pattern>” Searchs for commits by a particular author.
git log --grep=”<pattern>” Searchs for commits with a commit message that matches <pattern>.
git log <since>..<until> Shows commits that occur between <since> and <until>. Args can be a any kind of revision reference.
git log -- <file> 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 HEAD Shows difference between working directory and last commit.
git diff --cached Shows difference between staged changes and last commit
git reset Resets the staging area to match most recent commit but leaves the working directory unchanged.
git reset --hard Resets the staging area and working directory to match most recent commit and overwrites all changes in the working directory.
git reset <commit> Moves the current branch tip backward to <commit> resets the staging area to match but leaves the working directory unchanged.
git reset --hard <commit> Same as previous but resets both the staging area & working directory to match. Deletes uncommitted changes and all commits after <commit>.
git rebase -i <base> Interactively rebase current branch onto <base>. Launches editor to enter commands for how each commit will be transferred to the new base.
@BolajiAyodeji
Copy link
Author

BolajiAyodeji commented Feb 17, 2019

Thread here!

Got any questions, views, or proposed edits? Feel free to drop them here.

You should also check out this article where I shared some version control best practices alongside the cheat sheet.

Don't forget to leave a 🌟 if this is helpful, thanks.

@Erandkings
Copy link

Nice piece. Thanks

@bintjahsh
Copy link

This is very resourceful!

@BolajiAyodeji
Copy link
Author

Nice piece. Thanks

Glad you love it!

@BolajiAyodeji
Copy link
Author

BolajiAyodeji commented Feb 18, 2019

This is very resourceful!

Glad you love it!

@ajepe
Copy link

ajepe commented Feb 18, 2019

This is also handy: to compare two branches
git diff branch_a..branch_b

@BolajiAyodeji
Copy link
Author

This is also handy: to compare two branches
git diff branch_a..branch_b

Thanks!

@jimmyurl
Copy link

Good stuff bro!

@BolajiAyodeji
Copy link
Author

Good stuff bro!

Thank you!

@tracy-codes
Copy link

Starring because this is a solid resource. Even after 3 years of daily git usage, some commands still fly over my head. Thank you! ⚡

@jpinnix
Copy link

jpinnix commented Feb 18, 2019

@BolajiAyodeji
Copy link
Author

Starring because this is a solid resource. Even after 3 years of daily git usage, some commands still fly over my head. Thank you! ⚡

I'm glad this helped you, thanks!

@BolajiAyodeji
Copy link
Author

@BolajiAyodeji
Copy link
Author

BolajiAyodeji commented Feb 19, 2019

You should also check here: https://blog.bolajiayodeji.com/git-cheat-sheet-for-beginners-and-intermediates.

I added some VERSION CONTROL BEST PRACTISES in that article.

@JaybeeClassical
Copy link

Thanks for this

@kyleweishaar
Copy link

Very helpful, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment