{"slug": "git-cli-cheatsheet", "title": "Git CLI Cheatsheet", "summary": "This article provides a comprehensive Git command-line interface (CLI) cheatsheet, listing essential commands for tasks such as initialization, staging, committing, branching, merging, and stashing. It also includes links to external resources for beginners, pro tips, and official cheat sheets. The guide covers both basic and advanced operations, including resetting changes, rebasing, and managing untracked files.", "body_md": "- Related Setup: https://gist.github.com/hofmannsven/6814278\n- Related Pro Tips: https://ochronus.com/git-tips-from-the-trenches/\n- Interactive Beginners Tutorial: http://try.github.io/\n- Git Cheatsheet by GitHub: https://services.github.com/on-demand/downloads/github-git-cheat-sheet/\nPress minus + shift + s\nand return\nto chop/fold long lines!\nShow folder content: ls -la\nDo not put (external) dependencies in version control!\nSee where Git is located:\nwhich git\nGet the version of Git:\ngit --version\nCreate an alias (shortcut) for git status\n:\ngit config --global alias.st status\nHelp:\ngit help\nInitialize Git:\ngit init\nGet everything ready to commit:\ngit add .\nGet custom file ready to commit:\ngit add index.html\nCommit changes:\ngit commit -m \"Message\"\nCommit changes with title and description:\ngit commit -m \"Title\" -m \"Description...\"\nAdd and commit in one step:\ngit commit -am \"Message\"\nRemove files from Git:\ngit rm index.html\nUpdate all changes:\ngit add -u\nRemove file but do not track anymore:\ngit rm --cached index.html\nMove or rename files:\ngit mv index.html dir/index_new.html\nUndo modifications (restore files from latest commited version):\ngit checkout -- index.html\nRestore file from a custom commit (in current branch):\ngit checkout 6eb715d -- index.html\nGo back to commit:\ngit revert 073791e7dd71b90daa853b2c5acc2c925f02dbc6\nSoft reset (move HEAD only; neither staging nor working dir is changed):\ngit reset --soft 073791e7dd71b90daa853b2c5acc2c925f02dbc6\nUndo latest commit: git reset --soft HEAD~\nMixed reset (move HEAD and change staging to match repo; does not affect working dir):\ngit reset --mixed 073791e7dd71b90daa853b2c5acc2c925f02dbc6\nHard reset (move HEAD and change staging dir and working dir to match repo):\ngit reset --hard 073791e7dd71b90daa853b2c5acc2c925f02dbc6\nHard reset of a single file (@\nis short for HEAD\n):\ngit checkout @ -- index.html\nTest-Delete untracked files:\ngit clean -n\nDelete untracked files (not staging):\ngit clean -f\nUnstage (undo adds):\ngit reset HEAD index.html\nUpdate most recent commit (also update the commit message):\ngit commit --amend -m \"New Message\"\nShow branches:\ngit branch\nCreate branch:\ngit branch branchname\nChange to branch:\ngit checkout branchname\nCreate and change to new branch:\ngit checkout -b branchname\nRename branch:\ngit branch -m branchname new_branchname\nor:\ngit branch --move branchname new_branchname\nShow all completely merged branches with current branch:\ngit branch --merged\nDelete merged branch (only possible if not HEAD):\ngit branch -d branchname\nor:\ngit branch --delete branchname\nDelete not merged branch:\ngit branch -D branch_to_delete\nTrue merge (fast forward):\ngit merge branchname\nMerge to master (only if fast forward):\ngit merge --ff-only branchname\nMerge to master (force a new commit):\ngit merge --no-ff branchname\nStop merge (in case of conflicts):\ngit merge --abort\nStop merge (in case of conflicts):\ngit reset --merge\n// prior to v1.7.4\nUndo local merge that hasn't been pushed yet:\ngit reset --hard origin/master\nMerge only one specific commit:\ngit cherry-pick 073791e7\nRebase:\ngit checkout branchname\n» git rebase master\nor:\ngit merge master branchname\n(The rebase moves all of the commits in master\nonto the tip of branchname\n.)\nCancel rebase:\ngit rebase --abort\nSquash multiple commits into one:\ngit rebase -i HEAD~3\n(source)\nSquash-merge a feature branch (as one commit):\ngit merge --squash branchname\n(commit afterwards)\nPut in stash:\ngit stash save \"Message\"\nShow stash:\ngit stash list\nShow stash stats:\ngit stash show stash@{0}\nShow stash changes:\ngit stash show -p stash@{0}\nUse custom stash item and drop it:\ngit stash pop stash@{0}\nUse custom stash item and do not drop it:\ngit stash apply stash@{0}\nUse custom stash item and index:\ngit stash apply --index\nCreate branch from stash:\ngit stash branch new_branch\nDelete custom stash item:\ngit stash drop stash@{0}\nDelete complete stash:\ngit stash clear\nAbout: https://help.github.com/articles/ignoring-files\nUseful templates: https://github.com/github/gitignore\nAdd or edit gitignore:\nnano .gitignore\nTrack empty dir:\ntouch dir/.gitkeep\nShow commits:\ngit log\nShow oneline-summary of commits:\ngit log --oneline\nShow oneline-summary of commits with full SHA-1:\ngit log --format=oneline\nShow oneline-summary of the last three commits:\ngit log --oneline -3\nShow only custom commits:\ngit log --author=\"Sven\"\ngit log --grep=\"Message\"\ngit log --until=2013-01-01\ngit log --since=2013-01-01\nShow only custom data of commit:\ngit log --format=short\ngit log --format=full\ngit log --format=fuller\ngit log --format=email\ngit log --format=raw\nShow changes:\ngit log -p\nShow every commit since special commit for custom file only:\ngit log 6eb715d.. index.html\nShow changes of every commit since special commit for custom file only:\ngit log -p 6eb715d.. index.html\nShow stats and summary of commits:\ngit log --stat --summary\nShow history of commits as graph:\ngit log --graph\nShow history of commits as graph-summary:\ngit log --oneline --graph --all --decorate\nShows commits where the given string was added or removed:\ngit log -S 'assertInvalid' -- src/Illuminate/Testing/TestResponse.php\n(If assertInvalid\nonly appears once in the git history of src/Illuminate/Testing/TestResponse.php\nfile, it means that once it was introduced, it wasn't removed or added back, hence only one commit is shown in the results.)\nCompare modified files:\ngit diff\nCompare modified files and highlight changes only:\ngit diff --color-words index.html\nCompare modified files within the staging area:\ngit diff --staged\nCompare branches:\ngit diff master..branchname\nCompare branches like above:\ngit diff --color-words master..branchname^\nCompare commits:\ngit diff 6eb715d\ngit diff 6eb715d..HEAD\ngit diff 6eb715d..537a09f\nCompare commits of file:\ngit diff 6eb715d index.html\ngit diff 6eb715d..537a09f index.html\nCompare without caring about spaces:\ngit diff -b 6eb715d..HEAD\nor:\ngit diff --ignore-space-change 6eb715d..HEAD\nCompare without caring about all spaces:\ngit diff -w 6eb715d..HEAD\nor:\ngit diff --ignore-all-space 6eb715d..HEAD\nUseful comparings:\ngit diff --stat --summary 6eb715d..HEAD\nBlame:\ngit blame -L10,+1 index.html\nShow all released versions:\ngit tag\nShow all released versions with comments:\ngit tag -l -n1\nCreate release version:\ngit tag v1.0.0\nCreate release version with comment:\ngit tag -a v1.0.0 -m 'Message'\nCheckout a specific release version:\ngit checkout v1.0.0\nShow remote:\ngit remote\nShow remote details:\ngit remote -v\nAdd remote upstream from GitHub project:\ngit remote add upstream https://github.com/user/project.git\nAdd remote upstream from existing empty project on server:\ngit remote add upstream ssh://root@123.123.123.123/path/to/repository/.git\nFetch:\ngit fetch upstream\nFetch a custom branch:\ngit fetch upstream branchname:local_branchname\nMerge fetched commits:\ngit merge upstream/master\nRemove origin:\ngit remote rm origin\nShow remote branches:\ngit branch -r\nShow all branches (remote and local):\ngit branch -a\nCreate and checkout branch from a remote branch:\ngit checkout -b local_branchname upstream/remote_branchname\nCompare:\ngit diff origin/master..master\nPush (set default with -u\n):\ngit push -u origin master\nPush:\ngit push origin master\nForce-Push: `git push origin master --force\nPull:\ngit pull\nPull specific branch:\ngit pull origin branchname\nFetch a pull request on GitHub by its ID and create a new branch:\ngit fetch upstream pull/ID/head:new-pr-branch\nClone to localhost:\ngit clone https://github.com/user/project.git\nor:\ngit clone ssh://user@domain.com/~/dir/.git\nClone to localhost folder:\ngit clone https://github.com/user/project.git ~/dir/folder\nClone specific branch to localhost:\ngit clone -b branchname https://github.com/user/project.git\nClone with token authentication (in CI environment):\ngit clone https://oauth2:<token>@gitlab.com/username/repo.git\nDelete remote branch (push nothing):\ngit push origin :branchname\nor:\ngit push origin --delete branchname\nCreate a zip-archive: git archive --format zip --output filename.zip master\nExport/write custom log to a file: git log --author=sven --all > log.txt\nIgnore files that have already been committed to a Git repository: http://stackoverflow.com/a/1139797/1815847\nHide Git on the web via .htaccess\n: RedirectMatch 404 /\\.git\n(more info here: http://stackoverflow.com/a/17916515/1815847)\nWebsite: https://git-lfs.github.com/\nInstall: brew install git-lfs\nTrack *.psd\nfiles: git lfs track \"*.psd\"\n(init, add, commit and push as written above)", "url": "https://wpnews.pro/news/git-cli-cheatsheet", "canonical_source": "https://gist.github.com/hofmannsven/6814451", "published_at": "2013-10-03 18:17:58+00:00", "updated_at": "2026-05-22 15:42:32.274369+00:00", "lang": "en", "topics": ["developer-tools"], "entities": ["Git", "GitHub"], "alternates": {"html": "https://wpnews.pro/news/git-cli-cheatsheet", "markdown": "https://wpnews.pro/news/git-cli-cheatsheet.md", "text": "https://wpnews.pro/news/git-cli-cheatsheet.txt", "jsonld": "https://wpnews.pro/news/git-cli-cheatsheet.jsonld"}}