{"id":18034710,"url":"https://github.com/yuanqing/git-cheatsheet","last_synced_at":"2025-10-04T02:30:27.720Z","repository":{"id":23923889,"uuid":"27304540","full_name":"yuanqing/git-cheatsheet","owner":"yuanqing","description":":deciduous_tree: A handy list of commonly used Git commands","archived":false,"fork":false,"pushed_at":"2021-03-30T10:55:29.000Z","size":6,"stargazers_count":6,"open_issues_count":0,"forks_count":5,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-01-17T23:40:22.517Z","etag":null,"topics":["cheatsheet","git"],"latest_commit_sha":null,"homepage":"","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/yuanqing.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}},"created_at":"2014-11-29T14:30:57.000Z","updated_at":"2023-07-04T12:04:02.000Z","dependencies_parsed_at":"2022-07-20T23:33:05.970Z","dependency_job_id":null,"html_url":"https://github.com/yuanqing/git-cheatsheet","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yuanqing%2Fgit-cheatsheet","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yuanqing%2Fgit-cheatsheet/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yuanqing%2Fgit-cheatsheet/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yuanqing%2Fgit-cheatsheet/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/yuanqing","download_url":"https://codeload.github.com/yuanqing/git-cheatsheet/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":235209007,"owners_count":18953004,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","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":["cheatsheet","git"],"created_at":"2024-10-30T11:13:29.061Z","updated_at":"2025-10-04T02:30:22.465Z","avatar_url":"https://github.com/yuanqing.png","language":null,"readme":"# Git Cheatsheet\n\n\u003e A handy list of commonly used Git commands\n\n## Commands\n\n### Create new or download existing repository\n\n```sh\n# Initialize a new repository\n$ git init\n$ git remote add origin https://github.com/foo/bar.git\n\n# Download an existing remote repository\n$ git clone https://github.com/foo/bar.git\n```\n\n### Sync local repository with remote\n\n```sh\n# Sync with the remote branch\n$ git fetch\n\n# Sync with the remote branch, and merge it into the local branch\n$ git pull\n```\n\n### Branching\n\n```sh\n# List all branches\n$ git branch\n\n# Create a new branch\n$ git branch hotfix\n\n# Switch to a branch\n$ git checkout hotfix\n\n# Create a new branch, and switch to it\n$ git checkout -b hotfix\n\n# Delete a local branch\n$ git branch -D hotfix\n\n# Delete a remote branch\n$ git push origin :hotfix\n\n# Merge one branch into another\n$ git checkout main\n$ git merge hotfix\n\n# Merge one branch into another as a single commit\n$ git checkout main\n$ git merge --squash hotfix\n```\n\n### Tagging\n\n```sh\n# List all tags\n$ git tag\n\n# Tag a commit\n$ git tag -a v1.0.0 ea506f6c2c -m '1.0.0'\n\n# Delete a tag\n$ git tag -d v1.0.0\n```\n\n### Stage changes\n\n```sh\n# Stage a modified or untracked file\n$ git add foo\n\n# Stage a deleted file\n$ git rm foo\n\n# Stage all modified or untracked files, ignoring deleted files\n$ git add .\n\n# Stage all modified or deleted files, ignoring untracked files\n$ git add -u\n```\n\n### Unstage changes\n\n```sh\n# Unstage a single file\n$ git reset foo\n\n# Unstage all files\n$ git reset -- .\n\n# Restore a single file from the last commit\n$ git checkout foo\n\n# Restore all files from the last commit\n$ git checkout -- .\n\n# Delete all untracked files\n$ git clean -df\n```\n\n### Review changes\n\n```sh\n# See unstaged changes made to a single file\n$ git diff foo\n\n# See unstaged changes made to all files\n$ git diff\n\n# See staged changes made to all files\n$ git diff --cached\n\n# Compare the last and second-last commits\n$ git diff HEAD^ HEAD\n```\n\n### Commit changes\n\n```sh\n# List commit history\n$ git log\n\n# Make a commit\n$ git commit -m 'foo'\n\n# Amend the last commit message\n$ git commit --amend -m 'bar'\n\n# Amend the last commit message after having done a `git push`\n$ git commit --amend -m 'bar'\n$ git push origin +main\n\n# Delete the last commit\n$ git reset --soft HEAD~1\n\n# Delete the last commit, and unstage all files\n$ git reset --soft HEAD~1\n$ git reset -- .\n\n# Delete the last commit after having done a `git push`\n$ git reset --soft HEAD~1\n$ git push origin +main\n\n# Delete commits from history (eg. delete commits 2 and 4)\n#\n# commit 1: 1ebc709 (keep)\n# commit 2: 2de6867\n# commit 3: 3eba02b (keep)\n# commit 4: 4b632b2\n# commit 5: 56098c5 (keep)\n#\n$ git checkout 56098c5     # commit 5\n$ git checkout -b repair\n$ git cherry-pick 3eba02b  # commit 3\n$ git cherry-pick 1ebc709  # commit 1\n$ git checkout main\n$ git reset --hard 56098c5 # commit 5\n$ git merge repair\n$ git branch -D repair\n$ git push origin +main\n\n# Merge multiple commits (eg. the last 3 commits) into one\n#\n# 1. Mark all commits other than the first with `squash`, then save and quit.\n# 2. Enter a new commit message, then save and quit.\n#\n$ git rebase -i HEAD~3\n```\n\n### Push changes to remote\n\n```sh\n# Push changes\n$ git push origin main\n\n# Push changes, overriding the remote\n$ git push origin +main\n\n# Push tags\n$ git push --tags origin main\n```\n\n## References\n\n- [Oh Shit, Git!?!](https://ohshitgit.com/)\n- [Delete commits from history](https://stackoverflow.com/a/46049102)\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyuanqing%2Fgit-cheatsheet","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fyuanqing%2Fgit-cheatsheet","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyuanqing%2Fgit-cheatsheet/lists"}