{"id":21340296,"url":"https://github.com/mnestorov/git-commands","last_synced_at":"2026-05-21T10:04:34.024Z","repository":{"id":111493619,"uuid":"375863389","full_name":"mnestorov/git-commands","owner":"mnestorov","description":"These advanced Git commands and techniques will help you take your Git skills to the next level, allowing you to more effectively manage and collaborate on complex code projects.","archived":false,"fork":false,"pushed_at":"2023-11-30T09:52:38.000Z","size":76,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-16T02:47:58.692Z","etag":null,"topics":["git","git-commands","git-flow","github","snippets","version-control"],"latest_commit_sha":null,"homepage":"","language":null,"has_issues":false,"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/mnestorov.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":"2021-06-11T00:33:38.000Z","updated_at":"2023-11-30T14:01:41.000Z","dependencies_parsed_at":null,"dependency_job_id":"45885af1-8cf3-438f-82f1-f0ec43dad28f","html_url":"https://github.com/mnestorov/git-commands","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/mnestorov/git-commands","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mnestorov%2Fgit-commands","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mnestorov%2Fgit-commands/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mnestorov%2Fgit-commands/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mnestorov%2Fgit-commands/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mnestorov","download_url":"https://codeload.github.com/mnestorov/git-commands/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mnestorov%2Fgit-commands/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":33297159,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-21T02:57:32.698Z","status":"ssl_error","status_checked_at":"2026-05-21T02:57:31.990Z","response_time":62,"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":["git","git-commands","git-flow","github","snippets","version-control"],"created_at":"2024-11-22T00:49:59.930Z","updated_at":"2026-05-21T10:04:34.009Z","avatar_url":"https://github.com/mnestorov.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# Git Commands\n\n![Licence](https://img.shields.io/badge/Unlicense-red)\n\n## Overview\n\nThese advanced Git commands and techniques will help you take your Git skills to the next level, allowing you to more effectively manage and collaborate on complex code projects.\n\n## Setting up Git\n\n- [Configure your name and email](#configure-your-name-and-email)\n\n## Basic Git Commands\n\n- [Initialize a new Git repository](#initialize-a-new-git-repository)\n- [Clone a remote repository](#clone-a-remote-repository)\n- [Check the status of your repository](#check-the-status-of-your-repository)\n- [Stage files for commit](#stage-files-for-commit)\n- [Stage all files in the repository for commit](#stage-all-files-in-the-repository-for-commit)\n- [Commit staged files with a message](#commit-staged-files-with-a-message)\n- [Show the commit history](#show-the-commit-history)\n- [Show the commit history in a graph format](#show-the-commit-history-in-a-graph-format)\n- [Show the differences between the working directory and the last commit](#show-the-differences-between-the-working-directory-and-the-last-commit)\n\n## Branching and Merging\n\n- [List all branches](#list-all-branches)\n- [Create a new branch](#create-a-new-branch)\n- [Switch to a different branch](#switch-to-a-different-branch)\n- [Create and switch to a new branch in one command](#create-and-switch-to-a-new-branch-in-one-command)\n- [Merge a branch into the current branch](#merge-a-branch-into-the-current-branch)\n- [Delete a branch](#delete-a-branch)\n\n## Remote Repositories\n\n- [Show a list of remote repositories](#show-a-list-of-remote-repositories)\n- [Add a remote repository](#add-a-remote-repository)\n- [Fetch changes from a remote repository](#fetch-changes-from-a-remote-repository)\n- [Pull changes from a remote repository and merge them into the current branch](#pull-changes-from-a-remote-repository-and-merge-them-into-the-current-branch)\n- [Push changes to a remote repository](#push-changes-to-a-remote-repository)\n- [Remove a remote repository](#remove-a-remote-repository)\n\n## Stashing\n\n- [Stash changes in the working directory](#stash-changes-in-the-working-directory)\n- [Apply the latest stashed changes](#apply-the-latest-stashed-changes)\n- [Apply a specific stash](#apply-a-specific-stash)\n- [Drop a specific stash](#drop-a-specific-stash)\n- [List all stashes](#list-all-stashes)\n\n## Tagging\n\n- [List all tags](#list-all-tags)\n- [Create a new tag](#create-a-new-tag)\n- [Push tags to a remote repository](#push-tags-to-a-remote-repository)\n\n## Undoing and Reverting\n\n- [Unstage a file](#unstage-a-file)\n- [Revert changes in a file to the last commit](#revert-changes-in-a-file-to-the-last-commit)\n- [Revert a commit (creates a new commit that undoes the changes)](#revert-a-commit-creates-a-new-commit-that-undoes-the-changes)\n- [Reset the working directory to a specific commit](#reset-the-working-directory-to-a-specific-commit)\n\n## Other Commands for BitBucket / GitHub / GitLab\n\n- [Upload all files in a local directory to a new Git repository](#upload-all-files-in-a-local-directory-to-a-new-git-repository)\n- [Download all files from Git repository to a local directory](#download-all-files-from-git-repository-to-a-local-directory)\n- [Remove one file from Git cache](#remove-one-file-from-git-cache)\n- [Override entire local directory](#override-entire-local-directory)\n- [Ignore a directory](#ignore-a-directory)\n- [Add gitignore to an existing repository](#add-gitignore-to-an-existing-repository)\n- [Force a push or pull](#force-a-push-or-pull)\n- [Merging changes from remote pull request with conflicts](#merging-changes-from-remote-pull-request-with-conflicts)\n- [Rename branch](#rename-branch)\n- [Remove branch](#remove-branch)\n- [Replace master with contents of another branch](#replace-master-with-contents-of-another-branch)\n- [Remove all local branches except master](#remove-all-local-branches-except-master)\n- [Allow empty commit](#allow-empty-commit)\n- [Merge new-feature branch into master](#merge-new-feature-branch-into-master)\n- [Switch to branch that exists on origin](#switch-to-branch-that-exists-on-origin)\n- [Fetch branch from origin](#fetch-branch-from-origin)\n- [Accept all incoming changes](#accept-all-incoming-changes)\n- [Delete local and remote tag](#delete-local-and-remote-tag)\n- [Rebase from develop](#rebase-from-develop)\n- [Stashing](#stashing)\n- [Accidentally committed to develop and want to move that commit to a branch](#acidentally-committed-to-develop-and-want-to-move-that-commit-to-a-branch)\n- [Subtree within repo](#subtree-within-repo)\n- [Exiting VIM](#exiting-vim)\n\n## GitHub Specific Commands\n\n- [GitHub pages to non-docs folder](#github-pages-to-non-docs-folder)\n\n---\n\n## Setting up Git\n\n### Configure your name and email\n\n```\ngit config --global user.name \"Your Name\"\ngit config --global user.email \"you@example.com\"\n```\n\n## Basic Git Commands\n\n### Initialize a new Git repository\n\n```\ngit init\n```\n\n### Clone a remote repository\n\n```\ngit clone https://github.com/user/repo.git\n```\n\n### Check the status of your repository\n\n```\ngit status\n```\n\n### Stage files for commit\n\n```\ngit add file_name\n```\n\n### Stage all files in the repository for commit\n\n```\ngit add .\n```\n\n### Commit staged files with a message\n\n```\ngit commit -m \"Commit message\"\n```\n\n### Show the commit history\n\n```\ngit log\n```\n\n### Show the commit history in a graph format\n\n```\ngit log --graph --oneline --decorate\n```\n\n### Show the differences between the working directory and the last commit\n\n```\ngit diff\n```\n\n## Branching and Merging\n\n### List all branches\n\n```\ngit branch\n```\n\n### Create a new branch\n\n```\ngit branch new_branch\n```\n\n### Switch to a different branch\n\n```\ngit checkout branch_name\n```\n\n### Create and switch to a new branch in one command\n\n```\ngit checkout -b new_branch\n```\n\n### Merge a branch into the current branch\n\n```\ngit merge branch_name\n```\n\n### Delete a branch\n\n```\ngit branch -d branch_name\n```\n\n## Remote Repositories\n\n### Show a list of remote repositories\n\n```\ngit remote -v\n```\n\n### Add a remote repository\n\n```\ngit remote add remote_name https://github.com/user/repo.git\n```\n\n### Fetch changes from a remote repository\n\n```\ngit fetch remote_name\n```\n\n### Pull changes from a remote repository and merge them into the current branch\n\n```\ngit pull remote_name branch_name\n```\n\n### Push changes to a remote repository\n\n```\ngit push remote_name branch_name\n```\n\n### Remove a remote repository\n\n```\ngit remote rm remote_name\n```\n\n## Stashing\n\n### Stash changes in the working directory\n\n```\ngit stash\n```\n\n### Apply the latest stashed changes\n\n```\ngit stash apply\n```\n\n### Apply a specific stash\n\n```\ngit stash apply stash@{stash_number}\n```\n\n### Drop a specific stash\n\n```\ngit stash drop stash@{stash_number}\n```\n\n### List all stashes\n\n```\ngit stash list\n```\n\n## Tagging\n\n### List all tags\n\n```\ngit tag\n```\n\n### Create a new tag\n\n```\ngit tag -a tag_name -m \"Tag message\"\n```\n\n### Push tags to a remote repository\n\n```\ngit push remote_name --tags\n```\n\n## Undoing and Reverting\n\n### Unstage a file\n\n```\ngit reset HEAD file_name\n```\n\n### Revert changes in a file to the last commit\n\n```\ngit checkout -- file_name\n```\n\n### Revert a commit (creates a new commit that undoes the changes)\n\n```\ngit revert commit_hash\n```\n\n### Reset the working directory to a specific commit\n\n**WARNING:** This will discard all uncommitted changes\n\n```\ngit reset --hard commit_hash\n```\n## Other Commands for BitBucket / GitHub / GitLab\n\n### Upload all files in a local directory to a new Git repository\n\nIf you have a project on your computer and you just created an empty Git repository in GitHub, use these commands to upload everything to Git.\n\n```bash\ncd your-directory\ngit init\ngit remote add origin git@github.com:your-username/your-repo.git\ngit add .\ngit commit -am \"Message\"\ngit push -u origin master\n```\n\n### Download all files from Git repository to a local directory\n\nThe opposite of the above option - for example, if your repository exists in GitHub, and you're working on it in a different local computer. Run this command outside of where you want the new directory to appear (not within the directory you want it to appear).\n\n```bash\ngit clone git@github.com:your-username/your-repo.git     # using SSH\ngit clone https://github.com/your-username/your-repo.git # using HTTPS\n```\n\n### Remove one file from Git cache\n\nRemove one cached file.\n\n```bash\ngit rm -r —-cached file.txt\n```\n\n### Override entire local directory\n\nIf you have some merge conflicts, or accidentally started to make a change to your local directory before pulling the changes from the master, here's how you can revert your local directory to what's on GitHub.\n\n```bash\ngit fetch --all\ngit reset --hard origin/master\n```\n\n### Ignore a directory\n\nIf you've been tracking a directory and later decide to ignore the whole directory, simply adding it to `.gitignore` isn't enough. First you must add the directory to .gitignore, then run this command:\n\n```bash\ngit rm -r --cached your-directory\n```\n\nThen push the changes.\n\n### Add gitignore to an existing repository\n\nSimilar to above, but if you've added a `.gitignore` with a lot of changes.\n\n```bash\ngit rm -r --cached .\ngit add .\ngit commit -m \"Message\"\n```\n\n### Force a push or pull\n\nWhen you really want your local repository to override the remote.\n\n```bash\ngit push -f origin master\ngit pull -f origin master\n```\n\n### Merging changes from remote pull request with conflicts\n\nMake a new branch with their changes.\n\n```bash\ngit checkout -b their-branch master\ngit pull their.git master\n```\n\nPlay with the files and commit them.    \n\n```bash\ngit add files\ngit commit -m “Message\"\ngit push origin master\n```\n\nMerge back into your branch.  \n\n```bash\ngit checkout master\ngit merge --no-ff \u003ctheir-branch) (:wq!)\ngit push origin master\n```\n\n### Rename branch\n\nIf you have a local clone, you can update it by running the following commands.\n\n```bash\ngit branch -m master main\ngit fetch origin\ngit branch -u origin/main main\ngit remote set-head origin -a\n```\n\n### Remove branch\n\nPut a `:` in front to remove instead of update remotely.\n\n```bash\ngit push origin :branch-name\n```\n\nUse `--delete` or `-D` for local.\n\n```\ngit branch --delete branch-name\n````\n\n### Replace master with contents of another branch\n\n```bash\ngit checkout branch-name\ngit merge -s ours master\ngit checkout master\ngit merge branch-name\n```\n\n### Remove all local branches except master\n\n```bash\ngit branch | grep -v \"master\" | xargs git branch -D\n```\n\nMore than one branch may be added to the grep. To remove all local branches except \"master\" and \"develop\":\n\n```bash\ngit branch | grep -v \"master\\|develop\" | xargs git branch -D\n```\n\n ### Allow empty commit\n \n Fix the problem of git hooks claiming everything is \"Up-to-date\".\n \n ```bash\n git push production master\n git commit --allow-empty -m 'push to execute post-receive'\n git push production master\n ```\n \n ### Merge new-feature branch into master\n \n Merge branches.\n\n```bash\ngit checkout master\ngit pull origin master\ngit merge new-feature\ngit push origin master\n```\n\n### Switch to branch that exists on origin\n\n```bash\ngit fetch --prune --all\ngit checkout other-branch\n```\n\n### Fetch branch from origin\n\n```bash\ngit fetch origin\ngit checkout --track origin/\u003cremote_branch_name\u003e\n```\n\n### Accept all incoming changes\n\n```bash\ngit pull -Xtheirs\n```\n\n### Delete local and remote tag\n\n```bash\ngit push --delete origin tagName\ngit tag -d tagName\n```\n\n### Rebase from develop\n\n```bash\ngit fetch --prune --all\ngit rebase origin/develop\ngit pull\ngit push\n```\n\n### Stashing\n\nPut your changes away and switch to another branch\n\n```bash\ngit stash\ngit checkout -b new-branch\ngit stash pop\n```\n\n### Accidentally committed to develop and want to move that commit to a branch\n\n```bash\ngit branch new-branch\ngit reset HEAD~1\ngit checkout \u003cfiles\u003e\n```\n\n### Subtree within repo\n\n```bash\ngit subtree add --prefix \u003clocal-dir\u003e https://github.com/taniarascia/\u003crepo\u003e master --squash\ngit subtree pull --prefix \u003clocal-dir\u003e https://github.com/taniarascia/\u003crepo\u003e master --squash\ngit subtree push --prefix \u003clocal-dir\u003e https://github.com/taniarascia/\u003crepo\u003e master --squash\n```\n\n### Exiting VIM\n\nFor those new to command line, ending up at the commit message screen (often when you forget to the add `-m` flag to a commit) is confusing because pressing escape (or `CTRL` + `C`) does not exit the screen, as the default editor for Git is VIM. Instead, press escape (if you've started attempting to type something) and type the following command:\n\n```bash\n:q\n```\n\nAnd press enter, and you'll return to where you were.\n\n## GitHub Specific Commands\n\n### Adding an existing project to GitHub using the command line\n\n1. Initialize the local directory as a Git repository.\n\n```\n$ git init -b main\n```\n\n2. Add the files in your new local repository. This stages them for the first commit.\n\n```\n$ git add .\n```\n\nThe code above adds the files in the local repository and stages them for commit. To unstage a file, use **git reset HEAD YOUR-FILE**.\n\n3. Commit the files that you've staged in your local repository.\n\n```\n$ git commit -m \"First commit\"\n```\n\nThe code above commits the tracked changes and prepares them to be pushed to a remote repository. To remove this commit and modify the file, use **git reset --soft HEAD~1** and commit and add the file again.\n\n4. At the top of your GitHub repository's Quick Setup page, click to copy the remote repository URL. \n\n\u003cimg src=\"https://github.com/mnestorov/git-commands/blob/main/RemoteRepositoryUrl.png\"\u003e\n\n5. In the Command prompt, add the URL for the remote repository where your local repository will be pushed.\n\n```\n$ git remote add origin  \u003cREMOTE_URL\u003e \n# Sets the new remote\n$ git remote -v\n# Verifies the new remote URL\n```\n\n6. Push the changes in your local repository to GitHub.\n\n```\n$ git push origin main\n```\n\nThe code above pushes the changes in your local repository up to the remote repository you specified as the origin.\n\n### Change GitHub branch on local machine when the branch name is changed\n\n```\ngit branch -m main objects\ngit fetch origin\ngit branch -u origin/objects objects\ngit remote set-head origin -a\n```\n\n### GitHub pages to non-docs folder\n\n\"dist\" or whatever you want.\n\n```bash\ngit subtree push --prefix dist origin gh-pages\n```\n\n### Git Workflow\n\n```\ngit checkout main\ngit pull origin main\ngit checkout -b \u003cnew branch name here\u003e\ngit status\ngit add themes/.../header.php # add only this file for commit\ngit add . # add all changed files\ngit commit -m \"Commit message\"\ngit push origin \u003cbranch name\u003e\n```\n\n---\n\n## License\n\nThis repository is unlicense[d], so feel free to fork.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmnestorov%2Fgit-commands","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmnestorov%2Fgit-commands","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmnestorov%2Fgit-commands/lists"}