{"id":49561853,"url":"https://github.com/diegaccio/git-cheatsheet","last_synced_at":"2026-05-03T09:13:47.033Z","repository":{"id":272308454,"uuid":"915991250","full_name":"diegaccio/git-cheatsheet","owner":"diegaccio","description":"Git command line cheatsheet","archived":false,"fork":false,"pushed_at":"2025-01-29T09:07:32.000Z","size":33,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-01-29T10:22:31.641Z","etag":null,"topics":["command","command-line","git"],"latest_commit_sha":null,"homepage":"https://diegaccio.github.io/git-cheatsheet/","language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/diegaccio.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2025-01-13T08:47:49.000Z","updated_at":"2025-01-29T09:07:36.000Z","dependencies_parsed_at":null,"dependency_job_id":"6b943892-8f83-4f27-a552-76fd1fe1a18d","html_url":"https://github.com/diegaccio/git-cheatsheet","commit_stats":null,"previous_names":["diegaccio/git-cheatsheet"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/diegaccio/git-cheatsheet","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/diegaccio%2Fgit-cheatsheet","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/diegaccio%2Fgit-cheatsheet/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/diegaccio%2Fgit-cheatsheet/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/diegaccio%2Fgit-cheatsheet/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/diegaccio","download_url":"https://codeload.github.com/diegaccio/git-cheatsheet/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/diegaccio%2Fgit-cheatsheet/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":32563763,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-03T06:36:36.687Z","status":"ssl_error","status_checked_at":"2026-05-03T06:36:09.306Z","response_time":103,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["command","command-line","git"],"created_at":"2026-05-03T09:13:42.891Z","updated_at":"2026-05-03T09:13:47.029Z","avatar_url":"https://github.com/diegaccio.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003cimg src=\"Git-Logo-2Color.png \" height=\"50\" /\u003e\n\n# Git Command Line Cheat Sheet\n\n## Table of Contents\n\n- [Configuration](#configuration)\n- [Creating Snapshots](#creating-snapshots)\n- [Browsing History](#browsing-history)\n- [Branching \u0026 Merging](#branching--merging)\n- [Working with remote repositories](#working-with-remote-repositories)\n- [Rewriting History](#rewriting-history)\n- [Powerful Aliases](#powerful-aliases)\n\n## Configuration\n\n### Global vs Local\n\n```\ngit config --global                             #settings for the current user\ngit config --local                              #settings for the current folder\n```\n\n### Showing the configuration\n\n```\ngit config --global -l\n```\n\n### User\n\n```\ngit config --global user.name \"John Smith\"              #sets the user name\ngit config --global user.email \"johnsmith@gmail.com\"    #sets the user email\n```\n\n### Editor\n\n```\ngit config --global core.editor \"code --wait\"   # Sets the default editor to vscode\ngit config --global -e                          # Opens the global config file in the default editor\n```\n\n### Merge and Diff tool\n\nTo setup p4merge as diff and merge tool on macOS, install it using brew:\n\n```\nbrew install --cask p4v\n```\n\nThen add this sections to your git confi file:\n\n```\n[diff]\n\ttool = p4merge\n[difftool \"p4merge\"]\n\tpath = /Applications/p4merge.app/Contents/Resources/launchp4merge\n[difftool]\n\tprompt = false\n[merge]\n\ttool = p4merge\n[mergetool \"p4merge\"]\n\tpath = /Applications/p4merge.app/Contents/Resources/launchp4merge\n[mergetool]\n\tprompt = false\n\tkeepBackup = false\n```\n\n## Creating Snapshots\n\n### Initializing a repository\n\n```\ngit init\n```\n\n### Staging files\n\n```\ngit add file1.js                # Stages a single file\ngit add file1.js file2.js       # Stages multiple files\ngit add *.js                    # Stages with a pattern\ngit add .                       # Stages the current directory and all its content\n```\n\n### Viewing the status\n\n```\ngit status                      # Full status\ngit status -s                   # Short status\n```\n\n### Committing the staged files\n\n```\ngit commit -m \"Message\"         # Commits with a one-line message\ngit commit                      # Opens the default editor to type a long message\n```\n\n### Skipping the staging area\n\n```\ngit commit -am \"Message\"\n```\n\n### Removing files\n\n```\ngit rm file1.js                 # Removes from working directory and staging area\ngit rm --cached file1.js        # Removes from staging area only\n```\n\n### Renaming or moving files\n\n```\ngit mv file1.js file1.txt\n```\n\n### Viewing the staged/unstaged changes\n\n```\ngit diff                        # Shows unstaged changes\ngit diff file.txt               # Shows unstaged changes for a single file\ngit diff --staged               # Shows staged changes\ngit diff --cached               # Same as the above\n```\n\n### Viewing a commit\n\n```\ngit show 921a2ff                # Shows the given commit\ngit show HEAD                   # Shows the last commit\ngit show HEAD~2                 # Two steps before the last commit\ngit show HEAD:file.js           # Shows the version of file.js stored in the last commit\n```\n\n### Unstaging files (undoing git add)\n\n```\ngit restore --staged file.js    # Copies the last version of file.js from repo to index\n```\n\n### Discarding local changes\n\n```\ngit restore file.js             # Copies file.js from index to working directory\ngit restore file1.js file2.js   # Restores multiple files in working directory\ngit restore .                   # Discards all local changes (except untracked files)\ngit clean -fd                   # Removes all untracked files\n```\n\n### Restoring an earlier version of a file\n\n```\ngit restore --source=HEAD~2 file.js\n```\n\n## Browsing History\n\n### Viewing the history\n\n```\ngit log                         # Full history\ngit log --oneline               # Summary\ngit log --reverse               # Lists the commits from the oldest to the newest\ngit log --stat                  # Shows the list of modified files\ngit log --patch                 # Shows the actual changes (patches)\n```\n\n### Filtering the history\n\n```\ngit log -3                      # Shows the last 3 entries\ngit log --author=\"Diego\"\ngit log --before=\"2024-08-17\"\ngit log --after=\"one week ago\"\ngit log --grep=\"GUI\"            # Commits with \"GUI\" in their message\ngit log -S\"GUI\"                 # Commits with \"GUI\" in their patches\ngit log hash1..hash2            # Range of commits\ngit log file.txt                # Commits that touched file.txt\n```\n\n### Formatting the log output\n\n```\ngit log --pretty=format:\"%an committed %H\"\n```\n\n### Creating an alias\n\n```\ngit config --global alias.lg \"log --oneline\"\n```\n\n### Viewing a commit\n\n```\ngit show HEAD~2             # Shows the second commit after HEAD\ngit show HEAD~2:file1.txt   # Shows the version of file stored in this commit\n```\n\n### Comparing commits\n\n```\ngit diff HEAD~2 HEAD            # Shows the changes between two commits\ngit diff HEAD~2 HEAD file.txt   # Changes to file.txt only\n```\n\n### Checking out a commit\n\n```\ngit checkout dad47ed            # Checks out the given commit\ngit checkout master             # Checks out the master branch\n```\n\n### Finding a bad commit\n\n```\ngit bisect start                # Starts the bisect session\ngit bisect bad                  # Marks the current commit as a bad commit\ngit bisect good ca49180         # Marks the given commit as a good commit\ngit bisect reset                # Terminates the bisect session\n```\n\n### Finding contributors\n\n```\ngit shortlog\ngit shortlog -s                 # shows the number of contributions for each user\n```\n\n### Viewing the history of a file\n\n```\ngit log file.txt                # Shows the commits that touched file.txt\ngit log --stat file.txt         # Shows statistics (the number of changes) for file.txt\ngit log --patch file.txt        # Shows the patches (changes) applied to file.txt\n```\n\n### Finding the author of lines\n\n```\ngit blame file.txt              # Shows the author of each line in file.txt\n```\n\n### Tagging\n\n```\ngit tag v1.0                    # Tags the last commit as v1.0\ngit tag v1.0 5e7a828            # Tags an earlier commit\ngit tag                         # Lists all the tags\ngit tag -d v1.0                 # Deletes the given tag\n```\n\n## Branching \u0026 Merging\n\n### Managing branches\n\n```\ngit branch bugfix               # Creates a new branch called bugfix\ngit checkout bugfix             # Switches to the bugfix branch\ngit switch bugfix               # Same as the above (newer command)\ngit switch -C bugfix            # Creates and switches\ngit branch -d bugfix            # Deletes the bugfix branch\n```\n\n### Comparing branches\n\n```\ngit log master..bugfix          # Lists the commits in the bugfix branch not in master\ngit diff master..bugfix         # Shows the summary of changes\n```\n\n### Stashing\n\n```\ngit stash push -m \"Shash\"       # Creates a new stash\ngit stash list                  # Lists all the stashes\ngit stash show stash@{1}        # Shows the given stash\ngit stash show 1                # shortcut for stash@{1}\ngit stash apply 1               # Applies the given stash to the working dir\ngit stash drop 1                # Deletes the given stash\ngit stash clear                 # Deletes all the stashes\n```\n\n### Merging\n\n```\ngit merge bugfix                # Merges the bugfix branch into the current branch\ngit merge --no-ff bugfix        # Creates a merge commit even if FF is possible\ngit merge --squash bugfix       # Performs a squash merge\ngit merge --abort               # Aborts the merge\n```\n\n### Viewing the merged branches\n\n```\ngit branch --merged             # Shows the merged branches\ngit branch --no-merged          # Shows the unmerged branches\n```\n\n### Rebasing\n\n```\ngit rebase master               # Changes the base of the current branch - it rewrites history so be careful with shared work\n```\n\n### Cherry picking\n\n```\ngit cherry-pick dad47ed         # Applies the given commit on the current branch\n```\n\n## Working with remote repositories\n\n### Cloning a repository\n\n```\ngit clone url                   #clones the repository in the current directory\n```\n\n### Syncing with remotes\n\n```\ngit fetch origin master         # Fetches master from origin\ngit fetch origin                # Fetches all objects from origin\ngit fetch                       # Shortcut for \"git fetch origin\"\ngit pull                        # Fetch + merge\ngit push origin master          # Pushes master to origin\ngit push                        # Shortcut for \"git push origin master\"\n```\n\n### Sharing tags\n\n```\ngit push origin v1.0            # Pushes tag v1.0 to origin\ngit push origin -delete v1.0    # deletes the tag\n```\n\n### Sharing branches\n\n```\ngit branch -r                   # Shows remote tracking branches\ngit branch -vv                  # Shows local \u0026 remote tracking branches\ngit push -u origin bugfix       # Pushes bugfix to origin\ngit push -d origin bugfix       # Removes bugfix from origin\n```\n\n### Managing remotes\n\n```\ngit remote -v                   # Shows remote repos\ngit remote add upstream url     # Adds a new remote called upstream\ngit remote rm upstream          # Remotes upstream\ngit remote show origin          # Shows the mapping of the local branches to the remote branches\ngit branch -u origin/serverfix  # Branch serverfix set up to track remote branch serverfix from origin.\n```\n\n### Configuring a remote repository for a fork (i.e. GitHub contribution)\n\nYou must configure a remote that points to the upstream repository in Git to sync changes you make in a fork with the original repository (**Pull Request**). This also allows you to sync changes made in the original repository with the fork.\n\nList the current configured remote repository for your fork:\n\n```\n$ git remote -v\n\u003e origin  https://github.com/YOUR-USERNAME/YOUR-FORK.git (fetch)\n\u003e origin  https://github.com/YOUR-USERNAME/YOUR-FORK.git (push)\n```\n\nSpecify a new remote upstream repository that will be synced with the fork:\n\n```\ngit remote add upstream https://github.com/ORIGINAL-OWNER/ORIGINAL-REPOSITORY.git\n```\n\nVerify the new upstream repository you've specified for your fork:\n\n```\n$ git remote -v\n\u003e origin    https://github.com/YOUR-USERNAME/YOUR-FORK.git (fetch)\n\u003e origin    https://github.com/YOUR-USERNAME/YOUR-FORK.git (push)\n\u003e upstream  https://github.com/ORIGINAL-OWNER/ORIGINAL-REPOSITORY.git (fetch)\n\u003e upstream  https://github.com/ORIGINAL-OWNER/ORIGINAL-REPOSITORY.git (push)\n```\n\n## Rewriting History\n\nPlease avoid **Rewriting Shared History**: only rewrite history in private branches or before pushing changes to the remote.\nUse git revert Instead: To fix mistakes in a public branch, create new commits that undo or adjust previous changes without altering the existing history.\nThese practices help maintain a clear and consistent history, minimizing disruptions for collaborators.\n\n### Undoing commits\n\n```\ngit reset --soft HEAD^              # Removes the last commit, keeps changed staged\ngit reset --mixed HEAD^             # Unstages the changes as well\ngit reset --hard HEAD^              # Discards local changes\n```\n\n### Reverting commits\n\n```\ngit revert 72856ea                  # Reverts the given commit\ngit revert HEAD~3..                 # Reverts the last three commits\ngit revert --no-commit HEAD~3..\n```\n\n### Recovering lost commits\n\n```\ngit reflog                          # Shows the history of HEAD\ngit reflog show bugfix              # Shows the history of bugfix pointer\n```\n\n### Amending the last commit\n\n```\ngit commit --amend\n```\n\n### Interactive rebasing\n\n```\ngit rebase -i HEAD~5                #starts the interactive rebase at the fifth commit befor HEAD\n```\n\n## Powerful Aliases\n\n### Adding alias from command line\n\n```\ngit config --global alias.st status         #add a new line to the [alias] section of your global config\n\n```\n\n### Editing the global .gitconfi file\n\nOpen the global config file in the default editor:\n\n```\ngit config --global -e\n```\n\nAnd add this section:\n\n```\n[alias]\n   st = status\n```\n\n### The lazy typer\n\n```\n[alias]\n    s  = status\n    st = status\n    c = commit\n    sw = switch\n    br = branch\n```\n\n### Common Typos\n\n```\n[alias]\n    comit = commit\n    swicht = switch\n    statut = status\n```\n\n### Speedy commands\n\n```\n[alias]\n    cam = git commit -am                    #stage all and commit with message\n    dlc = diff --cached HEAD^               # Diff of last commit\n    aliases = config --get-regexp alias     # list all defined aliases\n    first = rev-list --max-parents=0 HEAD   # Find very first commit\n    incoming = log HEAD..@{upstream}        # what would be merged\n    gitoutgoing = log @{upstream}..HEAD        # what would be pushed\n```\n\n### Pretty formats\n\nAdd this section to your .gitconfig file:\n\n```\n[pretty]\n    slog = format:%C(yellow)%h %Cred%as %Cblue%an%Cgreen%d %Creset%s\n    bw = format:%h | %as | %\u003e(20,trunc)%d%x09%s\n```\n\nThen you can use this alias:\n\n```\n[alias]\n    l1 = log -1 --pretty=slog\n    l5 = log -5 --pretty=slog\n    slog = log --pretty=slog\n    slogbw = log --pretty=bw\n    glog = log --graph --pretty=slog\n    outgoing = log --pretty=slog @{u}..\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdiegaccio%2Fgit-cheatsheet","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdiegaccio%2Fgit-cheatsheet","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdiegaccio%2Fgit-cheatsheet/lists"}