Skip to content

MrKrishnaAgarwal/Git-CheatSheet

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

72 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Git Cheat Sheet

A quick reference guide to the most commonly used Git commands

Here is a list of the most common git commands. I've included the most common options for each command, but you can always run git help <command> to get more information. This repository also lists some great resources and books for learning git.

πŸ–₯️ Setting up your Git information

From scratch -- Set the name associated with your user account. git config --global user.name "NAME"
Set the email associated with your user account. git config --global user.email "youremail@abc.com"
Set your default editor: git config --global core.editor "vim"
Set your default merge tool: git config --global merge.tool "vimdiff"
Set your default push behaviour: git config --global push.default "simple"
Set your default pull behaviour: git config --global pull.rebase "true"
Set your default branch name: git config --global init.defaultBranch "main"
Set your default credential helper: git config --global credential.helper "cache --timeout=3600"

πŸ›  Create a Repository

Create a new local repository: git init [project name]
Download from an existing repository: git clone my_url

πŸ”Ž Observe your Repository

To list new or modified files not yet committed: git status
To show the changes to files not yet staged: git diff
To show the changes to staged files: git diff --cached
To show all staged and unstaged file changes: git diff HEAD
To show the changes between two commit ids: git diff commit1 commit2
To list the change dates and authors for a file: git blame [file]
To show the file changes for a commit id and/or file: git show [commit]: [file]
To show full change history: git log
To show the change history for file/directory including diffs: git log -p [file/directory]
To show the change history for a specific author: git log --author="[author name]"

🌴 Working with Branches

To list all local branches: git branch
To list remote and local branches: git branch -a
To switch to an existing branch, branch_name, and update the working directory: git checkout branch_name
To switch to the last used branch: git checkout -
To create a new branch called the new branch: git branch branch_name
To create a local branch and switch to it: git checkout -b branch_name
To delete the branch called my_branch: git branch -d my_branch
To push a branch to remote: git push origin branch_name
To rename a current branch: git branch -m new_name
To merge branch _a into branch_b: git checkout branch_b git merge branch_a
To abort the current merge: git merge --abort
To tag the current commit: git tag my_tag
To discard all local commits and changes: git reset --hard origin/<remote_branch_name>

πŸ‘› Make a change

To stage the file, ready for commit: git add [file]
To stage all changed files, ready for commit: git add .
To commit all staged files to the versioned history: git commit -m "commit message"
To commit all your tracked files to the versioned history: git commit -am "commit message
To set the executable flag to a file foo.sh: git update-index --chmod=+x foo.sh
To unstaged the file, keeping the file changes: git reset [file]
To revert everything to the last commit: git reset --hard
To overwrite commit history with your local history (force push): git push --force
To reset remote branch to specific commit (danger: use only if not distributed to other people before): git reset --hard <commit-hash> && git push -f origin <branch-name>

🚰 Synchronize

To get the latest changes from the origin (no merge): git fetch
To fetch the latest changes from the origin and merge: git pull
To fetch the latest changes from the origin and rebase: git pull --rebase
To push local changes to the origin: git push

🧾 Logs and History

To show commit history in single lines: git log --oneline
To show the commit history for the last N commits: git log -2
To show commit history for the last N commits with diff: git log -p -2
To show reflog history for emergency actions: git reflog
To show all local file changes in the working tree: git diff
To show changes made to a file: git diff myfile
To show who changed what & when in a file: git blame myfile
To show remote branches and their mapping to local: git remote show origin

🧹 Cleanup

To delete all untracked files: git clean -f
To delete all untracked files and directories: git clean -df
To undo local modifications to all files: git checkout --
To unstaged a file: git reset HEAD myfile
To undo local modifications to a file and stage it: git checkout -- myfile git add myfile
To find the commit that introduced a bug: git bisect start git bisect bad git bisect good <commit>

πŸ“¦ Submodules

To add a submodule: git submodule add <url>
To update a submodule: git submodule update --remote
To remove a submodule: git submodule deinit -f -- submodule_name git rm -f submodule_name git rm -f .gitmodules

πŸ“¦ Subtrees

To add a subtree: git subtree add --prefix=folder_name <url>
To update a subtree: git subtree pull --prefix=folder_name <url>
To remove a subtree: git subtree split --prefix=folder_name git rm -rf folder_name git commit -m "Remove folder_name"

πŸ“¦Stash

Stash Changes with a message git stash save "message"
List all stashes git stash list
Apply the most recent stash and remove it from the stash list. git stash pop


πŸ—£Help

git help -a and git help -g list available subcommands and some concept guides. See git help <command> or git help <concept> to read about a specific subcommand or concept. See git help git for an overview of the system.

Git Resources

You should check some of these Git resources as they would be amazing for your journey. You will master Git with these resources:

⭐ Oh My Git

This is an open-source card game that is dedicated to teaching important git commands. There's even an integrated terminal for you to test any git command you wish. Their graphics are nothing fancy but it helps with learning visually.

This site provides free full-stack courses that are maintained by the open-source community. They have two git courses that are well worth your attention.

Branching is an important topic in Git. There's no better way to learn it than this interactive game. Run commands with their built-in terminal and let their graphics explain the rest.

Become a git guru with the help of Atlassian tutorials. Atlassian has done a fantastic job with their explanations and visuals to understand git. This is one of the top resources for learning Git.

Learn all the fundamentals of Git with this guided tour. This resource explains every important Git command while you execute them on your local machine.

I'm a big believer that visual aids help you better understand and remember information. Which is why this resource is a must-see. Their diagrams and explanations are top-tier.

Get a dose of nostalgia with this one! Learn on-the-job Git workflow while playing minesweeper with another player. So much fun you'll forget you're even learning.

Are you more of an extensive reader, we got you!

Here is GitHub's own GIT Cheatsheet for you πŸ“—

For more in-depth reading - you should check Git Notes for Professionals πŸ“•

Want to learn Git more interactively?

Here are some of the best Git courses on Udemy πŸŽ“


Contributing

Contributions are always welcome! Please read the contribution guidelines first and then feel free to open a pull request.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Contributors

✨ Thanks goes to these wonderful people ✨


Krishna Agarwal

🚧 πŸ’»

Arefat H

πŸ’»

Thomas Schubert

πŸ’»

Sushil Poudel

πŸ’»

Gurnav Chaudhary

πŸ’»

Swati Aggrawal

πŸ’»

Ahmed Shaikh

πŸ’»

If this project helped you in any way, please consider giving it a ⭐️. It will help us reach more people and help them learn Git & GitHub.

Open Source Love Contributions