{"id":13394615,"url":"https://github.com/k88hudson/git-flight-rules","last_synced_at":"2026-01-25T07:36:25.647Z","repository":{"id":18901430,"uuid":"22119721","full_name":"k88hudson/git-flight-rules","owner":"k88hudson","description":"Flight rules for git","archived":false,"fork":false,"pushed_at":"2025-10-27T01:17:31.000Z","size":1035,"stargazers_count":42443,"open_issues_count":16,"forks_count":3203,"subscribers_count":643,"default_branch":"master","last_synced_at":"2025-10-27T03:15:14.808Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"cc-by-sa-4.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/k88hudson.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","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,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2014-07-22T20:35:09.000Z","updated_at":"2025-10-27T01:28:27.000Z","dependencies_parsed_at":"2023-02-17T01:15:21.128Z","dependency_job_id":"fbac8d24-1b9e-453c-8185-90300bee5ce7","html_url":"https://github.com/k88hudson/git-flight-rules","commit_stats":{"total_commits":337,"total_committers":128,"mean_commits":2.6328125,"dds":0.8011869436201781,"last_synced_commit":"e1fd8abce866303d217ff305dfd2a1ebd4d4b776"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/k88hudson/git-flight-rules","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/k88hudson%2Fgit-flight-rules","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/k88hudson%2Fgit-flight-rules/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/k88hudson%2Fgit-flight-rules/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/k88hudson%2Fgit-flight-rules/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/k88hudson","download_url":"https://codeload.github.com/k88hudson/git-flight-rules/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/k88hudson%2Fgit-flight-rules/sbom","scorecard":{"id":546548,"data":{"date":"2025-08-11","repo":{"name":"github.com/k88hudson/git-flight-rules","commit":"69f7635ed1b7e5671a8b673f53f9bc406c8e1505"},"scorecard":{"version":"v5.2.1-40-gf6ed084d","commit":"f6ed084d17c9236477efd66e5b258b9d4cc7b389"},"score":4.7,"checks":[{"name":"Code-Review","score":8,"reason":"Found 23/28 approved changesets -- score normalized to 8","details":null,"documentation":{"short":"Determines if the project requires human code review before pull requests (aka merge requests) are merged.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#code-review"}},{"name":"Binary-Artifacts","score":10,"reason":"no binaries found in the repo","details":null,"documentation":{"short":"Determines if the project has generated executable (binary) artifacts in the source repository.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#binary-artifacts"}},{"name":"Dangerous-Workflow","score":-1,"reason":"no workflows found","details":null,"documentation":{"short":"Determines if the project's GitHub Action workflows avoid dangerous patterns.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#dangerous-workflow"}},{"name":"Maintained","score":0,"reason":"0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0","details":null,"documentation":{"short":"Determines if the project is \"actively maintained\".","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#maintained"}},{"name":"Packaging","score":-1,"reason":"packaging workflow not detected","details":["Warn: no GitHub/GitLab publishing workflow detected."],"documentation":{"short":"Determines if the project is published as a package that others can easily download, install, easily update, and uninstall.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#packaging"}},{"name":"Token-Permissions","score":-1,"reason":"No tokens found","details":null,"documentation":{"short":"Determines if the project's workflows follow the principle of least privilege.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#token-permissions"}},{"name":"Pinned-Dependencies","score":-1,"reason":"no dependencies found","details":null,"documentation":{"short":"Determines if the project has declared and pinned the dependencies of its build process.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#pinned-dependencies"}},{"name":"CII-Best-Practices","score":0,"reason":"no effort to earn an OpenSSF best practices badge detected","details":null,"documentation":{"short":"Determines if the project has an OpenSSF (formerly CII) Best Practices Badge.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#cii-best-practices"}},{"name":"Security-Policy","score":0,"reason":"security policy file not detected","details":["Warn: no security policy file detected","Warn: no security file to analyze","Warn: no security file to analyze","Warn: no security file to analyze"],"documentation":{"short":"Determines if the project has published a security policy.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#security-policy"}},{"name":"Fuzzing","score":0,"reason":"project is not fuzzed","details":["Warn: no fuzzer integrations found"],"documentation":{"short":"Determines if the project uses fuzzing.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#fuzzing"}},{"name":"License","score":10,"reason":"license file detected","details":["Info: project has a license file: LICENSE:0","Info: FSF or OSI recognized license: Creative Commons Attribution Share Alike 4.0 International: LICENSE:0"],"documentation":{"short":"Determines if the project has defined a license.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#license"}},{"name":"Signed-Releases","score":-1,"reason":"no releases found","details":null,"documentation":{"short":"Determines if the project cryptographically signs release artifacts.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#signed-releases"}},{"name":"Vulnerabilities","score":10,"reason":"0 existing vulnerabilities detected","details":null,"documentation":{"short":"Determines if the project has open, known unfixed vulnerabilities.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#vulnerabilities"}},{"name":"Branch-Protection","score":-1,"reason":"internal error: error during branchesHandler.setup: internal error: githubv4.Query: Resource not accessible by integration","details":null,"documentation":{"short":"Determines if the default and release branches are protected with GitHub's branch protection settings.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#branch-protection"}},{"name":"SAST","score":0,"reason":"SAST tool is not run on all commits -- score normalized to 0","details":["Warn: 0 commits out of 26 are checked with a SAST tool"],"documentation":{"short":"Determines if the project uses static code analysis.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#sast"}}]},"last_synced_at":"2025-08-20T09:41:29.273Z","repository_id":18901430,"created_at":"2025-08-20T09:41:29.273Z","updated_at":"2025-08-20T09:41:29.273Z"},"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28747674,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-25T05:12:38.112Z","status":"ssl_error","status_checked_at":"2026-01-25T05:04:50.338Z","response_time":113,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: 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":[],"created_at":"2024-07-30T17:01:25.598Z","updated_at":"2026-01-25T07:36:25.639Z","avatar_url":"https://github.com/k88hudson.png","language":null,"readme":"# Flight rules for Git\n\n🌍\n*[English](README.md) ∙ [Español](README_es.md)  ∙  [Русский](README_ru.md) ∙ [繁體中文](README_zh-TW.md) ∙ [简体中文](README_zh-CN.md) ∙ [한국어](README_kr.md)  ∙  [Tiếng Việt](README_vi.md) ∙ [Français](README_fr.md) ∙ [日本語](README_ja.md)*\n\n#### What are \"flight rules\"?\n\nA guide for astronauts (now, programmers using Git) about what to do when things go wrong.\n\n\u003e  *Flight Rules* are the hard-earned body of knowledge recorded in manuals that list, step-by-step, what to do if X occurs, and why. Essentially, they are extremely detailed, scenario-specific standard operating procedures. [...]\n\n\u003e NASA has been capturing our missteps, disasters and solutions since the early 1960s, when Mercury-era ground teams first started gathering \"lessons learned\" into a compendium that now lists thousands of problematic situations, from engine failure to busted hatch handles to computer glitches, and their solutions.\n\n\u0026mdash; Chris Hadfield, *An Astronaut's Guide to Life on Earth*.\n\n#### Conventions for this document\n\nFor clarity's sake all examples in this document use a customized bash prompt in order to indicate the current branch and whether or not there are staged changes. The branch is enclosed in parentheses, and a `*` next to the branch name indicates staged changes.\n\nAll commands should work for at least git version 2.13.0. See the [git website](https://www.git-scm.com/) to update your local git version.\n\n[![Join the chat at https://gitter.im/k88hudson/git-flight-rules](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/k88hudson/git-flight-rules?utm_source=badge\u0026utm_medium=badge\u0026utm_campaign=pr-badge\u0026utm_content=badge)\n\u003c!-- START doctoc generated TOC please keep comment here to allow auto update --\u003e\n\u003c!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE --\u003e\n**Table of Contents**  *generated with [DocToc](https://github.com/thlorenz/doctoc)*\n\n  - [Repositories](#repositories)\n    - [I want to start a local repository](#i-want-to-start-a-local-repository)\n    - [I want to clone a remote repository](#i-want-to-clone-a-remote-repository)\n    - [I set the wrong remote repository](#i-set-the-wrong-remote-repository)\n    - [I want to add code to someone else's repository](#i-want-to-add-code-to-someone-elses-repository)\n      - [Suggesting code via pull requests](#suggesting-code-via-pull-requests)\n      - [Suggesting code via patches](#suggesting-code-via-patches)\n      - [I need to update my fork with latest updates from the original repository](#i-need-to-update-my-fork-with-latest-updates-from-the-original-repository)\n  - [Editing Commits](#editing-commits)\n    - [What did I just commit?](#what-did-i-just-commit)\n    - [I wrote the wrong thing in a commit message](#i-wrote-the-wrong-thing-in-a-commit-message)\n    - [I committed with the wrong name and email configured](#i-committed-with-the-wrong-name-and-email-configured)\n    - [I want to remove a file from the previous commit](#i-want-to-remove-a-file-from-the-previous-commit)\n    - [I want to move a change from one commit to another](#i-want-to-move-a-change-from-one-commit-to-another)\n    - [I want to delete or remove my last commit](#i-want-to-delete-or-remove-my-last-commit)\n    - [Delete/remove arbitrary commit](#deleteremove-arbitrary-commit)\n    - [I tried to push my amended commit to a remote, but I got an error message](#i-tried-to-push-my-amended-commit-to-a-remote-but-i-got-an-error-message)\n    - [I accidentally did a hard reset, and I want my changes back](#i-accidentally-did-a-hard-reset-and-i-want-my-changes-back)\n    - [I accidentally committed and pushed a merge](#i-accidentally-committed-and-pushed-a-merge)\n    - [I accidentally committed and pushed files containing sensitive data](#i-accidentally-committed-and-pushed-files-containing-sensitive-data)\n    - [I want to remove a large file from ever existing in repo history](#i-want-to-remove-a-large-file-from-ever-existing-in-repo-history)\n      - [Recommended Technique: Use third-party bfg](#recommended-technique-use-third-party-bfg)\n      - [Built-in Technique: Use git-filter-branch](#built-in-technique-use-git-filter-branch)\n      - [Final Step: Pushing your changed repo history](#final-step-pushing-your-changed-repo-history)\n    - [I need to change the content of a commit which is not my last](#i-need-to-change-the-content-of-a-commit-which-is-not-my-last)\n  - [Staging](#staging)\n    - [I want to stage all tracked files and leave untracked files](#i-want-to-stage-all-tracked-files-and-leave-untracked-files)\n      - [To stage part of tracked files](#to-stage-part-of-tracked-files)\n    - [I need to add staged changes to the previous commit](#i-need-to-add-staged-changes-to-the-previous-commit)\n    - [I want to stage part of a new file, but not the whole file](#i-want-to-stage-part-of-a-new-file-but-not-the-whole-file)\n    - [I want to add changes in one file to two different commits](#i-want-to-add-changes-in-one-file-to-two-different-commits)\n    - [I staged too many edits, and I want to break them out into a separate commit](#i-staged-too-many-edits-and-i-want-to-break-them-out-into-a-separate-commit)\n    - [I want to stage my unstaged edits, and unstage my staged edits](#i-want-to-stage-my-unstaged-edits-and-unstage-my-staged-edits)\n    - [I want to unstage a specific staged file](#i-want-to-unstage-a-specific-staged-file)\n  - [Discarding changes](#discarding-changes)\n    - [I want to discard my local uncommitted changes (staged and unstaged)](#i-want-to-discard-my-local-uncommitted-changes-staged-and-unstaged)\n    - [I want to discard specific unstaged changes](#i-want-to-discard-specific-unstaged-changes)\n    - [I want to discard specific unstaged files](#i-want-to-discard-specific-unstaged-files)\n    - [I want to discard only my unstaged local changes](#i-want-to-discard-only-my-unstaged-local-changes)\n    - [I want to discard all of my untracked files](#i-want-to-discard-all-of-my-untracked-files)\n  - [Branches](#branches)\n    - [I want to list all branches](#i-want-to-list-all-branches)\n    - [Create a branch from a commit](#create-a-branch-from-a-commit)\n    - [I pulled from/into the wrong branch](#i-pulled-frominto-the-wrong-branch)\n    - [I want to discard local commits so my branch is the same as one on the server](#i-want-to-discard-local-commits-so-my-branch-is-the-same-as-one-on-the-server)\n    - [I want to move my unstaged edits to a new branch](#i-want-to-move-my-unstaged-edits-to-a-new-branch)\n    - [I want to move my unstaged edits to a different, existing branch](#i-want-to-move-my-unstaged-edits-to-a-different-existing-branch)\n    - [I committed to main instead of a new branch](#i-committed-to-main-instead-of-a-new-branch)\n    - [I want to keep the whole file from another ref-ish](#i-want-to-keep-the-whole-file-from-another-ref-ish)\n    - [I made several commits on a single branch that should be on different branches](#i-made-several-commits-on-a-single-branch-that-should-be-on-different-branches)\n    - [I want to delete local branches that were deleted upstream](#i-want-to-delete-local-branches-that-were-deleted-upstream)\n    - [I accidentally deleted my branch](#i-accidentally-deleted-my-branch)\n    - [I want to delete a branch](#i-want-to-delete-a-branch)\n    - [I want to delete multiple branches](#i-want-to-delete-multiple-branches)\n    - [I want to rename a branch](#i-want-to-rename-a-branch)\n    - [I want to checkout to a remote branch that someone else is working on](#i-want-to-checkout-to-a-remote-branch-that-someone-else-is-working-on)\n    - [I want to create a new remote branch from current local one](#i-want-to-create-a-new-remote-branch-from-current-local-one)\n    - [I want to set a remote branch as the upstream for a local branch](#i-want-to-set-a-remote-branch-as-the-upstream-for-a-local-branch)\n    - [I want to set my HEAD to track the default remote branch](#i-want-to-set-my-head-to-track-the-default-remote-branch)\n    - [I made changes on the wrong branch](#i-made-changes-on-the-wrong-branch)\n    - [I want to split a branch into two](#i-want-to-split-a-branch-into-two)\n  - [Rebasing and Merging](#rebasing-and-merging)\n    - [I want to undo rebase/merge](#i-want-to-undo-rebasemerge)\n    - [I rebased, but I don't want to force push](#i-rebased-but-i-dont-want-to-force-push)\n    - [I need to combine commits](#i-need-to-combine-commits)\n      - [Safe merging strategy](#safe-merging-strategy)\n      - [I need to merge a branch into a single commit](#i-need-to-merge-a-branch-into-a-single-commit)\n      - [I want to combine only unpushed commits](#i-want-to-combine-only-unpushed-commits)\n      - [I need to abort the merge](#i-need-to-abort-the-merge)\n    - [I need to update the parent commit of my branch](#i-need-to-update-the-parent-commit-of-my-branch)\n    - [Check if all commits on a branch are merged](#check-if-all-commits-on-a-branch-are-merged)\n    - [Possible issues with interactive rebases](#possible-issues-with-interactive-rebases)\n      - [The rebase editing screen says 'noop'](#the-rebase-editing-screen-says-noop)\n      - [There were conflicts](#there-were-conflicts)\n  - [Stash](#stash)\n    - [Stash all edits](#stash-all-edits)\n    - [Stash specific files](#stash-specific-files)\n    - [Stash with message](#stash-with-message)\n    - [Apply a specific stash from list](#apply-a-specific-stash-from-list)\n    - [Stash while keeping unstaged edits](#stash-while-keeping-unstaged-edits)\n  - [Finding](#finding)\n    - [I want to find a string in any commit](#i-want-to-find-a-string-in-any-commit)\n    - [I want to find by author/committer](#i-want-to-find-by-authorcommitter)\n    - [I want to list commits containing specific files](#i-want-to-list-commits-containing-specific-files)\n    - [I want to view the commit history for a specific function](#i-want-to-view-the-commit-history-for-a-specific-function)\n    - [Find a tag where a commit is referenced](#find-a-tag-where-a-commit-is-referenced)\n  - [Submodules](#submodules)\n    - [Clone all submodules](#clone-all-submodules)\n    - [Remove a submodule](#remove-a-submodule)\n  - [Miscellaneous Objects](#miscellaneous-objects)\n    - [Copy a folder or file from one branch to another](#copy-a-folder-or-file-from-one-branch-to-another)\n    - [Restore a deleted file](#restore-a-deleted-file)\n    - [Delete tag](#delete-tag)\n    - [Recover a deleted tag](#recover-a-deleted-tag)\n    - [Deleted Patch](#deleted-patch)\n    - [Exporting a repository as a Zip file](#exporting-a-repository-as-a-zip-file)\n    - [Push a branch and a tag that have the same name](#push-a-branch-and-a-tag-that-have-the-same-name)\n  - [Tracking Files](#tracking-files)\n    - [I want to change a file name's capitalization, without changing the contents of the file](#i-want-to-change-a-file-names-capitalization-without-changing-the-contents-of-the-file)\n    - [I want to overwrite local files when doing a git pull](#i-want-to-overwrite-local-files-when-doing-a-git-pull)\n    - [I want to remove a file from Git but keep the file](#i-want-to-remove-a-file-from-git-but-keep-the-file)\n    - [I want to revert a file to a specific revision](#i-want-to-revert-a-file-to-a-specific-revision)\n    - [I want to list changes of a specific file between commits or branches](#i-want-to-list-changes-of-a-specific-file-between-commits-or-branches)\n    - [I want Git to ignore changes to a specific file](#i-want-git-to-ignore-changes-to-a-specific-file)\n  - [Debugging with Git](#debugging-with-git)\n  - [Configuration](#configuration)\n    - [I want to add aliases for some Git commands](#i-want-to-add-aliases-for-some-git-commands)\n    - [I want to add an empty directory to my repository](#i-want-to-add-an-empty-directory-to-my-repository)\n    - [I want to cache a username and password for a repository](#i-want-to-cache-a-username-and-password-for-a-repository)\n    - [I want to make Git ignore permissions and filemode changes](#i-want-to-make-git-ignore-permissions-and-filemode-changes)\n    - [I want to set a global user](#i-want-to-set-a-global-user)\n  - [I've no idea what I did wrong](#ive-no-idea-what-i-did-wrong)\n  - [Git Shortcuts](#git-shortcuts)\n    - [Git Bash](#git-bash)\n    - [PowerShell on Windows](#powershell-on-windows)\n- [Other Resources](#other-resources)\n  - [Books](#books)\n  - [Tutorials](#tutorials)\n  - [Scripts and Tools](#scripts-and-tools)\n  - [GUI Clients](#gui-clients)\n\n\u003c!-- END doctoc generated TOC please keep comment here to allow auto update --\u003e\n\n## Repositories\n\n### I want to start a local repository\n\nTo initialize an existing directory as a Git repository:\n\n```sh\n(my-folder) $ git init\n```\n\n### I want to clone a remote repository\n\nTo clone (copy) a remote repository, copy the URL for the repository, and run:\n\n```sh\n$ git clone [url]\n```\n\nThis will save it to a folder named the same as the remote repository's. Make sure you have a connection to the remote server you are cloning from (for most purposes this means making sure you are connected to the internet).\n\nTo clone it into a folder with a different name than the default repository name:\n\n```sh\n$ git clone [url] name-of-new-folder\n```\n\n### I set the wrong remote repository\n\nThere are a few possible problems here:\n\nIf you cloned the wrong repository, simply delete the directory created after running `git clone` and clone the correct repository.\n\nIf you set the wrong repository as the origin of an existing local repository, change the URL of your origin by running:\n\n```sh\n$ git remote set-url origin [url of the actual repo]\n```\n\nFor more, see [this StackOverflow topic](https://stackoverflow.com/questions/2432764/how-to-change-the-uri-url-for-a-remote-git-repository#2432799).\n\n\n### I want to add code to someone else's repository\n\nGit doesn't allow you to add code to someone else's repository without access rights. Neither does GitHub, which is not the same as Git, but rather a hosted service for Git repositories. However, you can suggest code using patches, or, on GitHub, forks and pull requests.\n\nFirst, a bit about forking. A fork is a copy of a repository. It is not a git operation, but is a common action on GitHub, Bitbucket, GitLab — or anywhere people host Git repositories. You can fork a repository through the hosted UI.\n\n#### Suggesting code via pull requests\n\nAfter you've forked a repository, you normally need to clone the repository to your machine. You can do some small edits on GitHub, for instance, without cloning, but this isn't a github-flight-rules list, so let's go with how to do this locally.\n\n```sh\n# if you are using ssh\n$ git clone git@github.com:k88hudson/git-flight-rules.git\n\n# if you are using https\n$ git clone https://github.com/k88hudson/git-flight-rules.git\n```\n\nIf you `cd` into the resulting directory, and type `git remote`, you'll see a list of the remotes. Normally there will be one remote - `origin` - which will point to `k88hudson/git-flight-rules`. In this case, we also want a remote that will point to your fork.\n\nFirst, to follow a Git convention, we normally use the remote name `origin` for your own repository and `upstream` for whatever you've forked. So, rename the `origin` remote to `upstream`\n\n```sh\n$ git remote rename origin upstream\n```\n\nYou can also do this using `git remote set-url`, but it takes longer and is more steps.\n\nThen, set up a new remote that points to your project.\n\n```sh\n$ git remote add origin git@github.com:YourName/git-flight-rules.git\n```\n\nNote that now you have two remotes.\n\n- `origin` references your own repository.\n- `upstream` references the original one.\n\nFrom origin, you can read and write. From upstream, you can only read.\n\nWhen you've finished making whatever changes you like, push your changes (normally in a branch) to the remote named `origin`. If you're on a branch, you could use `--set-upstream` to avoid specifying the remote tracking branch on every future push using this branch. For instance:\n\n```sh\n$ (feature/my-feature) git push --set-upstream origin feature/my-feature\n```\n\nThere is no way to suggest a pull request using the CLI using Git (although there are tools, like [hub](http://github.com/github/hub), which will do this for you). So, if you're ready to make a pull request, go to your GitHub (or another Git host) and create a new pull request. Note that your host automatically links the original and forked repositories.\n\nAfter all of this, do not forget to respond to any code review feedback.\n\n#### Suggesting code via patches\n\nAnother approach to suggesting code changes that doesn't rely on third party sites such as Github is to use `git format-patch`.\n\n`format-patch` creates a .patch file for one or more commits. This file is essentially a list of changes that looks similar to the commit diffs you can view on Github.\n\nA patch can be viewed and even edited by the recipient and applied using `git am`.\n\nFor example, to create a patch based on the previous commit you would run `git format-patch HEAD^` which would create a .patch file called something like 0001-My-Commit-Message.patch.\n\nTo apply this patch file to your repository you would run `git am ./0001-My-Commit-Message.patch`.\n\nPatches can also be sent via email using the `git send-email` command. For information on usage and configuration see: https://git-send-email.io\n\n#### I need to update my fork with latest updates from the original repository\n\nAfter a while, the `upstream` repository may have been updated, and these updates need to be pulled into your `origin` repo. Remember that like you, other people are contributing too. Suppose that you are in your own feature branch and you need to update it with the original repository updates.\n\nYou probably have set up a remote that points to the original project. If not, do this now. Generally we use `upstream` as a remote name:\n\n```sh\n$ (main) git remote add upstream \u003clink-to-original-repository\u003e\n# $ (main) git remote add upstream git@github.com:k88hudson/git-flight-rules.git\n```\n\nNow you can fetch from upstream and get the latest updates.\n\n```sh\n$ (main) git fetch upstream\n$ (main) git merge upstream/main\n\n# or using a single command\n$ (main) git pull upstream main\n```\n\n## Editing Commits\n\n\u003ca name=\"diff-last\"\u003e\u003c/a\u003e\n### What did I just commit?\n\nLet's say that you just blindly committed changes with `git commit -a` and you're not sure what the actual content of the commit you just made was. You can show the latest commit on your current HEAD with:\n\n```sh\n(main)$ git show\n```\n\nOr\n\n```sh\n$ git log -n1 -p\n```\n\nIf you want to see a file at a specific commit, you can also do this (where `\u003ccommitid\u003e` is the commit you're interested in):\n\n```sh\n$ git show \u003ccommitid\u003e:filename\n```\n\n\u003ca name=\"wrong-thing-in-commit-message\"\u003e\u003c/a\u003e\n### I wrote the wrong thing in a commit message\n\nIf you wrote the wrong thing and the commit has not yet been pushed, you can do the following to change the commit message without changing the changes in the commit:\n\n```sh\n$ git commit --amend --only\n```\n\nThis will open your default text editor, where you can edit the message. On the other hand, you can do this all in one command:\n\n```sh\n$ git commit --amend --only -m 'xxxxxxx'\n```\n\nIf you have already pushed the message, you can amend the commit and force push, but this is not recommended.\n\n\u003ca name=\"commit-wrong-author\"\u003e\u003c/a\u003e\n### I committed with the wrong name and email configured\n\nIf it's a single commit, amend it\n\n```sh\n$ git commit --amend --no-edit --author \"New Authorname \u003cauthoremail@mydomain.com\u003e\"\n```\n\nAn alternative is to correctly configure your author settings in `git config --global author.(name|email)` and then use\n\n```sh\n$ git commit --amend --reset-author --no-edit\n```\n\nIf you need to change for multiple commits, you can use\n\n```sh\n$ git -c rebase.instructionFormat='%s%nexec GIT_COMMITTER_DATE=\"%cD\" GIT_AUTHOR_DATE=\"%aD\" git commit --amend --no-edit --reset-author' rebase -r \u003ccommit\u003e\n```\n\n`\u003ccommit\u003e` is a commit before all your bad commits. If you need to change all of history in the current branch including the root of the branch, put `--root` there instead.\n\nIf you need to change all of history, see the man page for `git filter-branch`.\n\nNote this will change the history and a force push is required.\n\n### I want to remove a file from the previous commit\n\nIn order to remove changes for a file from the previous commit, do the following:\n\n```sh\n$ git checkout HEAD^ myfile\n$ git add myfile\n$ git commit --amend --no-edit\n```\n\nIn case the file was newly added to the commit and you want to remove it (from Git alone), do:\n\n```sh\n$ git rm --cached myfile\n$ git commit --amend --no-edit\n```\n\nThis is particularly useful when you have an open patch and you have committed an unnecessary file, and need to force push to update the patch on a remote. The `--no-edit` option is used to keep the existing commit message.\n\n\u003ca name=\"move-change-to-new-commit\"\u003e\u003c/a\u003e\n### I want to move a change from one commit to another\nIf you've made a commit that includes changes that would fit better in another commit, you can move the changes to the other commit using an interactive rebase. This comes from [stackoverflow](https://stackoverflow.com/a/54985304/2491502).\n\nFor example, you have three commits (a, b, c). On b, you've changes file1 and file2 and you want to move the change on file1 from commit b to commit a.\n\nFirst, rebase interactively:\n\n```sh\n$ git rebase -i HEAD~3\n```\n\nThis will open an editor with the following:\n\n```sh\npick a\npick b\npick c\n```\n\nChange the lines with a and b to edit:\n\n```sh\nedit a\nedit b\npick c\n```\n\nSave and close the editor. This will bring you to commit b. Now, reset the file1 changes:\n\n```sh\n$ git reset HEAD~1 file1\n```\n\nThis will unstage the changes in file1. Now, stash those changes and continue the rebase:\n\n```sh\n$ git stash\n$ git rebase --continue\n```\n\nNow you will be editing commit a. Unstash the changes then add them to the current commit and continue the rebase:\n\n```sh\n$ git stash pop\n$ git add file1\n$ git commit --amend --no-edit\n$ git rebase --continue\n```\n\nNow your rebase is complete, with the changes from b on a. If you wanted to move the changes from b to c, you would have to do two rebases since c comes before b: one to get the changes out of b, then another to edit c and add the stashed changes.\n\n\u003ca name=\"delete-pushed-commit\"\u003e\u003c/a\u003e\n### I want to delete or remove my last commit\n\nIf you need to delete pushed commits, you can use the following. However, it will irreversibly change your history, and mess up the history of anyone else who had already pulled from the repository. In short, if you're not sure, you should never do this, ever.\n\n```sh\n$ git reset HEAD^ --hard\n$ git push --force-with-lease [remote] [branch]\n```\n\nIf you haven't pushed, to reset Git to the state it was in before you made your last commit (while keeping your staged changes):\n\n```\n(my-branch)$ git reset --soft HEAD^\n```\n\nThis only works if you haven't pushed. If you have pushed, the only truly safe thing to do is `git revert SHAofBadCommit`. That will create a new commit that undoes all the previous commit's changes. Or, if the branch you pushed to is rebase-safe (ie. other devs aren't expected to pull from it), you can just use `git push --force-with-lease`. For more, see [the above section](#deleteremove-last-pushed-commit).\n\n\u003ca name=\"delete-any-commit\"\u003e\u003c/a\u003e\n### Delete/remove arbitrary commit\n\nThe same warning applies as above. Never do this if possible.\n\n```sh\n$ git rebase --onto SHA1_OF_BAD_COMMIT^ SHA1_OF_BAD_COMMIT\n$ git push --force-with-lease [remote] [branch]\n```\n\nOr do an [interactive rebase](#interactive-rebase) and remove the line(s) corresponding to commit(s) you want to see removed.\n\n\u003ca name=\"force-push\"\u003e\u003c/a\u003e\n### I tried to push my amended commit to a remote, but I got an error message\n\n```sh\nTo https://github.com/yourusername/repo.git\n! [rejected]        mybranch -\u003e mybranch (non-fast-forward)\nerror: failed to push some refs to 'https://github.com/tanay1337/webmaker.org.git'\nhint: Updates were rejected because the tip of your current branch is behind\nhint: its remote counterpart. Integrate the remote changes (e.g.\nhint: 'git pull ...') before pushing again.\nhint: See the 'Note about fast-forwards' in 'git push --help' for details.\n```\n\nNote that, as with rebasing (see below), amending **replaces the old commit with a new one**, so you must force push (`--force-with-lease`) your changes if you have already pushed the pre-amended commit to your remote. Be careful when you do this \u0026ndash; *always* make sure you specify a branch!\n\n```sh\n(my-branch)$ git push origin mybranch --force-with-lease\n```\n\nIn general, **avoid force pushing**. It is best to create and push a new commit rather than force-pushing the amended commit as it will cause conflicts in the source history for any other developer who has interacted with the branch in question or any child branches. `--force-with-lease` will still fail, if someone else was also working on the same branch as you, and your push would overwrite those changes.\n\nIf you are *absolutely* sure that nobody is working on the same branch or you want to update the tip of the branch *unconditionally*, you can use `--force` (`-f`), but this should be avoided in general.\n\n\u003ca name=\"undo-git-reset-hard\"\u003e\u003c/a\u003e\n### I accidentally did a hard reset, and I want my changes back\n\nIf you accidentally do `git reset --hard`, you can normally still get your commit back, as git keeps a log of everything for a few days.\n\nNote: This is only valid if your work is backed up, i.e., either committed or stashed. `git reset --hard` _will remove_ uncommitted modifications, so use it with caution. (A safer option is `git reset --keep`.)\n\n```sh\n(main)$ git reflog\n```\n\nYou'll see a list of your past commits, and a commit for the reset. Choose the SHA of the commit you want to return to, and reset again:\n\n```sh\n(main)$ git reset --hard SHA1234\n```\n\nAnd you should be good to go.\n\n\u003ca name=\"undo-a-commit-merge\"\u003e\u003c/a\u003e\n### I accidentally committed and pushed a merge\n\nIf you accidentally merged a feature branch to the main development branch before it was ready to be merged, you can still undo the merge. But there's a catch: A merge commit has more than one parent (usually two).\n\nThe command to use\n```sh\n(feature-branch)$ git revert -m 1 \u003ccommit\u003e\n```\nwhere the -m 1 option says to select parent number 1 (the branch into which the merge was made) as the parent to revert to.\n\nNote: the parent number is not a commit identifier. Rather, a merge commit has a line `Merge: 8e2ce2d 86ac2e7`. The parent number is the 1-based index of the desired parent on this line, the first identifier is number 1, the second is number 2, and so on.\n\n\u003ca name=\"undo-sensitive-commit-push\"\u003e\u003c/a\u003e\n### I accidentally committed and pushed files containing sensitive data\n\nIf you accidentally pushed files containing sensitive, or private data (passwords, keys, etc.), you can amend the previous commit. Keep in mind that once you have pushed a commit, you should consider any data it contains to be compromised. These steps can remove the sensitive data from your public repo or your local copy, but you **cannot** remove the sensitive data from other people's pulled copies. If you committed a password, **change it immediately**. If you committed a key, **re-generate it immediately**. Amending the pushed commit is not enough, since anyone could have pulled the original commit containing your sensitive data in the meantime.\n\nIf you edit the file and remove the sensitive data, then run\n```sh\n(feature-branch)$ git add edited_file\n(feature-branch)$ git commit --amend --no-edit\n(feature-branch)$ git push --force-with-lease origin [branch]\n```\n\nIf you want to remove an entire file (but keep it locally), then run\n```sh\n(feature-branch)$ git rm --cached sensitive_file\necho sensitive_file \u003e\u003e .gitignore\n(feature-branch)$ git add .gitignore\n(feature-branch)$ git commit --amend --no-edit\n(feature-branch)$ git push --force-with-lease origin [branch]\n```\nAlternatively store your sensitive data in local environment variables.\n\nIf you want to completely remove an entire file (and not keep it locally), then run\n\n```sh\n(feature-branch)$ git rm sensitive_file\n(feature-branch)$ git commit --amend --no-edit\n(feature-branch)$ git push --force-with-lease origin [branch]\n```\n\nIf you have made other commits in the meantime (i.e. the sensitive data is in a commit before the previous commit), you will have to rebase.\n\n\u003ca name=\"remove-large-file-in-repo-history\"\u003e\u003c/a\u003e\n### I want to remove a large file from ever existing in repo history\n\nIf the file you want to delete is secret or sensitive, instead see [how to remove sensitive files](#undo-sensitive-commit-push).\n\nEven if you delete a large or unwanted file in a recent commit, it still exists in git history, in your repo's `.git` folder, and will make `git clone` download unneeded files.\n\nThe actions in this part of the guide will require a force push, and rewrite large sections of repo history, so if you are working with remote collaborators, check first that any local work of theirs is pushed.\n\nThere are two options for rewriting history, the built-in `git-filter-branch` or [`bfg-repo-cleaner`](https://rtyley.github.io/bfg-repo-cleaner/). `bfg` is significantly cleaner and more performant, but it is a third-party download and requires java. We will describe both alternatives. The final step is to force push your changes, which requires special consideration on top of a regular force push, given that a great deal of repo history will have been permanently changed.\n\n#### Recommended Technique: Use third-party bfg\n\nUsing bfg-repo-cleaner requires java. Download the bfg jar from the link [here](https://rtyley.github.io/bfg-repo-cleaner/). Our examples will use `bfg.jar`, but your download may have a version number, e.g. `bfg-1.13.0.jar`.\n\nTo delete a specific file.\n\n```sh\n(main)$ git rm path/to/filetoremove\n(main)$ git commit -m \"Commit removing filetoremove\"\n(main)$ java -jar ~/Downloads/bfg.jar --delete-files filetoremove\n```\n\nNote that in bfg you must use the plain file name even if it is in a subdirectory.\n\nYou can also delete a file by pattern, e.g.:\n\n```sh\n(main)$ git rm *.jpg\n(main)$ git commit -m \"Commit removing *.jpg\"\n(main)$ java -jar ~/Downloads/bfg.jar --delete-files *.jpg\n```\n\nWith bfg, the files that exist on your latest commit will not be affected. For example, if you had several large .tga files in your repo, and then in an earlier commit, you deleted a subset of them, this call does not touch files present in the latest commit\n\nNote, if you renamed a file as part of a commit, e.g. if it started as `LargeFileFirstName.mp4` and a commit changed it to `LargeFileSecondName.mp4`, running `java -jar ~/Downloads/bfg.jar --delete-files LargeFileSecondName.mp4` will not remove it from git history. Either run the `--delete-files` command with both filenames, or with a matching pattern.\n\n#### Built-in Technique: Use git-filter-branch\n\n`git-filter-branch` is more cumbersome and has less features, but you may use it if you cannot install or run `bfg`.\n\nIn the below, replace `filepattern` may be a specific name or pattern, e.g. `*.jpg`. This will remove files matching the pattern from all history and branches.\n\n```sh\n(main)$ git filter-branch --force --index-filter 'git rm --cached --ignore-unmatch filepattern' --prune-empty --tag-name-filter cat -- --all\n```\n\nBehind-the-scenes explanation:\n\n`--tag-name-filter cat` is a cumbersome, but simplest, way to apply the original tags to the new commits, using the command cat.\n\n`--prune-empty` removes any now-empty commits.\n\n#### Final Step: Pushing your changed repo history\n\nOnce you have removed your desired files, test carefully that you haven't broken anything in your repo - if you have, it is easiest to re-clone your repo to start over.\nTo finish, optionally use git garbage collection to minimize your local .git folder size, and then force push.\n```sh\n(main)$ git reflog expire --expire=now --all \u0026\u0026 git gc --prune=now --aggressive\n(main)$ git push origin --force --tags\n```\n\nSince you just rewrote the entire git repo history, the `git push` operation may be too large, and return the error `“The remote end hung up unexpectedly”`. If this happens, you can try increasing the git post buffer:\n```sh\n(main)$ git config http.postBuffer 524288000\n(main)$ git push --force\n```\n\nIf this does not work, you will need to manually push the repo history in chunks of commits. In the command below, try increasing `\u003cnumber\u003e` until the push operation succeeds.\n```sh\n(main)$ git push -u origin HEAD~\u003cnumber\u003e:refs/head/main --force\n```\nOnce the push operation succeeds the first time, decrease `\u003cnumber\u003e` gradually until a conventional `git push` succeeds.\n\n\u003ca name=\"change-content-of-commit-not-my-last\"\u003e\u003c/a\u003e\n### I need to change the content of a commit which is not my last\n\nConsider you created some (e.g. three) commits and later realize you missed doing something that belongs contextually into the first of those commits. This bothers you, because if you'd create a new commit containing those changes, you'd have a clean code base, but your commits weren't atomic (i.e. changes that belonged to each other weren't in the same commit). In such a situation you may want to change the commit where these changes belong to, include them and have the following commits unaltered. In such a case, `git rebase` might save you.\n\nConsider a situation where you want to change the third last commit you made.\n\n```sh\n(your-branch)$ git rebase -i HEAD~4\n```\n\ngets you into interactive rebase mode, which allows you to edit any of your last three commits. A text editor pops up, showing you something like\n\n```sh\npick 9e1d264 The third last commit\npick 4b6e19a The second to last commit\npick f4037ec The last commit\n```\n\nwhich you change into\n\n```sh\nedit 9e1d264 The third last commit\npick 4b6e19a The second to last commit\npick f4037ec The last commit\n```\n\nThis tells rebase that you want to edit your third last commit and keep the other two unaltered. Then you'll save (and close) the editor. Git will then start to rebase. It stops on the commit you want to alter, giving you the chance to edit that commit. Now you can apply the changes which you missed applying when you initially committed that commit. You do so by editing and staging them. Afterwards you'll run\n\n```sh\n(your-branch)$ git commit --amend\n```\n\nwhich tells Git to recreate the commit. Also, Git will ask you to write a new commit message, using the original commit message as a starting point. Having done that, the hard part is solved.\n\n```sh\n(your-branch)$ git rebase --continue\n```\n\nwill do the rest of the work for you.\n\n## Staging\n\n\u003ca name=\"stage-tracked-files-and-leave-untracked-files\"\u003e\u003c/a\u003e\n### I want to stage all tracked files and leave untracked files\n\n```sh\n$ git add -u\n```\n\n#### To stage part of tracked files\n\n```sh\n# to stage files with ext .txt\n$ git add -u *.txt\n\n# to stage all files inside directory src\n$ git add -u src/\n```\n\n\u003ca name=\"add-staged-changes-to-previous-commit\"\u003e\u003c/a\u003e\n### I need to add staged changes to the previous commit\n\n```sh\n(my-branch*)$ git commit --amend\n```\n\nIf you already know you don't want to change the commit message, you can tell git to reuse the commit message:\n\n```sh\n(my-branch*)$ git commit --amend -C HEAD\n```\n\n\u003ca name=\"commit-partial-new-file\"\u003e\u003c/a\u003e\n### I want to stage part of a new file, but not the whole file\n\nNormally, if you want to stage part of a file, you run this:\n\n```sh\n$ git add --patch filename.x\n```\n\n`-p` will work for short. This will open interactive mode. You would be able to use the `s` option to split the commit - however, if the file is new, you will not have this option. To add a new file, do this:\n\n```sh\n$ git add -N filename.x\n```\n\nThen, you will need to use the `e` option to manually choose which lines to add. Running `git diff --cached` or\n`git diff --staged` will show you which lines you have staged compared to which are still saved locally.\n\n\u003ca name=\"stage-in-two-commits\"\u003e\u003c/a\u003e\n### I want to add changes in one file to two different commits\n\n`git add` will add the entire file to a commit. `git add -p` will allow to interactively select which changes you want to add.\n\n\u003ca name=\"selective-unstage-edits\"\u003e\u003c/a\u003e\n### I staged too many edits, and I want to break them out into a separate commit\n\n`git reset -p` will open a patch mode reset dialog.  This is similar to `git add -p`, except that selecting \"yes\" will unstage the change, removing it from the upcoming commit.\n\n\u003ca name=\"unstaging-edits-and-staging-the-unstaged\"\u003e\u003c/a\u003e\n### I want to stage my unstaged edits, and unstage my staged edits\n\nIn many cases, you should unstage all of your staged files and then pick the file you want and commit it. However, if you want to switch the staged and unstaged edits, you can create a temporary commit to store your staged files, stage your unstaged files and then stash them. Then, reset the temporary commit and pop your stash.\n\n```sh\n$ git commit -m \"WIP\"\n$ git add . # This will also add untracked files.\n$ git stash\n$ git reset HEAD^\n$ git stash pop --index 0\n```\n\nNOTE 1: The reason to use `pop` here is want to keep idempotent as much as possible.\nNOTE 2: Your staged files will be marked as unstaged if you don't use the `--index` flag. ([This link](https://stackoverflow.com/questions/31595873/git-stash-with-staged-files-does-stash-convert-staged-files-to-unstaged?answertab=active#tab-top) explains why.)\n\n\u003ca name=\"unstage-specific-staged-file\"\u003e\u003c/a\u003e\n### I want to unstage a specific staged file\n\nSometimes we have one or more files that accidentally ended up being staged, and these files have not been committed before. To unstage them:\n\n```sh\n$ git reset -- \u003cfilename\u003e\n```\n\nThis results in unstaging the file and make it look like it's untracked.\n\n## Discarding changes\n\n\u003ca name=\"discard-local-uncommitted-changes\"\u003e\u003c/a\u003e\n### I want to discard my local uncommitted changes (staged and unstaged)\n\nIf you want to discard all your local staged and unstaged changes, you can do this:\n\n```sh\n(my-branch)$ git reset --hard\n# or\n(main)$ git checkout -f\n```\n\nThis will unstage all files you might have staged with `git add`:\n\n```sh\n$ git reset\n```\n\nThis will revert all local uncommitted changes (should be executed in repo root):\n\n```sh\n$ git checkout .\n```\n\nYou can also revert uncommitted changes to a particular file or directory:\n\n```sh\n$ git checkout [some_dir|file.txt]\n```\n\nYet another way to revert all uncommitted changes (longer to type, but works from any subdirectory):\n\n```sh\n$ git reset --hard HEAD\n```\n\nThis will remove all local untracked files, so only files tracked by Git remain:\n\n```sh\n$ git clean -fd\n```\n\n`-x` will also remove all ignored files.\n\n### I want to discard specific unstaged changes\n\nWhen you want to get rid of some, but not all changes in your working copy.\n\nCheckout undesired changes, keep good changes.\n\n```sh\n$ git checkout -p\n# Answer y to all of the snippets you want to drop\n```\n\nAnother strategy involves using `stash`. Stash all the good changes, reset working copy, and reapply good changes.\n\n```sh\n$ git stash -p\n# Select all of the snippets you want to save\n$ git reset --hard\n$ git stash pop\n```\n\nAlternatively, stash your undesired changes, and then drop stash.\n\n```sh\n$ git stash -p\n# Select all of the snippets you don't want to save\n$ git stash drop\n```\n\n### I want to discard specific unstaged files\n\nWhen you want to get rid of one specific file in your working copy.\n\n```sh\n$ git checkout myFile\n```\n\nAlternatively, to discard multiple files in your working copy, list them all.\n\n```sh\n$ git checkout myFirstFile mySecondFile\n```\n\n### I want to discard only my unstaged local changes\n\nWhen you want to get rid of all of your unstaged local uncommitted changes\n\n```sh\n$ git checkout .\n```\n\u003ca name=\"discard-all-untracked-files\"\u003e\u003c/a\u003e\n### I want to discard all of my untracked files\n\nWhen you want to get rid of all of your untracked files\n\n```sh\n$ git clean -f\n```\n\n## Branches\n\n### I want to list all branches\n\nList local branches\n\n```sh\n$ git branch\n```\n\nList remote branches\n\n```sh\n$ git branch -r\n```\n\nList all branches (both local and remote)\n\n```sh\n$ git branch -a\n```\n\n\u003ca name=\"create-branch-from-commit\"\u003e\u003c/a\u003e\n### Create a branch from a commit\n```sh\n$ git checkout -b \u003cbranch\u003e \u003cSHA1_OF_COMMIT\u003e\n```\n\n\u003ca name=\"pull-wrong-branch\"\u003e\u003c/a\u003e\n### I pulled from/into the wrong branch\n\nThis is another chance to use `git reflog` to see where your HEAD pointed before the bad pull.\n\n```sh\n(main)$ git reflog\nab7555f HEAD@{0}: pull origin wrong-branch: Fast-forward\nc5bc55a HEAD@{1}: checkout: checkout message goes here\n```\n\nSimply reset your branch back to the desired commit:\n\n```sh\n$ git reset --hard c5bc55a\n```\n\nDone.\n\n\u003ca name=\"discard-local-commits\"\u003e\u003c/a\u003e\n### I want to discard local commits so my branch is the same as one on the server\n\nConfirm that you haven't pushed your changes to the server.\n\n`git status` should show how many commits you are ahead of origin:\n\n```sh\n(my-branch)$ git status\n# On branch my-branch\n# Your branch is ahead of 'origin/my-branch' by 2 commits.\n#   (use \"git push\" to publish your local commits)\n#\n```\n\nOne way of resetting branch `my-branch` to match `origin/my-branch` (to have the same as what is on the remote) is to do this:\n\n```sh\n(my-branch)$ git reset --hard origin/my-branch\n```\n\n\u003ca name=\"move-unstaged-edits-to-new-branch\"\u003e\u003c/a\u003e\n### I want to move my unstaged edits to a new branch\n\n```sh\n$ git checkout -b my-branch\n```\n\n\u003ca name=\"move-unstaged-edits-to-old-branch\"\u003e\u003c/a\u003e\n### I want to move my unstaged edits to a different, existing branch\n\n```sh\n$ git stash\n$ git checkout my-branch\n$ git stash pop\n```\n\n\u003ca name=\"commit-wrong-branch\"\u003e\u003c/a\u003e\n### I committed to main instead of a new branch\n\nCreate the new branch while remaining on main:\n\n```sh\n(main)$ git branch my-branch\n```\n\nReset the branch main to the previous commit:\n\n```sh\n(main)$ git reset --hard HEAD^\n```\n\n`HEAD^` is short for `HEAD^1`. This stands for the first parent of `HEAD`, similarly `HEAD^2` stands for the second parent of the commit (merges can have 2 parents).\n\nNote that `HEAD^2` is **not** the same as `HEAD~2` (see [this link](http://www.paulboxley.com/blog/2011/06/git-caret-and-tilde) for more information).\n\nAlternatively, if you don't want to use `HEAD^`, find out what the commit hash you want to set your main branch to (`git log` should do the trick). Then reset to that hash. `git push` will make sure that this change is reflected on your remote.\n\nFor example, if the hash of the commit that your main branch is supposed to be at is `a13b85e`:\n\n```sh\n(main)$ git reset --hard a13b85e\nHEAD is now at a13b85e\n```\n\nCheckout the new branch to continue working:\n\n```sh\n(main)$ git checkout my-branch\n```\n\n\u003ca name=\"keep-whole-file\"\u003e\u003c/a\u003e\n### I want to keep the whole file from another ref-ish\n\nSay you have a working spike (see note), with hundreds of changes. Everything is working. Now, you commit into another branch to save that work:\n\n```sh\n(solution)$ git add -A \u0026\u0026 git commit -m \"Adding all changes from this spike into one big commit.\"\n```\n\nWhen you want to put it into a branch (maybe feature, maybe `develop`), you're interested in keeping whole files. You want to split your big commit into smaller ones.\n\nSay you have:\n\n  * branch `solution`, with the solution to your spike. One ahead of `develop`.\n  * branch `develop`, where you want to add your changes.\n\nYou can solve it bringing the contents to your branch:\n\n```sh\n(develop)$ git checkout solution -- file1.txt\n```\n\nThis will get the contents of that file in branch `solution` to your branch `develop`:\n\n```sh\n# On branch develop\n# Your branch is up-to-date with 'origin/develop'.\n# Changes to be committed:\n#  (use \"git reset HEAD \u003cfile\u003e...\" to unstage)\n#\n#        modified:   file1.txt\n```\n\nThen, commit as usual.\n\nNote: Spike solutions are made to analyze or solve the problem. These solutions are used for estimation and discarded once everyone gets clear visualization of the problem. ~ [Wikipedia](https://en.wikipedia.org/wiki/Extreme_programming_practices).\n\n\u003ca name=\"cherry-pick\"\u003e\u003c/a\u003e\n### I made several commits on a single branch that should be on different branches\n\nSay you are on your main branch. Running `git log`, you see you have made two commits:\n\n```sh\n(main)$ git log\n\ncommit e3851e817c451cc36f2e6f3049db528415e3c114\nAuthor: Alex Lee \u003calexlee@example.com\u003e\nDate:   Tue Jul 22 15:39:27 2014 -0400\n\n    Bug #21 - Added CSRF protection\n\ncommit 5ea51731d150f7ddc4a365437931cd8be3bf3131\nAuthor: Alex Lee \u003calexlee@example.com\u003e\nDate:   Tue Jul 22 15:39:12 2014 -0400\n\n    Bug #14 - Fixed spacing on title\n\ncommit a13b85e984171c6e2a1729bb061994525f626d14\nAuthor: Aki Rose \u003cakirose@example.com\u003e\nDate:   Tue Jul 21 01:12:48 2014 -0400\n\n    First commit\n```\n\nLet's take note of our commit hashes for each bug (`e3851e8` for #21, `5ea5173` for #14).\n\nFirst, let's reset our main branch to the correct commit (`a13b85e`):\n\n```sh\n(main)$ git reset --hard a13b85e\nHEAD is now at a13b85e\n```\n\nNow, we can create a fresh branch for our bug #21:\n\n```sh\n(main)$ git checkout -b 21\n(21)$\n```\n\nNow, let's *cherry-pick* the commit for bug #21 on top of our branch. That means we will be applying that commit, and only that commit, directly on top of whatever our head is at.\n\n```sh\n(21)$ git cherry-pick e3851e8\n```\n\nAt this point, there is a possibility there might be conflicts. See the [**There were conflicts**](#merge-conflict) section in the [interactive rebasing section above](#interactive-rebase) for how to resolve conflicts.\n\nNow let's create a new branch for bug #14, also based on main\n\n```sh\n(21)$ git checkout main\n(main)$ git checkout -b 14\n(14)$\n```\n\nAnd finally, let's cherry-pick the commit for bug #14:\n\n```sh\n(14)$ git cherry-pick 5ea5173\n```\n\n\u003ca name=\"delete-stale-local-branches\"\u003e\u003c/a\u003e\n### I want to delete local branches that were deleted upstream\n\nOnce you merge a pull request on GitHub, it gives you the option to delete the merged branch in your fork. If you aren't planning to keep working on the branch, it's cleaner to delete the local copies of the branch so you don't end up cluttering up your working checkout with a lot of stale branches.\n\n```sh\n$ git fetch -p upstream\n```\n\nwhere, `upstream` is the remote you want to fetch from.\n\n\u003ca name=\"restore-a-deleted-branch\"\u003e\u003c/a\u003e\n### I accidentally deleted my branch\n\nIf you're regularly pushing to remote, you should be safe most of the time. But still sometimes you may end up deleting your branches. Let's say we create a branch and create a new file:\n\n```sh\n(main)$ git checkout -b my-branch\n(my-branch)$ git branch\n(my-branch)$ touch foo.txt\n(my-branch)$ ls\nREADME.md foo.txt\n```\n\nLet's add it and commit.\n\n```sh\n(my-branch)$ git add .\n(my-branch)$ git commit -m 'foo.txt added'\n(my-branch)$ foo.txt added\n 1 files changed, 1 insertions(+)\n create mode 100644 foo.txt\n(my-branch)$ git log\n\ncommit 4e3cd85a670ced7cc17a2b5d8d3d809ac88d5012\nAuthor: siemiatj \u003csiemiatj@example.com\u003e\nDate:   Wed Jul 30 00:34:10 2014 +0200\n\n    foo.txt added\n\ncommit 69204cdf0acbab201619d95ad8295928e7f411d5\nAuthor: Kate Hudson \u003ckatehudson@example.com\u003e\nDate:   Tue Jul 29 13:14:46 2014 -0400\n\n    Fixes #6: Force pushing after amending commits\n```\n\nNow we're switching back to main and 'accidentally' removing our branch.\n\n```sh\n(my-branch)$ git checkout main\nSwitched to branch 'main'\nYour branch is up-to-date with 'origin/main'.\n(main)$ git branch -D my-branch\nDeleted branch my-branch (was 4e3cd85).\n(main)$ echo oh noes, deleted my branch!\noh noes, deleted my branch!\n```\n\nAt this point you should get familiar with 'reflog', an upgraded logger. It stores the history of all the action in the repo.\n\n```\n(main)$ git reflog\n69204cd HEAD@{0}: checkout: moving from my-branch to main\n4e3cd85 HEAD@{1}: commit: foo.txt added\n69204cd HEAD@{2}: checkout: moving from main to my-branch\n```\n\nAs you can see we have commit hash from our deleted branch. Let's see if we can restore our deleted branch.\n\n```sh\n(main)$ git checkout -b my-branch-help\nSwitched to a new branch 'my-branch-help'\n(my-branch-help)$ git reset --hard 4e3cd85\nHEAD is now at 4e3cd85 foo.txt added\n(my-branch-help)$ ls\nREADME.md foo.txt\n```\n\nVoila! We got our removed file back. `git reflog` is also useful when rebasing goes terribly wrong.\n\n### I want to delete a branch\n\nTo delete a remote branch:\n\n```sh\n(main)$ git push origin --delete my-branch\n```\n\nYou can also do:\n\n```sh\n(main)$ git push origin :my-branch\n```\n\nTo delete a local branch:\n\n```sh\n(main)$ git branch -d my-branch\n```\n\nTo delete a local branch that *has not* been merged to the current branch or an upstream:\n\n```sh\n(main)$ git branch -D my-branch\n```\n\n### I want to delete multiple branches\n\nSay you want to delete all branches that start with `fix/`:\n\n```sh\n(main)$ git branch | grep 'fix/' | xargs git branch -d\n```\n\n### I want to rename a branch\n\nTo rename the current (local) branch:\n\n```sh\n(main)$ git branch -m new-name\n```\n\nTo rename a different (local) branch:\n\n```sh\n(main)$ git branch -m old-name new-name\n```\n\nTo delete the `old-name` remote branch and push the `new-name` local branch:\n\n```sh\n(main)$ git push origin :old_name new_name\n```\n\n\u003ca name=\"working-on-checkout-remote-branch\"\u003e\u003c/a\u003e\n### I want to checkout to a remote branch that someone else is working on\n\nFirst, fetch all branches from remote:\n\n```sh\n(main)$ git fetch --all\n```\n\nSay you want to checkout to `daves` from the remote.\n\n```sh\n(main)$ git checkout --track origin/daves\nBranch daves set up to track remote branch daves from origin.\nSwitched to a new branch 'daves'\n```\n\n(`--track` is shorthand for `git checkout -b [branch] [remotename]/[branch]`)\n\nThis will give you a local copy of the branch `daves`, and any update that has been pushed will also show up remotely.\n\n### I want to create a new remote branch from current local one\n\n```sh\n$ git push \u003cremote\u003e HEAD\n```\n\nIf you would also like to set that remote branch as upstream for the current one, use the following instead:\n\n```sh\n$ git push -u \u003cremote\u003e HEAD\n```\n\nWith the `upstream` mode and the `simple` (default in Git 2.0) mode of the `push.default` config, the following command will push the current branch with regards to the remote branch that has been registered previously with `-u`:\n\n```sh\n$ git push\n```\n\nThe behavior of the other modes of `git push` is described in the [doc of `push.default`](https://git-scm.com/docs/git-config#Documentation/git-config.txt-pushdefault).\n\n### I want to set a remote branch as the upstream for a local branch\n\nYou can set a remote branch as the upstream for the current local branch using:\n\n```sh\n$ git branch --set-upstream-to [remotename]/[branch]\n# or, using the shorthand:\n$ git branch -u [remotename]/[branch]\n```\n\nTo set the upstream remote branch for another local branch:\n\n```sh\n$ git branch -u [remotename]/[branch] [local-branch]\n```\n\n\u003ca name=\"head-to-track-remote-branch\"\u003e\u003c/a\u003e\n### I want to set my HEAD to track the default remote branch\n\nBy checking your remote branches, you can see which remote branch your HEAD is tracking. In some cases, this is not the desired branch.\n\n```sh\n$ git branch -r\n  origin/HEAD -\u003e origin/gh-pages\n  origin/main\n```\n\nTo change `origin/HEAD` to track `origin/main`, you can run this command:\n\n```sh\n$ git remote set-head origin --auto\norigin/HEAD set to main\n```\n\n### I made changes on the wrong branch\n\nYou've made uncommitted changes and realise you're on the wrong branch. Stash changes and apply them to the branch you want:\n\n```sh\n(wrong_branch)$ git stash\n(wrong_branch)$ git checkout \u003ccorrect_branch\u003e\n(correct_branch)$ git stash apply\n```\n\n\u003ca name=\"split-branch-into-two\"\u003e\u003c/a\u003e\n### I want to split a branch into two\n\nYou've made a lot of commits on a branch and now want to separate it into two, ending with a branch up to an earlier commit and another with all the changes.\n\nUse `git log` to find the commit where you want to split. Then do the following:\n\n```sh\n(original_branch)$ git checkout -b new_branch\n(new_branch)$ git checkout original_branch\n(original_branch)$ git reset --hard \u003csha1 split here\u003e\n```\n\nIf you had previously pushed the `original_branch` to remote, you will need to do a force push. For more information check [Stack Overflow](https://stackoverflow.com/questions/28983458/how-to-split-a-branch-in-two-with-git/28983843#28983843)\n\n## Rebasing and Merging\n\n\u003ca name=\"undo-rebase\"\u003e\u003c/a\u003e\n### I want to undo rebase/merge\n\nYou may have merged or rebased your current branch with a wrong branch, or you can't figure it out or finish the rebase/merge process. Git saves the original HEAD pointer in a variable called ORIG_HEAD before doing dangerous operations, so it is simple to recover your branch at the state before the rebase/merge.\n\n```sh\n(my-branch)$ git reset --hard ORIG_HEAD\n```\n\n\u003ca name=\"force-push-rebase\"\u003e\u003c/a\u003e\n### I rebased, but I don't want to force push\n\nUnfortunately, you have to force push, if you want those changes to be reflected on the remote branch. This is because you have changed the history. The remote branch won't accept changes unless you force push. This is one of the main reasons many people use a merge workflow, instead of a rebasing workflow - large teams can get into trouble with developers force pushing. Use this with caution. A safer way to use rebase is not to reflect your changes on the remote branch at all, and instead to do the following:\n\n```sh\n(main)$ git checkout my-branch\n(my-branch)$ git rebase -i main\n(my-branch)$ git checkout main\n(main)$ git merge --ff-only my-branch\n```\n\nFor more, see [this SO thread](https://stackoverflow.com/questions/11058312/how-can-i-use-git-rebase-without-requiring-a-forced-push).\n\n\u003ca name=\"interactive-rebase\"\u003e\u003c/a\u003e\n### I need to combine commits\n\nLet's suppose you are working in a branch that is/will become a pull-request against `main`. In the simplest case when all you want to do is to combine *all* commits into a single one and you don't care about commit timestamps, you can reset and recommit. Make sure the main branch is up to date and all your changes committed, then:\n\n```sh\n(my-branch)$ git reset --soft main\n(my-branch)$ git commit -am \"New awesome feature\"\n```\n\nIf you want more control, and also to preserve timestamps, you need to do something called an interactive rebase:\n\n```sh\n(my-branch)$ git rebase -i main\n```\n\nIf you aren't working against another branch you'll have to rebase relative to your `HEAD`. If you want to squash the last 2 commits, for example, you'll have to rebase against `HEAD~2`. For the last 3, `HEAD~3`, etc.\n\n```sh\n(main)$ git rebase -i HEAD~2\n```\n\nAfter you run the interactive rebase command, you will see something like this in your  text editor:\n\n```vim\npick a9c8a1d Some refactoring\npick 01b2fd8 New awesome feature\npick b729ad5 fixup\npick e3851e8 another fix\n\n# Rebase 8074d12..b729ad5 onto 8074d12\n#\n# Commands:\n#  p, pick = use commit\n#  r, reword = use commit, but edit the commit message\n#  e, edit = use commit, but stop for amending\n#  s, squash = use commit, but meld into previous commit\n#  f, fixup = like \"squash\", but discard this commit's log message\n#  x, exec = run command (the rest of the line) using shell\n#\n# These lines can be re-ordered; they are executed from top to bottom.\n#\n# If you remove a line here THAT COMMIT WILL BE LOST.\n#\n# However, if you remove everything, the rebase will be aborted.\n#\n# Note that empty commits are commented out\n```\n\nAll the lines beginning with a `#` are comments, they won't affect your rebase.\n\nThen you replace `pick` commands with any in the list above, and you can also remove commits by removing corresponding lines.\n\nFor example, if you want to **leave the oldest (first) commit alone and combine all the following commits with the second oldest**, you should edit the letter next to each commit except the first and the second to say `f`:\n\n```vim\npick a9c8a1d Some refactoring\npick 01b2fd8 New awesome feature\nf b729ad5 fixup\nf e3851e8 another fix\n```\n\nIf you want to combine these commits **and rename the commit**, you should additionally add an `r` next to the second commit or simply use `s` instead of `f`:\n\n```vim\npick a9c8a1d Some refactoring\npick 01b2fd8 New awesome feature\ns b729ad5 fixup\ns e3851e8 another fix\n```\n\nYou can then rename the commit in the next text prompt that pops up.\n\n```vim\nNewer, awesomer features\n\n# Please enter the commit message for your changes. Lines starting\n# with '#' will be ignored, and an empty message aborts the commit.\n# rebase in progress; onto 8074d12\n# You are currently editing a commit while rebasing branch 'main' on '8074d12'.\n#\n# Changes to be committed:\n#   modified:   README.md\n#\n\n```\n\nIf everything is successful, you should see something like this:\n\n```sh\n(main)$ Successfully rebased and updated refs/heads/main.\n```\n\n#### Safe merging strategy\n`--no-commit` performs the merge but pretends the merge failed and does not autocommit, giving the user a chance to inspect and further tweak the merge result before committing. `no-ff` maintains evidence that a feature branch once existed, keeping project history consistent.\n\n```sh\n(main)$ git merge --no-ff --no-commit my-branch\n```\n\n#### I need to merge a branch into a single commit\n\n```sh\n(main)$ git merge --squash my-branch\n```\n\n\u003ca name=\"rebase-unpushed-commits\"\u003e\u003c/a\u003e\n#### I want to combine only unpushed commits\n\nSometimes you have several work in progress commits that you want to combine before you push them upstream. You don't want to accidentally combine any commits that have already been pushed upstream because someone else may have already made commits that reference them.\n\n```sh\n(main)$ git rebase -i @{u}\n```\n\nThis will do an interactive rebase that lists only the commits that you haven't already pushed, so it will be safe to reorder/fix/squash anything in the list.\n\n#### I need to abort the merge\n\nSometimes the merge can produce problems in certain files, in those cases we can use the option `abort` to abort the current conflict resolution process, and try to reconstruct the pre-merge state.\n\n```sh\n(my-branch)$ git merge --abort\n```\n\nThis command is available since Git version \u003e= 1.7.4\n\n### I need to update the parent commit of my branch\n\nSay I have a main branch, a feature-1 branch branched from main, and a feature-2 branch branched off of feature-1. If I make a commit to feature-1, then the parent commit of feature-2 is no longer accurate (it should be the head of feature-1, since we branched off of it). We can fix this with `git rebase --onto`.\n\n```sh\n(feature-2)$ git rebase --onto feature-1 \u003cthe first commit in your feature-2 branch that you don't want to bring along\u003e feature-2\n```\n\nThis helps in sticky scenarios where you might have a feature built on another feature that hasn't been merged yet, and a bugfix on the feature-1 branch needs to be reflected in your feature-2 branch.\n\n### Check if all commits on a branch are merged\n\nTo check if all commits on a branch are merged into another branch, you should diff between the heads (or any commits) of those branches:\n\n```sh\n(main)$ git log --graph --left-right --cherry-pick --oneline HEAD...feature/120-on-scroll\n```\n\nThis will tell you if any commits are in one but not the other, and will give you a list of any nonshared between the branches. Another option is to do this:\n\n```sh\n(main)$ git log main ^feature/120-on-scroll --no-merges\n```\n\n### Possible issues with interactive rebases\n\n\u003ca name=\"noop\"\u003e\u003c/a\u003e\n#### The rebase editing screen says 'noop'\n\nIf you're seeing this:\n```\nnoop\n```\n\nThat means you are trying to rebase against a branch that is at an identical commit, or is *ahead* of your current branch. You can try:\n\n* making sure your main branch is where it should be\n* rebase against `HEAD~2` or earlier instead\n\n\u003ca name=\"merge-conflict\"\u003e\u003c/a\u003e\n#### There were conflicts\n\nIf you are unable to successfully complete the rebase, you may have to resolve conflicts.\n\nFirst run `git status` to see which files have conflicts in them:\n\n```sh\n(my-branch)$ git status\nOn branch my-branch\nChanges not staged for commit:\n  (use \"git add \u003cfile\u003e...\" to update what will be committed)\n  (use \"git checkout -- \u003cfile\u003e...\" to discard changes in working directory)\n\n  both modified:   README.md\n```\n\nIn this example, `README.md` has conflicts. Open that file and look for the following:\n\n```vim\n   \u003c\u003c\u003c\u003c\u003c\u003c\u003c HEAD\n   some code\n   =========\n   some code\n   \u003e\u003e\u003e\u003e\u003e\u003e\u003e new-commit\n```\n\nYou will need to resolve the differences between the code that was added in your new commit (in the example, everything from the middle line to `new-commit`) and your `HEAD`.\n\nIf you want to keep one branch's version of the code, you can use `--ours` or `--theirs`:\n\n```sh\n(main*)$ git checkout --ours README.md\n```\n\n- When *merging*, use `--ours` to keep changes from the local branch, or `--theirs` to keep changes from the other branch.\n- When *rebasing*, use `--theirs` to keep changes from the local branch, or `--ours` to keep changes from the other branch. For an explanation of this swap, see [this note in the Git documentation](https://git-scm.com/docs/git-rebase#Documentation/git-rebase.txt---merge).\n\nIf the merges are more complicated, you can use a visual diff editor:\n\n```sh\n(main*)$ git mergetool -t opendiff\n```\n\nAfter you have resolved all conflicts and tested your code, `git add` the files you have changed, and then continue the rebase with `git rebase --continue`\n\n```sh\n(my-branch)$ git add README.md\n(my-branch)$ git rebase --continue\n```\n\nIf after resolving all the conflicts you end up with an identical tree to what it was before the commit, you need to `git rebase --skip` instead.\n\nIf at any time you want to stop the entire rebase and go back to the original state of your branch, you can do so:\n\n```sh\n(my-branch)$ git rebase --abort\n```\n\n\u003ca name=\"stashing\"\u003e\u003c/a\u003e\n## Stash\n\n### Stash all edits\n\nTo stash all the edits in your working directory\n\n```sh\n$ git stash\n```\n\nIf you also want to stash untracked files, use `-u` option.\n\n```sh\n$ git stash -u\n```\n\n### Stash specific files\n\nTo stash only one file from your working directory\n\n```sh\n$ git stash push working-directory-path/filename.ext\n```\n\nTo stash multiple files from your working directory\n\n```sh\n$ git stash push working-directory-path/filename1.ext working-directory-path/filename2.ext\n```\n\n\u003ca name=\"stash-msg\"\u003e\u003c/a\u003e\n### Stash with message\n\n```sh\n$ git stash save \u003cmessage\u003e\n```\n\nor\n\n```sh\n$ git stash push -m \u003cmessage\u003e\n```\n\n\u003ca name=\"stash-apply-specific\"\u003e\u003c/a\u003e\n### Apply a specific stash from list\n\nFirst check your list of stashes with message using\n\n```sh\n$ git stash list\n```\n\nThen apply a specific stash from the list using\n\n```sh\n$ git stash apply \"stash@{n}\"\n```\n\nHere, 'n' indicates the position of the stash in the stack. The topmost stash will be position 0.\n\nFurthermore, using a time-based stash reference is also possible.\n\n```sh\n$ git stash apply \"stash@{2.hours.ago}\"\n```\n\n\u003ca name=\"stage-and-keep-unstaged\"\u003e\u003c/a\u003e\n### Stash while keeping unstaged edits\n\nYou can manually create a `stash commit`, and then use `git stash store`.\n\n```sh\n$ git stash create\n$ git stash store -m \u003cmessage\u003e CREATED_SHA1\n```\n\n## Finding\n\n### I want to find a string in any commit\n\nTo find a certain string which was introduced in any commit, you can use the following structure:\n\n```sh\n$ git log -S \"string to find\"\n```\n\nCommons parameters:\n\n* `--source` means to show the ref name given on the command line by which each commit was reached.\n\n* `--all` means to start from every branch.\n\n* `--reverse` prints in reverse order, it means that will show the first commit that made the change.\n\n\u003ca name=\"find-by-committer\"\u003e\u003c/a\u003e\n### I want to find by author/committer\n\nTo find all commits by author/committer you can use:\n\n```sh\n$ git log --author=\u003cname or email\u003e\n$ git log --committer=\u003cname or email\u003e\n```\n\nKeep in mind that author and committer are not the same. The `--author` is the person who originally wrote the code; on the other hand, the `--committer`, is the person who committed the code on behalf of the original author.\n\n### I want to list commits containing specific files\n\nTo find all commits containing a specific file you can use:\n\n```sh\n$ git log -- \u003cpath to file\u003e\n```\n\nYou would usually specify an exact path, but you may also use wild cards in the path and file name:\n\n```sh\n$ git log -- **/*.js\n```\n\nWhile using wildcards, it's useful to inform `--name-status` to see the list of committed files:\n\n```sh\n$ git log --name-status -- **/*.js\n```\n\n\u003ca name=\"view-commit-history-for-specific-function\"\u003e\u003c/a\u003e\n### I want to view the commit history for a specific function\n\nTo trace the evolution of a single function you can use:\n\n```sh\n$ git log -L :FunctionName:FilePath\n```\n\nNote that you can combine this with further `git log` options, like [revision ranges](https://git-scm.com/docs/gitrevisions) and [commit limits](https://git-scm.com/docs/git-log/#_commit_limiting).\n\n### Find a tag where a commit is referenced\n\nTo find all tags containing a specific commit:\n\n```sh\n$ git tag --contains \u003ccommitid\u003e\n```\n\n## Submodules\n\n\u003ca name=\"clone-submodules\"\u003e\u003c/a\u003e\n### Clone all submodules\n\n```sh\n$ git clone --recursive git://github.com/foo/bar.git\n```\n\nIf already cloned:\n\n```sh\n$ git submodule update --init --recursive\n```\n\n\u003ca name=\"delete-submodule\"\u003e\u003c/a\u003e\n### Remove a submodule\n\nCreating a submodule is pretty straight-forward, but deleting them less so. The commands you need are:\n\n```sh\n$ git submodule deinit submodulename\n$ git rm submodulename\n$ git rm --cached submodulename\n$ rm -rf .git/modules/submodulename\n```\n\n## Miscellaneous Objects\n\n### Copy a folder or file from one branch to another\n\n```sh\n$ git checkout \u003cbranch-you-want-the-directory-from\u003e -- \u003cfolder-name or file-name\u003e\n```\n\n### Restore a deleted file\n\nFirst find the commit when the file last existed:\n\n```sh\n$ git rev-list -n 1 HEAD -- filename\n```\n\nThen checkout that file:\n\n```\ngit checkout deletingcommitid^ -- filename\n```\n\n### Delete tag\n\n```sh\n$ git tag -d \u003ctag_name\u003e\n$ git push \u003cremote\u003e :refs/tags/\u003ctag_name\u003e\n```\n\n\u003ca name=\"recover-tag\"\u003e\u003c/a\u003e\n### Recover a deleted tag\n\nIf you want to recover a tag that was already deleted, you can do so by following these steps: First, you need to find the unreachable tag:\n\n```sh\n$ git fsck --unreachable | grep tag\n```\n\nMake a note of the tag's hash. Then, restore the deleted tag with following, making use of [`git update-ref`](https://git-scm.com/docs/git-update-ref):\n\n```sh\n$ git update-ref refs/tags/\u003ctag_name\u003e \u003chash\u003e\n```\n\nYour tag should now have been restored.\n\n### Deleted Patch\n\nIf someone has sent you a pull request on GitHub, but then deleted their original fork, you will be unable to clone their repository or to use `git am` as the [.diff, .patch](https://github.com/blog/967-github-secrets) URLs become unavailable. But you can checkout the PR itself using [GitHub's special refs](https://gist.github.com/piscisaureus/3342247). To fetch the content of PR#1 into a new branch called pr_1:\n\n```sh\n$ git fetch origin refs/pull/1/head:pr_1\nFrom github.com:foo/bar\n * [new ref]         refs/pull/1/head -\u003e pr_1\n```\n\n### Exporting a repository as a Zip file\n\n```sh\n$ git archive --format zip --output /full/path/to/zipfile.zip main\n```\n### Push a branch and a tag that have the same name\n\nIf there is a tag on a remote repository that has the same name as a branch you will get the following error when trying to push that branch with a standard `$ git push \u003cremote\u003e \u003cbranch\u003e` command.\n\n```sh\n$ git push origin \u003cbranch\u003e\nerror: dst refspec same matches more than one.\nerror: failed to push some refs to '\u003cgit server\u003e'\n```\n\nFix this by specifying you want to push the head reference.\n\n```sh\n$ git push origin refs/heads/\u003cbranch-name\u003e\n```\n\nIf you want to push a tag to a remote repository that has the same name as a branch, you can use a similar command.\n\n```sh\n$ git push origin refs/tags/\u003ctag-name\u003e\n```\n\n## Tracking Files\n\n\u003ca name=\"change-file-name-capitalization-without-changing-contents\"\u003e\u003c/a\u003e\n### I want to change a file name's capitalization, without changing the contents of the file\n\n```sh\n(main)$ git mv --force myfile MyFile\n```\n\n### I want to overwrite local files when doing a git pull\n\n```sh\n(main)$ git fetch --all\n(main)$ git reset --hard origin/main\n```\n\n\u003ca name=\"remove-from-git\"\u003e\u003c/a\u003e\n### I want to remove a file from Git but keep the file\n\n```sh\n(main)$ git rm --cached log.txt\n```\n\n### I want to revert a file to a specific revision\n\nAssuming the hash of the commit you want is c5f567:\n\n```sh\n(main)$ git checkout c5f567 -- file1/to/restore file2/to/restore\n```\n\nIf you want to revert to changes made just 1 commit before c5f567, pass the commit hash as c5f567~1:\n\n```sh\n(main)$ git checkout c5f567~1 -- file1/to/restore file2/to/restore\n```\n\n### I want to list changes of a specific file between commits or branches\n\nAssuming you want to compare last commit with file from commit c5f567:\n\n```sh\n$ git diff HEAD:path_to_file/file c5f567:path_to_file/file\n# or\n$ git diff HEAD c5f567 -- path_to_file/file\n```\n\nIf you are going to compare changes between the tips of the `main` and the `staging` branches:\n\n```sh\n$ git diff main:path_to_file/file staging:path_to_file/file\n# or\n$ git diff main staging -- path_to_file/file\n```\n\n### I want Git to ignore changes to a specific file\n\nThis works great for config templates or other files that require locally adding credentials that shouldn't be committed.\n\n```sh\n$ git update-index --assume-unchanged file-to-ignore\n```\n\nNote that this does *not* remove the file from source control - it is only ignored locally. To undo this and tell Git to notice changes again, this clears the ignore flag:\n\n```sh\n$ git update-index --no-assume-unchanged file-to-stop-ignoring\n```\n\n## Debugging with Git\n\nThe [git-bisect](https://git-scm.com/docs/git-bisect) command uses a binary search to find which commit in your Git history introduced a bug.\n\nSuppose you're on the `main` branch, and you want to find the commit that broke some feature. You start bisect:\n\n```sh\n$ git bisect start\n```\n\nThen you should specify which commit is bad, and which one is known to be good. Assuming that your *current* version is bad, and `v1.1.1` is good:\n\n```sh\n$ git bisect bad\n$ git bisect good v1.1.1\n```\n\nNow `git-bisect` selects a commit in the middle of the range that you specified, checks it out, and asks you whether it's good or bad. You should see something like:\n\n```sh\n$ Bisecting: 5 revision left to test after this (roughly 5 step)\n$ [c44abbbee29cb93d8499283101fe7c8d9d97f0fe] Commit message\n$ (c44abbb)$\n```\n\nYou will now check if this commit is good or bad. If it's good:\n\n```sh\n$ (c44abbb)$ git bisect good\n```\n\nand `git-bisect` will select another commit from the range for you. This process (selecting `good` or `bad`) will repeat until there are no more revisions left to inspect, and the command will finally print a description of the **first** bad commit.\n\n## Configuration\n\n### I want to add aliases for some Git commands\n\nOn OS X and Linux, your git configuration file is stored in ```~/.gitconfig```.  I've added some example aliases I use as shortcuts (and some of my common typos) in the ```[alias]``` section as shown below:\n\n```vim\n[alias]\n    a = add\n    amend = commit --amend\n    c = commit\n    ca = commit --amend\n    ci = commit -a\n    co = checkout\n    d = diff\n    dc = diff --changed\n    ds = diff --staged\n    extend = commit --amend -C HEAD\n    f = fetch\n    loll = log --graph --decorate --pretty=oneline --abbrev-commit\n    m = merge\n    one = log --pretty=oneline\n    outstanding = rebase -i @{u}\n    reword = commit --amend --only\n    s = status\n    unpushed = log @{u}\n    wc = whatchanged\n    wip = rebase -i @{u}\n    zap = fetch -p\n    day = log --reverse --no-merges --branches=* --date=local --since=midnight --author=\\\"$(git config --get user.name)\\\"\n    delete-merged-branches = \"!f() { git checkout --quiet main \u0026\u0026 git branch --merged | grep --invert-match '\\\\*' | xargs -n 1 git branch --delete; git checkout --quiet @{-1}; }; f\"\n```\n\n### I want to add an empty directory to my repository\n\nYou can’t! Git doesn’t support this, but there’s a hack. You can create a .gitignore file in the directory with the following contents:\n\n```\n # Ignore everything in this directory\n *\n # Except this file\n !.gitignore\n```\n\nAnother common convention is to make an empty file in the folder, titled .gitkeep.\n\n```sh\n$ mkdir mydir\n$ touch mydir/.gitkeep\n```\n\nYou can also name the file as just .keep , in which case the second line above would be ```touch mydir/.keep```\n\n### I want to cache a username and password for a repository\n\nYou might have a repository that requires authentication.  In which case you can cache a username and password so you don't have to enter it on every push and pull. Credential helper can do this for you.\n\n```sh\n$ git config --global credential.helper cache\n# Set git to use the credential memory cache\n```\n\n```sh\n$ git config --global credential.helper 'cache --timeout=3600'\n# Set the cache to timeout after 1 hour (setting is in seconds)\n```\n\nTo find a credential helper:\n\n```sh\n$ git help -a | grep credential\n# Shows you possible credential helpers\n```\n\nFor OS specific credential caching:\n\n```sh\n$ git config --global credential.helper osxkeychain\n# For OSX\n```\n\n```sh\n$ git config --global credential.helper manager\n# Git for Windows 2.7.3+\n```\n\n```sh\n$ git config --global credential.helper gnome-keyring\n# Ubuntu and other GNOME-based distros\n```\n\nMore credential helpers can likely be found for different distributions and operating systems.\n\n### I want to make Git ignore permissions and filemode changes\n\n```sh\n$ git config core.fileMode false\n```\n\nIf you want to make this the default behaviour for logged-in users, then use:\n\n```sh\n$ git config --global core.fileMode false\n```\n\n### I want to set a global user\n\nTo configure user information used across all local repositories, and to set a name that is identifiable for credit when review version history:\n\n```sh\n$ git config --global user.name “[firstname lastname]”\n```\n\nTo set an email address that will be associated with each history marker:\n\n```sh\ngit config --global user.email “[valid-email]”\n```\n\n## I've no idea what I did wrong\n\nSo, you're screwed - you `reset` something, or you merged the wrong branch, or you force pushed and now you can't find your commits. You know, at some point, you were doing alright, and you want to go back to some state you were at.\n\nThis is what `git reflog` is for. `reflog` keeps track of any changes to the tip of a branch, even if that tip isn't referenced by a branch or a tag. Basically, every time HEAD changes, a new entry is added to the reflog. This only works for local repositories, sadly, and it only tracks movements (not changes to a file that weren't recorded anywhere, for instance).\n\n```sh\n(main)$ git reflog\n0a2e358 HEAD@{0}: reset: moving to HEAD~2\n0254ea7 HEAD@{1}: checkout: moving from 2.2 to main\nc10f740 HEAD@{2}: checkout: moving from main to 2.2\n```\n\nThe reflog above shows a checkout from main to the 2.2 branch and back. From there, there's a hard reset to an older commit. The latest activity is represented at the top labeled `HEAD@{0}`.\n\nIf it turns out that you accidentally moved back, the reflog will contain the commit main pointed to (0254ea7) before you accidentally dropped 2 commits.\n\n```sh\n$ git reset --hard 0254ea7\n```\n\nUsing `git reset` it is then possible to change main back to the commit it was before. This provides a safety net in case history was accidentally changed.\n\n(copied and edited from [Source](https://www.atlassian.com/git/tutorials/rewriting-history/git-reflog)).\n\n\u003ca name=\"git-shortcuts\"\u003e\u003c/a\u003e\n## Git Shortcuts\n\n### Git Bash\n\nOnce you're comfortable with what the above commands are doing, you might want to create some shortcuts for Git Bash. This allows you to work a lot faster by doing complex tasks in really short commands.\n\n```sh\nalias sq=squash\n\nfunction squash() {\n    git rebase -i HEAD~$1\n}\n```\n\nCopy those commands to your .bashrc or .bash_profile.\n\n### PowerShell on Windows\n\nIf you are using PowerShell on Windows, you can also set up aliases and functions. Add these commands to your profile, whose path is defined in the `$profile` variable. Learn more at the [About Profiles](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_profiles) page on the Microsoft documentation site.\n\n```powershell\nSet-Alias sq Squash-Commits\n\nfunction Squash-Commits {\n  git rebase -i HEAD~$1\n}\n```\n\n# Other Resources\n\n## Books\n\n* [Learn Enough Git to Be Dangerous](https://www.learnenough.com/git-tutorial) - A book by Michael Hartl covering Git from basics\n* [Pro Git](https://git-scm.com/book/en/v2) - Scott Chacon and Ben Straub's excellent book about Git\n* [Git Internals](https://github.com/pluralsight/git-internals-pdf) - Scott Chacon's other excellent book about Git\n* [Nasa handbook](https://www.nasa.gov/wp-content/uploads/2018/09/nasa_systems_engineering_handbook_0.pdf)\n\n## Tutorials\n\n* [19 Git Tips For Everyday Use](https://www.alexkras.com/19-git-tips-for-everyday-use) - A list of useful Git one liners\n* [Atlassian's Git tutorial](https://www.atlassian.com/git/tutorials) Get Git right with tutorials from beginner to advanced.\n* [Getting solid at Git rebase vs. merge](https://medium.com/@porteneuve/getting-solid-at-git-rebase-vs-merge-4fa1a48c53aa)\n* [Git and GitHub Cartoons](https://allisonhorst.com/git-github) – Cartoons by Allison Horst that showcase how to use Git.\n* [Git Commands and Best Practices Cheat Sheet](https://zeroturnaround.com/rebellabs/git-commands-and-best-practices-cheat-sheet) - A Git cheat sheet in a blog post with more explanations\n* [Git from the inside out](https://codewords.recurse.com/issues/two/git-from-the-inside-out) - A tutorial that dives into Git's internals\n* [git-workflow](https://github.com/asmeurer/git-workflow) - [Aaron Meurer](https://github.com/asmeurer)'s howto on using Git to contribute to open source repositories\n* [GitHub as a workflow](https://hugogiraudel.com/2015/08/13/github-as-a-workflow/) - An interesting take on using GitHub as a workflow, particularly with empty PRs\n* [Githug](https://github.com/Gazler/githug) - A game to learn more common Git workflows\n* [Learn Git branching](https://learngitbranching.js.org/) An interactive web based branching/merging/rebasing tutorial\n* [learnGitBranching](https://github.com/pcottle/learnGitBranching) - An interactive git visualization to challenge and educate!\n\n## Scripts and Tools\n\n* [firstaidgit.io](http://firstaidgit.io/) A searchable selection of the most frequently asked Git questions\n* [git-extra-commands](https://github.com/unixorn/git-extra-commands) - a collection of useful extra Git scripts\n* [git-extras](https://github.com/tj/git-extras) - GIT utilities -- repo summary, repl, changelog population, author commit percentages and more\n* [git-fire](https://github.com/qw3rtman/git-fire) - git-fire is a Git plugin that helps in the event of an emergency by adding all current files, committing, and pushing to a new branch (to prevent merge conflicts).\n* [git-tips](https://github.com/git-tips/tips) - Small Git tips\n* [git-town](https://github.com/Originate/git-town) - Generic, high-level Git workflow support! http://www.git-town.com\n\n## GUI Clients\n* [GitKraken](https://www.gitkraken.com/) - The downright luxurious Git client, for Windows, Mac \u0026 Linux\n* [git-cola](https://git-cola.github.io/) - another Git client for Windows and OS X\n* [GitUp](https://github.com/git-up/GitUp) - A newish GUI that has some very opinionated ways of dealing with Git's complications\n* [gitx-dev](https://rowanj.github.io/gitx/) - another graphical Git client for OS X\n* [Sourcetree](https://www.sourcetreeapp.com/) - Simplicity meets power in a beautiful and free Git GUI. For Windows and Mac.\n* [Tower](https://www.git-tower.com/) - graphical Git client for OS X (paid)\n* [tig](https://jonas.github.io/tig/) - terminal text-mode interface for Git\n* [Magit](https://magit.vc/) - Interface to Git implemented as an Emacs package.\n* [GitExtensions](https://github.com/gitextensions/gitextensions) - a shell extension, a Visual Studio 2010-2015 plugin and a standalone Git repository tool.\n* [Fork](https://git-fork.com/) - a fast and friendly Git client for Mac (beta)\n* [gmaster](https://gmaster.io/) - a Git client for Windows that has 3-way merge, analyze refactors, semantic diff and merge (beta)\n* [gitk](https://git-scm.com/docs/gitk) - a Git client for linux to allow simple view of repo state.\n* [SublimeMerge](https://www.sublimemerge.com/) - Blazing fast, extensible client that provides 3-way merges, powerful search and syntax highlighting, in active development.\n","funding_links":[],"categories":["Others","Awesomes / Utils","miscellaneous","Git","Uncategorized","Coding","Misc","GitHub","Guides","Other Resources","Documentation / Guides / Exercises / Boilerplates","Tutorial","Development Guidelines","Programing resources","Foundations","others","其他","Learning Resources","Version Control","git","Linux生态圈Dev\u0026Ops工具与服务","Javascript","Diversos","Dev","Table des matières","Tools","工具集"],"sub_categories":["Uncategorized","Git","Other","Tools","**Learn git**","网络服务_其他","General","GIT","git help","Version Control","Git指南"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fk88hudson%2Fgit-flight-rules","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fk88hudson%2Fgit-flight-rules","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fk88hudson%2Fgit-flight-rules/lists"}