git-flight-rules/README.md

1471 lines
50 KiB
Markdown
Raw Normal View History

A few cleanups (#163) * Consolidate the name of the subsystem The name of the subsytem is considered to be 'Git' and not 'git'. There was inconsistency in the document by referring to the subsystem using both 'git' and 'Git'. Consolidate the usages to 'Git' which is generally considered to be the name of the subsystem. * Consolidate the name of GitHub They website is generally called 'GitHub' and not 'github'. * Quote the git commands correctly * Avoid fullstops in section names This is done for the sake of consistency. Most of the section names don't have a fullstop at the end. So, ... * Update the ToC The Table of Contents seems to have been out of date with the section titles. So, update the ToC with 'doctoc'. * Clarify that the changes are removed only for the previous commit * Showcase the flexibility of `git fetch -p` The example for that exhibits the way to 'prune' remote branches that were deleted upstream wasn't flexible as it relied on the command defaulting to the upstream of the current branch. This might lead the reader into overlooking the flexibility of the `git fetch`. Show that the 'upstream' can be mentioned in the command thus show casing the flexibility of `git fetch`. * Exemplify the safer version of branch deletion It's not good for newbies to start using 'force deletion' when they want to delete a branch as it might lead them to them into 'accidentally' deleting their branches often without merging them into other branches or pushing them to an upstream. So, exemplify the safer version of branch deletion (branch -d) and warn them about what `git branch -D` does. * Improve readability of a few phrases It's not required to use 'git' a lot as this a document about Git, after all. * Use HTTPS links for sites that serve using HTTPS * Clarify that rebasing just re-writes history Rebasing fast-forwards when the tip of the branch is a descendent of the tip of the upstream. In other cases it re-writes the history. This re-write is what actually leads the user to 'force' update the remote. So, clarify that a user has to force update only when the history is re-written regardless of whether the branch was fast-forwarded. * Attribute both the authors of the second edition of Pro Git * Try a different form of emphasizing text Capitalizing words seems to be over emphasizing words. Italicize the words, instead to see if works. * Mention what 'upstream' means for the sake of clarity * Simplify the way to create a remote branch from the local one The commands were needlessly complex by not considering the fact that the command defaults to HEAD when no branch is specified and changing configuration when it wasn't required. Simplify the commands to make readers more happy! * Remove a character cruft left over while editing This is an instance of a carelessly edited document getting into version control. ;) * Improve a sentence ... by, - expanding acronyms - quoting a command line parameter
2017-12-08 22:47:41 +06:00
# Flight rules for Git
🌍
*[English](README.md) ∙ [Русский](README_ru.md) ∙ [简体中文](README_zh-CN.md)*
2015-03-16 22:20:51 +06:00
2015-03-16 22:33:13 +06:00
#### What are "flight rules"?
A few cleanups (#163) * Consolidate the name of the subsystem The name of the subsytem is considered to be 'Git' and not 'git'. There was inconsistency in the document by referring to the subsystem using both 'git' and 'Git'. Consolidate the usages to 'Git' which is generally considered to be the name of the subsystem. * Consolidate the name of GitHub They website is generally called 'GitHub' and not 'github'. * Quote the git commands correctly * Avoid fullstops in section names This is done for the sake of consistency. Most of the section names don't have a fullstop at the end. So, ... * Update the ToC The Table of Contents seems to have been out of date with the section titles. So, update the ToC with 'doctoc'. * Clarify that the changes are removed only for the previous commit * Showcase the flexibility of `git fetch -p` The example for that exhibits the way to 'prune' remote branches that were deleted upstream wasn't flexible as it relied on the command defaulting to the upstream of the current branch. This might lead the reader into overlooking the flexibility of the `git fetch`. Show that the 'upstream' can be mentioned in the command thus show casing the flexibility of `git fetch`. * Exemplify the safer version of branch deletion It's not good for newbies to start using 'force deletion' when they want to delete a branch as it might lead them to them into 'accidentally' deleting their branches often without merging them into other branches or pushing them to an upstream. So, exemplify the safer version of branch deletion (branch -d) and warn them about what `git branch -D` does. * Improve readability of a few phrases It's not required to use 'git' a lot as this a document about Git, after all. * Use HTTPS links for sites that serve using HTTPS * Clarify that rebasing just re-writes history Rebasing fast-forwards when the tip of the branch is a descendent of the tip of the upstream. In other cases it re-writes the history. This re-write is what actually leads the user to 'force' update the remote. So, clarify that a user has to force update only when the history is re-written regardless of whether the branch was fast-forwarded. * Attribute both the authors of the second edition of Pro Git * Try a different form of emphasizing text Capitalizing words seems to be over emphasizing words. Italicize the words, instead to see if works. * Mention what 'upstream' means for the sake of clarity * Simplify the way to create a remote branch from the local one The commands were needlessly complex by not considering the fact that the command defaults to HEAD when no branch is specified and changing configuration when it wasn't required. Simplify the commands to make readers more happy! * Remove a character cruft left over while editing This is an instance of a carelessly edited document getting into version control. ;) * Improve a sentence ... by, - expanding acronyms - quoting a command line parameter
2017-12-08 22:47:41 +06:00
A [guide for astronauts](https://www.jsc.nasa.gov/news/columbia/fr_generic.pdf) (now, programmers using Git) about what to do when things go wrong.
2015-03-16 22:33:13 +06:00
> *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. [...]
> 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.
— Chris Hadfield, *An Astronaut's Guide to Life*.
#### Conventions for this document
2015-04-21 12:39:46 +06:00
For 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.
2015-03-16 22:33:13 +06:00
[![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&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
<!-- START doctoc generated TOC please keep comment here to allow auto update -->
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
**Table of Contents** *generated with [DocToc](https://github.com/thlorenz/doctoc)*
2015-09-24 21:13:56 +06:00
- [Editing Commits](#editing-commits)
- [What did I just commit?](#what-did-i-just-commit)
- [I wrote the wrong thing in a commit message](#i-wrote-the-wrong-thing-in-a-commit-message)
- [I committed with the wrong name and email configured](#i-committed-with-the-wrong-name-and-email-configured)
A few cleanups (#163) * Consolidate the name of the subsystem The name of the subsytem is considered to be 'Git' and not 'git'. There was inconsistency in the document by referring to the subsystem using both 'git' and 'Git'. Consolidate the usages to 'Git' which is generally considered to be the name of the subsystem. * Consolidate the name of GitHub They website is generally called 'GitHub' and not 'github'. * Quote the git commands correctly * Avoid fullstops in section names This is done for the sake of consistency. Most of the section names don't have a fullstop at the end. So, ... * Update the ToC The Table of Contents seems to have been out of date with the section titles. So, update the ToC with 'doctoc'. * Clarify that the changes are removed only for the previous commit * Showcase the flexibility of `git fetch -p` The example for that exhibits the way to 'prune' remote branches that were deleted upstream wasn't flexible as it relied on the command defaulting to the upstream of the current branch. This might lead the reader into overlooking the flexibility of the `git fetch`. Show that the 'upstream' can be mentioned in the command thus show casing the flexibility of `git fetch`. * Exemplify the safer version of branch deletion It's not good for newbies to start using 'force deletion' when they want to delete a branch as it might lead them to them into 'accidentally' deleting their branches often without merging them into other branches or pushing them to an upstream. So, exemplify the safer version of branch deletion (branch -d) and warn them about what `git branch -D` does. * Improve readability of a few phrases It's not required to use 'git' a lot as this a document about Git, after all. * Use HTTPS links for sites that serve using HTTPS * Clarify that rebasing just re-writes history Rebasing fast-forwards when the tip of the branch is a descendent of the tip of the upstream. In other cases it re-writes the history. This re-write is what actually leads the user to 'force' update the remote. So, clarify that a user has to force update only when the history is re-written regardless of whether the branch was fast-forwarded. * Attribute both the authors of the second edition of Pro Git * Try a different form of emphasizing text Capitalizing words seems to be over emphasizing words. Italicize the words, instead to see if works. * Mention what 'upstream' means for the sake of clarity * Simplify the way to create a remote branch from the local one The commands were needlessly complex by not considering the fact that the command defaults to HEAD when no branch is specified and changing configuration when it wasn't required. Simplify the commands to make readers more happy! * Remove a character cruft left over while editing This is an instance of a carelessly edited document getting into version control. ;) * Improve a sentence ... by, - expanding acronyms - quoting a command line parameter
2017-12-08 22:47:41 +06:00
- [I want to remove a file from the previous commit](#i-want-to-remove-a-file-from-the-previous-commit)
2015-09-24 21:13:56 +06:00
- [I want to delete or remove my last commit](#i-want-to-delete-or-remove-my-last-commit)
- [Delete/remove arbitrary commit](#deleteremove-arbitrary-commit)
- [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)
2015-09-24 21:13:56 +06:00
- [I accidentally did a hard reset, and I want my changes back](#i-accidentally-did-a-hard-reset-and-i-want-my-changes-back)
- [Staging](#staging)
- [I need to add staged changes to the previous commit](#i-need-to-add-staged-changes-to-the-previous-commit)
- [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)
- [I want to add changes in one file to two different commits](#i-want-to-add-changes-in-one-file-to-two-different-commits)
2016-12-14 00:03:57 +06:00
- [I want to stage my unstaged edits, and unstage my staged edits](#i-want-to-stage-my-unstaged-edits-and-unstage-my-staged-edits)
2015-09-24 21:13:56 +06:00
- [Unstaged Edits](#unstaged-edits)
- [I want to move my unstaged edits to a new branch](#i-want-to-move-my-unstaged-edits-to-a-new-branch)
- [I want to move my unstaged edits to a different, existing branch](#i-want-to-move-my-unstaged-edits-to-a-different-existing-branch)
- [I want to discard my local uncommitted changes (staged and unstaged)](#i-want-to-discard-my-local-uncommitted-changes-staged-and-unstaged)
- [I want to discard specific unstaged changes](#i-want-to-discard-specific-unstaged-changes)
- [I want to discard specific unstaged files](#i-want-to-discard-specific-unstaged-files)
- [I want to discard only my unstaged local changes](#i-want-to-discard-only-my-unstaged-local-changes)
A few cleanups (#163) * Consolidate the name of the subsystem The name of the subsytem is considered to be 'Git' and not 'git'. There was inconsistency in the document by referring to the subsystem using both 'git' and 'Git'. Consolidate the usages to 'Git' which is generally considered to be the name of the subsystem. * Consolidate the name of GitHub They website is generally called 'GitHub' and not 'github'. * Quote the git commands correctly * Avoid fullstops in section names This is done for the sake of consistency. Most of the section names don't have a fullstop at the end. So, ... * Update the ToC The Table of Contents seems to have been out of date with the section titles. So, update the ToC with 'doctoc'. * Clarify that the changes are removed only for the previous commit * Showcase the flexibility of `git fetch -p` The example for that exhibits the way to 'prune' remote branches that were deleted upstream wasn't flexible as it relied on the command defaulting to the upstream of the current branch. This might lead the reader into overlooking the flexibility of the `git fetch`. Show that the 'upstream' can be mentioned in the command thus show casing the flexibility of `git fetch`. * Exemplify the safer version of branch deletion It's not good for newbies to start using 'force deletion' when they want to delete a branch as it might lead them to them into 'accidentally' deleting their branches often without merging them into other branches or pushing them to an upstream. So, exemplify the safer version of branch deletion (branch -d) and warn them about what `git branch -D` does. * Improve readability of a few phrases It's not required to use 'git' a lot as this a document about Git, after all. * Use HTTPS links for sites that serve using HTTPS * Clarify that rebasing just re-writes history Rebasing fast-forwards when the tip of the branch is a descendent of the tip of the upstream. In other cases it re-writes the history. This re-write is what actually leads the user to 'force' update the remote. So, clarify that a user has to force update only when the history is re-written regardless of whether the branch was fast-forwarded. * Attribute both the authors of the second edition of Pro Git * Try a different form of emphasizing text Capitalizing words seems to be over emphasizing words. Italicize the words, instead to see if works. * Mention what 'upstream' means for the sake of clarity * Simplify the way to create a remote branch from the local one The commands were needlessly complex by not considering the fact that the command defaults to HEAD when no branch is specified and changing configuration when it wasn't required. Simplify the commands to make readers more happy! * Remove a character cruft left over while editing This is an instance of a carelessly edited document getting into version control. ;) * Improve a sentence ... by, - expanding acronyms - quoting a command line parameter
2017-12-08 22:47:41 +06:00
- [I want to discard all of my untracked files](#i-want-to-discard-all-of-my-untracked-files)
2015-09-24 21:13:56 +06:00
- [Branches](#branches)
2017-12-16 22:50:00 +06:00
- [I want to list all branches](#i-want-to-list-all-branches)
A few cleanups (#163) * Consolidate the name of the subsystem The name of the subsytem is considered to be 'Git' and not 'git'. There was inconsistency in the document by referring to the subsystem using both 'git' and 'Git'. Consolidate the usages to 'Git' which is generally considered to be the name of the subsystem. * Consolidate the name of GitHub They website is generally called 'GitHub' and not 'github'. * Quote the git commands correctly * Avoid fullstops in section names This is done for the sake of consistency. Most of the section names don't have a fullstop at the end. So, ... * Update the ToC The Table of Contents seems to have been out of date with the section titles. So, update the ToC with 'doctoc'. * Clarify that the changes are removed only for the previous commit * Showcase the flexibility of `git fetch -p` The example for that exhibits the way to 'prune' remote branches that were deleted upstream wasn't flexible as it relied on the command defaulting to the upstream of the current branch. This might lead the reader into overlooking the flexibility of the `git fetch`. Show that the 'upstream' can be mentioned in the command thus show casing the flexibility of `git fetch`. * Exemplify the safer version of branch deletion It's not good for newbies to start using 'force deletion' when they want to delete a branch as it might lead them to them into 'accidentally' deleting their branches often without merging them into other branches or pushing them to an upstream. So, exemplify the safer version of branch deletion (branch -d) and warn them about what `git branch -D` does. * Improve readability of a few phrases It's not required to use 'git' a lot as this a document about Git, after all. * Use HTTPS links for sites that serve using HTTPS * Clarify that rebasing just re-writes history Rebasing fast-forwards when the tip of the branch is a descendent of the tip of the upstream. In other cases it re-writes the history. This re-write is what actually leads the user to 'force' update the remote. So, clarify that a user has to force update only when the history is re-written regardless of whether the branch was fast-forwarded. * Attribute both the authors of the second edition of Pro Git * Try a different form of emphasizing text Capitalizing words seems to be over emphasizing words. Italicize the words, instead to see if works. * Mention what 'upstream' means for the sake of clarity * Simplify the way to create a remote branch from the local one The commands were needlessly complex by not considering the fact that the command defaults to HEAD when no branch is specified and changing configuration when it wasn't required. Simplify the commands to make readers more happy! * Remove a character cruft left over while editing This is an instance of a carelessly edited document getting into version control. ;) * Improve a sentence ... by, - expanding acronyms - quoting a command line parameter
2017-12-08 22:47:41 +06:00
- [Create a branch from a commit](#create-a-branch-from-a-commit)
2015-09-24 21:13:56 +06:00
- [I pulled from/into the wrong branch](#i-pulled-frominto-the-wrong-branch)
- [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)
- [I committed to master instead of a new branch](#i-committed-to-master-instead-of-a-new-branch)
- [I want to keep the whole file from another ref-ish](#i-want-to-keep-the-whole-file-from-another-ref-ish)
2015-09-24 21:13:56 +06:00
- [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)
- [I want to delete local branches that were deleted upstream](#i-want-to-delete-local-branches-that-were-deleted-upstream)
- [I accidentally deleted my branch](#i-accidentally-deleted-my-branch)
- [I want to delete a branch](#i-want-to-delete-a-branch)
- [I want to delete multiple branches](#i-want-to-delete-multiple-branches)
- [I want to rename a branch](#i-want-to-rename-a-branch)
2017-11-06 07:31:09 +06:00
- [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)
- [I want to create a new remote branch from current local one](#i-want-to-create-a-new-remote-branch-from-current-local-one)
- [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)
- [I want to set my HEAD to track the default remote branch](#i-want-to-set-my-head-to-track-the-default-remote-branch)
2015-09-24 21:13:56 +06:00
- [Rebasing and Merging](#rebasing-and-merging)
A few cleanups (#163) * Consolidate the name of the subsystem The name of the subsytem is considered to be 'Git' and not 'git'. There was inconsistency in the document by referring to the subsystem using both 'git' and 'Git'. Consolidate the usages to 'Git' which is generally considered to be the name of the subsystem. * Consolidate the name of GitHub They website is generally called 'GitHub' and not 'github'. * Quote the git commands correctly * Avoid fullstops in section names This is done for the sake of consistency. Most of the section names don't have a fullstop at the end. So, ... * Update the ToC The Table of Contents seems to have been out of date with the section titles. So, update the ToC with 'doctoc'. * Clarify that the changes are removed only for the previous commit * Showcase the flexibility of `git fetch -p` The example for that exhibits the way to 'prune' remote branches that were deleted upstream wasn't flexible as it relied on the command defaulting to the upstream of the current branch. This might lead the reader into overlooking the flexibility of the `git fetch`. Show that the 'upstream' can be mentioned in the command thus show casing the flexibility of `git fetch`. * Exemplify the safer version of branch deletion It's not good for newbies to start using 'force deletion' when they want to delete a branch as it might lead them to them into 'accidentally' deleting their branches often without merging them into other branches or pushing them to an upstream. So, exemplify the safer version of branch deletion (branch -d) and warn them about what `git branch -D` does. * Improve readability of a few phrases It's not required to use 'git' a lot as this a document about Git, after all. * Use HTTPS links for sites that serve using HTTPS * Clarify that rebasing just re-writes history Rebasing fast-forwards when the tip of the branch is a descendent of the tip of the upstream. In other cases it re-writes the history. This re-write is what actually leads the user to 'force' update the remote. So, clarify that a user has to force update only when the history is re-written regardless of whether the branch was fast-forwarded. * Attribute both the authors of the second edition of Pro Git * Try a different form of emphasizing text Capitalizing words seems to be over emphasizing words. Italicize the words, instead to see if works. * Mention what 'upstream' means for the sake of clarity * Simplify the way to create a remote branch from the local one The commands were needlessly complex by not considering the fact that the command defaults to HEAD when no branch is specified and changing configuration when it wasn't required. Simplify the commands to make readers more happy! * Remove a character cruft left over while editing This is an instance of a carelessly edited document getting into version control. ;) * Improve a sentence ... by, - expanding acronyms - quoting a command line parameter
2017-12-08 22:47:41 +06:00
- [I want to undo rebase/merge](#i-want-to-undo-rebasemerge)
- [I rebased, but I don't want to force push](#i-rebased-but-i-dont-want-to-force-push)
2015-09-24 21:13:56 +06:00
- [I need to combine commits](#i-need-to-combine-commits)
- [Safe merging strategy](#safe-merging-strategy)
- [I need to merge a branch into a single commit](#i-need-to-merge-a-branch-into-a-single-commit)
- [I want to combine only unpushed commits](#i-want-to-combine-only-unpushed-commits)
2017-11-28 09:02:58 +06:00
- [I need to abort the merge](#i-need-to-abort-the-merge)
2015-09-24 21:13:56 +06:00
- [Check if all commits on a branch are merged](#check-if-all-commits-on-a-branch-are-merged)
- [Possible issues with interactive rebases](#possible-issues-with-interactive-rebases)
- [The rebase editing screen says 'noop'](#the-rebase-editing-screen-says-noop)
- [There were conflicts](#there-were-conflicts)
A few cleanups (#163) * Consolidate the name of the subsystem The name of the subsytem is considered to be 'Git' and not 'git'. There was inconsistency in the document by referring to the subsystem using both 'git' and 'Git'. Consolidate the usages to 'Git' which is generally considered to be the name of the subsystem. * Consolidate the name of GitHub They website is generally called 'GitHub' and not 'github'. * Quote the git commands correctly * Avoid fullstops in section names This is done for the sake of consistency. Most of the section names don't have a fullstop at the end. So, ... * Update the ToC The Table of Contents seems to have been out of date with the section titles. So, update the ToC with 'doctoc'. * Clarify that the changes are removed only for the previous commit * Showcase the flexibility of `git fetch -p` The example for that exhibits the way to 'prune' remote branches that were deleted upstream wasn't flexible as it relied on the command defaulting to the upstream of the current branch. This might lead the reader into overlooking the flexibility of the `git fetch`. Show that the 'upstream' can be mentioned in the command thus show casing the flexibility of `git fetch`. * Exemplify the safer version of branch deletion It's not good for newbies to start using 'force deletion' when they want to delete a branch as it might lead them to them into 'accidentally' deleting their branches often without merging them into other branches or pushing them to an upstream. So, exemplify the safer version of branch deletion (branch -d) and warn them about what `git branch -D` does. * Improve readability of a few phrases It's not required to use 'git' a lot as this a document about Git, after all. * Use HTTPS links for sites that serve using HTTPS * Clarify that rebasing just re-writes history Rebasing fast-forwards when the tip of the branch is a descendent of the tip of the upstream. In other cases it re-writes the history. This re-write is what actually leads the user to 'force' update the remote. So, clarify that a user has to force update only when the history is re-written regardless of whether the branch was fast-forwarded. * Attribute both the authors of the second edition of Pro Git * Try a different form of emphasizing text Capitalizing words seems to be over emphasizing words. Italicize the words, instead to see if works. * Mention what 'upstream' means for the sake of clarity * Simplify the way to create a remote branch from the local one The commands were needlessly complex by not considering the fact that the command defaults to HEAD when no branch is specified and changing configuration when it wasn't required. Simplify the commands to make readers more happy! * Remove a character cruft left over while editing This is an instance of a carelessly edited document getting into version control. ;) * Improve a sentence ... by, - expanding acronyms - quoting a command line parameter
2017-12-08 22:47:41 +06:00
- [Stash](#stash)
- [Stash all edits](#stash-all-edits)
- [Stash specific files](#stash-specific-files)
A few cleanups (#163) * Consolidate the name of the subsystem The name of the subsytem is considered to be 'Git' and not 'git'. There was inconsistency in the document by referring to the subsystem using both 'git' and 'Git'. Consolidate the usages to 'Git' which is generally considered to be the name of the subsystem. * Consolidate the name of GitHub They website is generally called 'GitHub' and not 'github'. * Quote the git commands correctly * Avoid fullstops in section names This is done for the sake of consistency. Most of the section names don't have a fullstop at the end. So, ... * Update the ToC The Table of Contents seems to have been out of date with the section titles. So, update the ToC with 'doctoc'. * Clarify that the changes are removed only for the previous commit * Showcase the flexibility of `git fetch -p` The example for that exhibits the way to 'prune' remote branches that were deleted upstream wasn't flexible as it relied on the command defaulting to the upstream of the current branch. This might lead the reader into overlooking the flexibility of the `git fetch`. Show that the 'upstream' can be mentioned in the command thus show casing the flexibility of `git fetch`. * Exemplify the safer version of branch deletion It's not good for newbies to start using 'force deletion' when they want to delete a branch as it might lead them to them into 'accidentally' deleting their branches often without merging them into other branches or pushing them to an upstream. So, exemplify the safer version of branch deletion (branch -d) and warn them about what `git branch -D` does. * Improve readability of a few phrases It's not required to use 'git' a lot as this a document about Git, after all. * Use HTTPS links for sites that serve using HTTPS * Clarify that rebasing just re-writes history Rebasing fast-forwards when the tip of the branch is a descendent of the tip of the upstream. In other cases it re-writes the history. This re-write is what actually leads the user to 'force' update the remote. So, clarify that a user has to force update only when the history is re-written regardless of whether the branch was fast-forwarded. * Attribute both the authors of the second edition of Pro Git * Try a different form of emphasizing text Capitalizing words seems to be over emphasizing words. Italicize the words, instead to see if works. * Mention what 'upstream' means for the sake of clarity * Simplify the way to create a remote branch from the local one The commands were needlessly complex by not considering the fact that the command defaults to HEAD when no branch is specified and changing configuration when it wasn't required. Simplify the commands to make readers more happy! * Remove a character cruft left over while editing This is an instance of a carelessly edited document getting into version control. ;) * Improve a sentence ... by, - expanding acronyms - quoting a command line parameter
2017-12-08 22:47:41 +06:00
- [Stash with message](#stash-with-message)
- [Apply a specific stash from list](#apply-a-specific-stash-from-list)
- [Finding](#finding)
- [I want to find a string in any commit](#i-want-to-find-a-string-in-any-commit)
- [I want to find by author/committer](#i-want-to-find-by-authorcommitter)
- [I want to list commits containing specific files](#i-want-to-list-commits-containing-specific-files)
- [Find a tag where a commit is referenced](#find-a-tag-where-a-commit-is-referenced)
- [Submodules](#submodules)
2015-09-24 21:13:56 +06:00
- [Clone all submodules](#clone-all-submodules)
- [Remove a submodule](#remove-a-submodule)
- [Miscellaneous Objects](#miscellaneous-objects)
- [Restore a deleted file](#restore-a-deleted-file)
- [Delete tag](#delete-tag)
- [Recover a deleted tag](#recover-a-deleted-tag)
2015-09-24 21:13:56 +06:00
- [Deleted Patch](#deleted-patch)
- [Exporting a repository as a Zip file](#exporting-a-repository-as-a-zip-file)
2015-09-24 21:13:56 +06:00
- [Tracking Files](#tracking-files)
A few cleanups (#163) * Consolidate the name of the subsystem The name of the subsytem is considered to be 'Git' and not 'git'. There was inconsistency in the document by referring to the subsystem using both 'git' and 'Git'. Consolidate the usages to 'Git' which is generally considered to be the name of the subsystem. * Consolidate the name of GitHub They website is generally called 'GitHub' and not 'github'. * Quote the git commands correctly * Avoid fullstops in section names This is done for the sake of consistency. Most of the section names don't have a fullstop at the end. So, ... * Update the ToC The Table of Contents seems to have been out of date with the section titles. So, update the ToC with 'doctoc'. * Clarify that the changes are removed only for the previous commit * Showcase the flexibility of `git fetch -p` The example for that exhibits the way to 'prune' remote branches that were deleted upstream wasn't flexible as it relied on the command defaulting to the upstream of the current branch. This might lead the reader into overlooking the flexibility of the `git fetch`. Show that the 'upstream' can be mentioned in the command thus show casing the flexibility of `git fetch`. * Exemplify the safer version of branch deletion It's not good for newbies to start using 'force deletion' when they want to delete a branch as it might lead them to them into 'accidentally' deleting their branches often without merging them into other branches or pushing them to an upstream. So, exemplify the safer version of branch deletion (branch -d) and warn them about what `git branch -D` does. * Improve readability of a few phrases It's not required to use 'git' a lot as this a document about Git, after all. * Use HTTPS links for sites that serve using HTTPS * Clarify that rebasing just re-writes history Rebasing fast-forwards when the tip of the branch is a descendent of the tip of the upstream. In other cases it re-writes the history. This re-write is what actually leads the user to 'force' update the remote. So, clarify that a user has to force update only when the history is re-written regardless of whether the branch was fast-forwarded. * Attribute both the authors of the second edition of Pro Git * Try a different form of emphasizing text Capitalizing words seems to be over emphasizing words. Italicize the words, instead to see if works. * Mention what 'upstream' means for the sake of clarity * Simplify the way to create a remote branch from the local one The commands were needlessly complex by not considering the fact that the command defaults to HEAD when no branch is specified and changing configuration when it wasn't required. Simplify the commands to make readers more happy! * Remove a character cruft left over while editing This is an instance of a carelessly edited document getting into version control. ;) * Improve a sentence ... by, - expanding acronyms - quoting a command line parameter
2017-12-08 22:47:41 +06:00
- [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)
- [I want to overwrite local files when doing a git pull](#i-want-to-overwrite-local-files-when-doing-a-git-pull)
A few cleanups (#163) * Consolidate the name of the subsystem The name of the subsytem is considered to be 'Git' and not 'git'. There was inconsistency in the document by referring to the subsystem using both 'git' and 'Git'. Consolidate the usages to 'Git' which is generally considered to be the name of the subsystem. * Consolidate the name of GitHub They website is generally called 'GitHub' and not 'github'. * Quote the git commands correctly * Avoid fullstops in section names This is done for the sake of consistency. Most of the section names don't have a fullstop at the end. So, ... * Update the ToC The Table of Contents seems to have been out of date with the section titles. So, update the ToC with 'doctoc'. * Clarify that the changes are removed only for the previous commit * Showcase the flexibility of `git fetch -p` The example for that exhibits the way to 'prune' remote branches that were deleted upstream wasn't flexible as it relied on the command defaulting to the upstream of the current branch. This might lead the reader into overlooking the flexibility of the `git fetch`. Show that the 'upstream' can be mentioned in the command thus show casing the flexibility of `git fetch`. * Exemplify the safer version of branch deletion It's not good for newbies to start using 'force deletion' when they want to delete a branch as it might lead them to them into 'accidentally' deleting their branches often without merging them into other branches or pushing them to an upstream. So, exemplify the safer version of branch deletion (branch -d) and warn them about what `git branch -D` does. * Improve readability of a few phrases It's not required to use 'git' a lot as this a document about Git, after all. * Use HTTPS links for sites that serve using HTTPS * Clarify that rebasing just re-writes history Rebasing fast-forwards when the tip of the branch is a descendent of the tip of the upstream. In other cases it re-writes the history. This re-write is what actually leads the user to 'force' update the remote. So, clarify that a user has to force update only when the history is re-written regardless of whether the branch was fast-forwarded. * Attribute both the authors of the second edition of Pro Git * Try a different form of emphasizing text Capitalizing words seems to be over emphasizing words. Italicize the words, instead to see if works. * Mention what 'upstream' means for the sake of clarity * Simplify the way to create a remote branch from the local one The commands were needlessly complex by not considering the fact that the command defaults to HEAD when no branch is specified and changing configuration when it wasn't required. Simplify the commands to make readers more happy! * Remove a character cruft left over while editing This is an instance of a carelessly edited document getting into version control. ;) * Improve a sentence ... by, - expanding acronyms - quoting a command line parameter
2017-12-08 22:47:41 +06:00
- [I want to remove a file from Git but keep the file](#i-want-to-remove-a-file-from-git-but-keep-the-file)
- [I want to revert a file to a specific revision](#i-want-to-revert-a-file-to-a-specific-revision)
2015-09-24 21:13:56 +06:00
- [Configuration](#configuration)
A few cleanups (#163) * Consolidate the name of the subsystem The name of the subsytem is considered to be 'Git' and not 'git'. There was inconsistency in the document by referring to the subsystem using both 'git' and 'Git'. Consolidate the usages to 'Git' which is generally considered to be the name of the subsystem. * Consolidate the name of GitHub They website is generally called 'GitHub' and not 'github'. * Quote the git commands correctly * Avoid fullstops in section names This is done for the sake of consistency. Most of the section names don't have a fullstop at the end. So, ... * Update the ToC The Table of Contents seems to have been out of date with the section titles. So, update the ToC with 'doctoc'. * Clarify that the changes are removed only for the previous commit * Showcase the flexibility of `git fetch -p` The example for that exhibits the way to 'prune' remote branches that were deleted upstream wasn't flexible as it relied on the command defaulting to the upstream of the current branch. This might lead the reader into overlooking the flexibility of the `git fetch`. Show that the 'upstream' can be mentioned in the command thus show casing the flexibility of `git fetch`. * Exemplify the safer version of branch deletion It's not good for newbies to start using 'force deletion' when they want to delete a branch as it might lead them to them into 'accidentally' deleting their branches often without merging them into other branches or pushing them to an upstream. So, exemplify the safer version of branch deletion (branch -d) and warn them about what `git branch -D` does. * Improve readability of a few phrases It's not required to use 'git' a lot as this a document about Git, after all. * Use HTTPS links for sites that serve using HTTPS * Clarify that rebasing just re-writes history Rebasing fast-forwards when the tip of the branch is a descendent of the tip of the upstream. In other cases it re-writes the history. This re-write is what actually leads the user to 'force' update the remote. So, clarify that a user has to force update only when the history is re-written regardless of whether the branch was fast-forwarded. * Attribute both the authors of the second edition of Pro Git * Try a different form of emphasizing text Capitalizing words seems to be over emphasizing words. Italicize the words, instead to see if works. * Mention what 'upstream' means for the sake of clarity * Simplify the way to create a remote branch from the local one The commands were needlessly complex by not considering the fact that the command defaults to HEAD when no branch is specified and changing configuration when it wasn't required. Simplify the commands to make readers more happy! * Remove a character cruft left over while editing This is an instance of a carelessly edited document getting into version control. ;) * Improve a sentence ... by, - expanding acronyms - quoting a command line parameter
2017-12-08 22:47:41 +06:00
- [I want to add aliases for some Git commands](#i-want-to-add-aliases-for-some-git-commands)
- [I want to add an empty directory to my repository](#i-want-to-add-an-empty-directory-to-my-repository)
2016-08-02 07:08:05 +06:00
- [I want to cache a username and password for a repository](#i-want-to-cache-a-username-and-password-for-a-repository)
- [I want to make Git ignore permissions and filemode changes](#i-want-to-make-git-ignore-permissions-and-filemode-changes)
- [I've no idea what I did wrong](#ive-no-idea-what-i-did-wrong)
- [Other Resources](#other-resources)
2015-05-22 08:02:08 +06:00
- [Books](#books)
- [Tutorials](#tutorials)
2015-09-24 21:13:56 +06:00
- [Scripts and Tools](#scripts-and-tools)
2015-05-25 11:35:52 +06:00
- [GUI Clients](#gui-clients)
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
2015-09-24 21:13:56 +06:00
## Editing Commits
<a name="diff-last"></a>
### What did I just commit?
Let'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:
2015-09-24 21:13:56 +06:00
```sh
(master)$ git show
2015-09-24 21:13:56 +06:00
```
Or
```sh
$ git log -n1 -p
```
If you want to see a file at a specific commit, you can also do this (where `<commitid>` is the commit you're interested in):
```sh
$ git show <commitid>:filename
```
2015-09-24 21:13:56 +06:00
<a name="#i-wrote-the-wrong-thing-in-a-commit-message"></a>
### I wrote the wrong thing in a commit message
2015-03-08 00:39:30 +06:00
If you wrote the wrong thing and the commit has not yet been pushed, you can do the following to change the commit message:
2015-03-08 00:39:30 +06:00
```sh
$ git commit --amend
2015-04-21 12:39:46 +06:00
```
2017-11-07 07:09:42 +06:00
This will open your default text editor, where you can edit the message. On the other hand, you can do this all in one command:
```sh
$ git commit --amend -m 'xxxxxxx'
```
If you have already pushed the message, you can amend the commit and force push, but this is not recommended.
2015-09-24 21:13:56 +06:00
<a name="commit-wrong-author"></a>
### I committed with the wrong name and email configured
If it's a single commit, amend it
2014-07-23 02:33:36 +06:00
```sh
$ git commit --amend --no-edit --author "New Authorname <authoremail@mydomain.com>"
```
An alternative is to correctly configure your author settings in `git config --global author.(name|email)` and then use
```sh
$ git commit --amend --reset-author --no-edit
2015-09-24 21:13:56 +06:00
```
A few cleanups (#163) * Consolidate the name of the subsystem The name of the subsytem is considered to be 'Git' and not 'git'. There was inconsistency in the document by referring to the subsystem using both 'git' and 'Git'. Consolidate the usages to 'Git' which is generally considered to be the name of the subsystem. * Consolidate the name of GitHub They website is generally called 'GitHub' and not 'github'. * Quote the git commands correctly * Avoid fullstops in section names This is done for the sake of consistency. Most of the section names don't have a fullstop at the end. So, ... * Update the ToC The Table of Contents seems to have been out of date with the section titles. So, update the ToC with 'doctoc'. * Clarify that the changes are removed only for the previous commit * Showcase the flexibility of `git fetch -p` The example for that exhibits the way to 'prune' remote branches that were deleted upstream wasn't flexible as it relied on the command defaulting to the upstream of the current branch. This might lead the reader into overlooking the flexibility of the `git fetch`. Show that the 'upstream' can be mentioned in the command thus show casing the flexibility of `git fetch`. * Exemplify the safer version of branch deletion It's not good for newbies to start using 'force deletion' when they want to delete a branch as it might lead them to them into 'accidentally' deleting their branches often without merging them into other branches or pushing them to an upstream. So, exemplify the safer version of branch deletion (branch -d) and warn them about what `git branch -D` does. * Improve readability of a few phrases It's not required to use 'git' a lot as this a document about Git, after all. * Use HTTPS links for sites that serve using HTTPS * Clarify that rebasing just re-writes history Rebasing fast-forwards when the tip of the branch is a descendent of the tip of the upstream. In other cases it re-writes the history. This re-write is what actually leads the user to 'force' update the remote. So, clarify that a user has to force update only when the history is re-written regardless of whether the branch was fast-forwarded. * Attribute both the authors of the second edition of Pro Git * Try a different form of emphasizing text Capitalizing words seems to be over emphasizing words. Italicize the words, instead to see if works. * Mention what 'upstream' means for the sake of clarity * Simplify the way to create a remote branch from the local one The commands were needlessly complex by not considering the fact that the command defaults to HEAD when no branch is specified and changing configuration when it wasn't required. Simplify the commands to make readers more happy! * Remove a character cruft left over while editing This is an instance of a carelessly edited document getting into version control. ;) * Improve a sentence ... by, - expanding acronyms - quoting a command line parameter
2017-12-08 22:47:41 +06:00
If you need to change all of history, see the man page for `git filter-branch`.
2015-09-24 21:13:56 +06:00
A few cleanups (#163) * Consolidate the name of the subsystem The name of the subsytem is considered to be 'Git' and not 'git'. There was inconsistency in the document by referring to the subsystem using both 'git' and 'Git'. Consolidate the usages to 'Git' which is generally considered to be the name of the subsystem. * Consolidate the name of GitHub They website is generally called 'GitHub' and not 'github'. * Quote the git commands correctly * Avoid fullstops in section names This is done for the sake of consistency. Most of the section names don't have a fullstop at the end. So, ... * Update the ToC The Table of Contents seems to have been out of date with the section titles. So, update the ToC with 'doctoc'. * Clarify that the changes are removed only for the previous commit * Showcase the flexibility of `git fetch -p` The example for that exhibits the way to 'prune' remote branches that were deleted upstream wasn't flexible as it relied on the command defaulting to the upstream of the current branch. This might lead the reader into overlooking the flexibility of the `git fetch`. Show that the 'upstream' can be mentioned in the command thus show casing the flexibility of `git fetch`. * Exemplify the safer version of branch deletion It's not good for newbies to start using 'force deletion' when they want to delete a branch as it might lead them to them into 'accidentally' deleting their branches often without merging them into other branches or pushing them to an upstream. So, exemplify the safer version of branch deletion (branch -d) and warn them about what `git branch -D` does. * Improve readability of a few phrases It's not required to use 'git' a lot as this a document about Git, after all. * Use HTTPS links for sites that serve using HTTPS * Clarify that rebasing just re-writes history Rebasing fast-forwards when the tip of the branch is a descendent of the tip of the upstream. In other cases it re-writes the history. This re-write is what actually leads the user to 'force' update the remote. So, clarify that a user has to force update only when the history is re-written regardless of whether the branch was fast-forwarded. * Attribute both the authors of the second edition of Pro Git * Try a different form of emphasizing text Capitalizing words seems to be over emphasizing words. Italicize the words, instead to see if works. * Mention what 'upstream' means for the sake of clarity * Simplify the way to create a remote branch from the local one The commands were needlessly complex by not considering the fact that the command defaults to HEAD when no branch is specified and changing configuration when it wasn't required. Simplify the commands to make readers more happy! * Remove a character cruft left over while editing This is an instance of a carelessly edited document getting into version control. ;) * Improve a sentence ... by, - expanding acronyms - quoting a command line parameter
2017-12-08 22:47:41 +06:00
<a href="#i-want-to-remove-a-file-from-the-previous-commit"></a>
### I want to remove a file from the previous commit
2015-09-24 21:13:56 +06:00
A few cleanups (#163) * Consolidate the name of the subsystem The name of the subsytem is considered to be 'Git' and not 'git'. There was inconsistency in the document by referring to the subsystem using both 'git' and 'Git'. Consolidate the usages to 'Git' which is generally considered to be the name of the subsystem. * Consolidate the name of GitHub They website is generally called 'GitHub' and not 'github'. * Quote the git commands correctly * Avoid fullstops in section names This is done for the sake of consistency. Most of the section names don't have a fullstop at the end. So, ... * Update the ToC The Table of Contents seems to have been out of date with the section titles. So, update the ToC with 'doctoc'. * Clarify that the changes are removed only for the previous commit * Showcase the flexibility of `git fetch -p` The example for that exhibits the way to 'prune' remote branches that were deleted upstream wasn't flexible as it relied on the command defaulting to the upstream of the current branch. This might lead the reader into overlooking the flexibility of the `git fetch`. Show that the 'upstream' can be mentioned in the command thus show casing the flexibility of `git fetch`. * Exemplify the safer version of branch deletion It's not good for newbies to start using 'force deletion' when they want to delete a branch as it might lead them to them into 'accidentally' deleting their branches often without merging them into other branches or pushing them to an upstream. So, exemplify the safer version of branch deletion (branch -d) and warn them about what `git branch -D` does. * Improve readability of a few phrases It's not required to use 'git' a lot as this a document about Git, after all. * Use HTTPS links for sites that serve using HTTPS * Clarify that rebasing just re-writes history Rebasing fast-forwards when the tip of the branch is a descendent of the tip of the upstream. In other cases it re-writes the history. This re-write is what actually leads the user to 'force' update the remote. So, clarify that a user has to force update only when the history is re-written regardless of whether the branch was fast-forwarded. * Attribute both the authors of the second edition of Pro Git * Try a different form of emphasizing text Capitalizing words seems to be over emphasizing words. Italicize the words, instead to see if works. * Mention what 'upstream' means for the sake of clarity * Simplify the way to create a remote branch from the local one The commands were needlessly complex by not considering the fact that the command defaults to HEAD when no branch is specified and changing configuration when it wasn't required. Simplify the commands to make readers more happy! * Remove a character cruft left over while editing This is an instance of a carelessly edited document getting into version control. ;) * Improve a sentence ... by, - expanding acronyms - quoting a command line parameter
2017-12-08 22:47:41 +06:00
In order to remove changes for a file from the previous commit, do the following:
2015-09-24 21:13:56 +06:00
```sh
$ git checkout HEAD^ myfile
$ git add myfile
$ git commit --amend --no-edit
2015-09-24 21:13:56 +06:00
```
In case the file was newly added to the commit and you want to remove it (from Git alone), do:
```sh
$ git rm --cached myfile
$ git commit --amend --no-edit
```
2017-11-12 21:31:50 +06:00
This 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.
2015-09-24 21:13:56 +06:00
<a name="delete-pushed-commit"></a>
### I want to delete or remove my last commit
2016-11-12 21:10:29 +06:00
If 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.
2015-09-24 21:13:56 +06:00
```sh
$ git reset HEAD^ --hard
$ git push --force-with-lease [remote] [branch]
2015-09-24 21:13:56 +06:00
```
If you haven't pushed, to reset Git to the state it was in before you made your last commit (while keeping your staged changes):
```
(my-branch*)$ git reset --soft HEAD@{1}
```
2017-11-11 03:04:58 +06:00
This 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).
2015-09-24 21:13:56 +06:00
<a name="delete-any-commit"></a>
### Delete/remove arbitrary commit
The same warning applies as above. Never do this if possible.
2015-09-24 21:13:56 +06:00
```sh
$ git rebase --onto SHA1_OF_BAD_COMMIT^ SHA1_OF_BAD_COMMIT
$ git push --force-with-lease [remote] [branch]
2014-07-23 02:33:36 +06:00
```
2016-11-12 21:10:29 +06:00
Or do an [interactive rebase](#interactive-rebase) and remove the line(s) corresponding to commit(s) you want to see removed.
2015-09-24 21:13:56 +06:00
<a name="#force-push"></a>
### I tried to push my amended commit to a remote, but I got an error message
```sh
To https://github.com/yourusername/repo.git
! [rejected] mybranch -> mybranch (non-fast-forward)
error: failed to push some refs to 'https://github.com/tanay1337/webmaker.org.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
```
Note 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 &ndash; *always* make sure you specify a branch!
```sh
(my-branch)$ git push origin mybranch --force-with-lease
```
2017-11-19 23:56:42 +06:00
In 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.
2014-08-20 00:48:43 +06:00
If 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.
2015-05-04 09:42:00 +06:00
2015-09-24 21:13:56 +06:00
<a href="undo-git-reset-hard"></a>
### I accidentally did a hard reset, and I want my changes back
If 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.
2015-05-04 09:42:00 +06:00
Note: 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`.)
2015-05-04 09:42:00 +06:00
```sh
2015-09-24 21:13:56 +06:00
(master)$ git reflog
2015-05-04 09:42:00 +06:00
```
2015-09-24 21:13:56 +06:00
You'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:
2014-07-23 02:33:36 +06:00
```sh
2015-09-24 21:13:56 +06:00
(master)$ git reset --hard SHA1234
2014-07-23 02:33:36 +06:00
```
2015-09-24 21:13:56 +06:00
And you should be good to go.
2014-07-23 02:33:36 +06:00
2015-09-24 21:13:56 +06:00
## Staging
2014-07-23 02:33:36 +06:00
2015-09-24 21:13:56 +06:00
<a href="#i-need-to-add-staged-changes-to-the-previous-commit"></a>
### I need to add staged changes to the previous commit
2014-07-23 02:33:36 +06:00
2015-09-24 21:13:56 +06:00
```sh
(my-branch*)$ git commit --amend
2014-07-23 02:33:36 +06:00
```
2015-09-24 21:13:56 +06:00
<a name="commit-partial-new-file"></a>
### I want to stage part of a new file, but not the whole file
2014-07-23 02:33:36 +06:00
2015-09-24 21:13:56 +06:00
Normally, if you want to stage part of a file, you run this:
2014-07-23 02:33:36 +06:00
2015-09-24 21:13:56 +06:00
```sh
$ git add --patch filename.x
2014-07-23 02:33:36 +06:00
```
2015-09-24 21:13:56 +06:00
`-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:
2014-07-23 02:33:36 +06:00
2015-09-24 21:13:56 +06:00
```sh
$ git add -N filename.x
2014-07-23 02:33:36 +06:00
```
Then, you will need to use the `e` option to manually choose which lines to add. Running `git diff --cached` or
`git diff --staged` will show you which lines you have staged compared to which are still saved locally.
2014-07-23 02:33:36 +06:00
2015-09-24 21:13:56 +06:00
<a href="stage-in-two-commits"></a>
### I want to add changes in one file to two different commits
2014-07-23 02:33:36 +06:00
2015-09-24 21:13:56 +06:00
`git add` will add the entire file to a commit. `git add -p` will allow to interactively select which changes you want to add.
2014-07-23 02:33:36 +06:00
2016-12-14 00:03:57 +06:00
<a href="unstaging-edits-and-staging-the-unstaged"></a>
### I want to stage my unstaged edits, and unstage my staged edits
This is tricky. The best I figure is that you should stash your unstaged edits. Then, reset. After that, pop your stashed edits back, and add them.
```sh
$ git stash -k
$ git reset --hard
$ git stash pop
$ git add -A
```
2015-09-24 21:13:56 +06:00
## Unstaged Edits
2015-09-24 21:13:56 +06:00
<a href="move-unstaged-edits-to-new-branch"></a>
### I want to move my unstaged edits to a new branch
2015-03-17 01:15:05 +06:00
2014-07-30 00:53:00 +06:00
```sh
2015-09-24 21:13:56 +06:00
$ git checkout -b my-branch
2014-07-30 00:53:00 +06:00
```
2015-09-24 21:13:56 +06:00
<a href="move-unstaged-edits-to-old-branch"></a>
### I want to move my unstaged edits to a different, existing branch
2014-07-30 00:53:00 +06:00
```sh
2015-09-24 21:13:56 +06:00
$ git stash
$ git checkout my-branch
$ git stash pop
2014-07-30 00:53:00 +06:00
```
2014-07-23 02:33:36 +06:00
2015-09-24 21:13:56 +06:00
<a href="i-want-to-discard-my-local-uncommitted-changes"></a>
### I want to discard my local uncommitted changes (staged and unstaged)
If you want to discard all your local staged and unstaged changes, you can do this:
```sh
(my-branch)$ git reset --hard
# or
(master)$ git checkout -f
```
A few cleanups (#163) * Consolidate the name of the subsystem The name of the subsytem is considered to be 'Git' and not 'git'. There was inconsistency in the document by referring to the subsystem using both 'git' and 'Git'. Consolidate the usages to 'Git' which is generally considered to be the name of the subsystem. * Consolidate the name of GitHub They website is generally called 'GitHub' and not 'github'. * Quote the git commands correctly * Avoid fullstops in section names This is done for the sake of consistency. Most of the section names don't have a fullstop at the end. So, ... * Update the ToC The Table of Contents seems to have been out of date with the section titles. So, update the ToC with 'doctoc'. * Clarify that the changes are removed only for the previous commit * Showcase the flexibility of `git fetch -p` The example for that exhibits the way to 'prune' remote branches that were deleted upstream wasn't flexible as it relied on the command defaulting to the upstream of the current branch. This might lead the reader into overlooking the flexibility of the `git fetch`. Show that the 'upstream' can be mentioned in the command thus show casing the flexibility of `git fetch`. * Exemplify the safer version of branch deletion It's not good for newbies to start using 'force deletion' when they want to delete a branch as it might lead them to them into 'accidentally' deleting their branches often without merging them into other branches or pushing them to an upstream. So, exemplify the safer version of branch deletion (branch -d) and warn them about what `git branch -D` does. * Improve readability of a few phrases It's not required to use 'git' a lot as this a document about Git, after all. * Use HTTPS links for sites that serve using HTTPS * Clarify that rebasing just re-writes history Rebasing fast-forwards when the tip of the branch is a descendent of the tip of the upstream. In other cases it re-writes the history. This re-write is what actually leads the user to 'force' update the remote. So, clarify that a user has to force update only when the history is re-written regardless of whether the branch was fast-forwarded. * Attribute both the authors of the second edition of Pro Git * Try a different form of emphasizing text Capitalizing words seems to be over emphasizing words. Italicize the words, instead to see if works. * Mention what 'upstream' means for the sake of clarity * Simplify the way to create a remote branch from the local one The commands were needlessly complex by not considering the fact that the command defaults to HEAD when no branch is specified and changing configuration when it wasn't required. Simplify the commands to make readers more happy! * Remove a character cruft left over while editing This is an instance of a carelessly edited document getting into version control. ;) * Improve a sentence ... by, - expanding acronyms - quoting a command line parameter
2017-12-08 22:47:41 +06:00
This will unstage all files you might have staged with `git add`:
```sh
$ git reset
```
This will revert all local uncommitted changes (should be executed in repo root):
2014-07-23 02:33:36 +06:00
2015-09-24 21:13:56 +06:00
```sh
$ git checkout .
```
You can also revert uncommitted changes to a particular file or directory:
```sh
$ git checkout [some_dir|file.txt]
2014-07-23 02:33:36 +06:00
```
Yet another way to revert all uncommitted changes (longer to type, but works from any subdirectory):
```sh
$ git reset --hard HEAD
```
A few cleanups (#163) * Consolidate the name of the subsystem The name of the subsytem is considered to be 'Git' and not 'git'. There was inconsistency in the document by referring to the subsystem using both 'git' and 'Git'. Consolidate the usages to 'Git' which is generally considered to be the name of the subsystem. * Consolidate the name of GitHub They website is generally called 'GitHub' and not 'github'. * Quote the git commands correctly * Avoid fullstops in section names This is done for the sake of consistency. Most of the section names don't have a fullstop at the end. So, ... * Update the ToC The Table of Contents seems to have been out of date with the section titles. So, update the ToC with 'doctoc'. * Clarify that the changes are removed only for the previous commit * Showcase the flexibility of `git fetch -p` The example for that exhibits the way to 'prune' remote branches that were deleted upstream wasn't flexible as it relied on the command defaulting to the upstream of the current branch. This might lead the reader into overlooking the flexibility of the `git fetch`. Show that the 'upstream' can be mentioned in the command thus show casing the flexibility of `git fetch`. * Exemplify the safer version of branch deletion It's not good for newbies to start using 'force deletion' when they want to delete a branch as it might lead them to them into 'accidentally' deleting their branches often without merging them into other branches or pushing them to an upstream. So, exemplify the safer version of branch deletion (branch -d) and warn them about what `git branch -D` does. * Improve readability of a few phrases It's not required to use 'git' a lot as this a document about Git, after all. * Use HTTPS links for sites that serve using HTTPS * Clarify that rebasing just re-writes history Rebasing fast-forwards when the tip of the branch is a descendent of the tip of the upstream. In other cases it re-writes the history. This re-write is what actually leads the user to 'force' update the remote. So, clarify that a user has to force update only when the history is re-written regardless of whether the branch was fast-forwarded. * Attribute both the authors of the second edition of Pro Git * Try a different form of emphasizing text Capitalizing words seems to be over emphasizing words. Italicize the words, instead to see if works. * Mention what 'upstream' means for the sake of clarity * Simplify the way to create a remote branch from the local one The commands were needlessly complex by not considering the fact that the command defaults to HEAD when no branch is specified and changing configuration when it wasn't required. Simplify the commands to make readers more happy! * Remove a character cruft left over while editing This is an instance of a carelessly edited document getting into version control. ;) * Improve a sentence ... by, - expanding acronyms - quoting a command line parameter
2017-12-08 22:47:41 +06:00
This will remove all local untracked files, so only files tracked by Git remain:
```sh
$ git clean -fd
```
`-x` will also remove all ignored files.
<a href="i-want-to-discard-specific-unstaged-changes"></a>
### I want to discard specific unstaged changes
When you want to get rid of some, but not all changes in your working copy.
Checkout undesired changes, keep good changes.
```sh
$ git checkout -p
# Answer y to all of the snippets you want to drop
```
2017-06-05 17:08:56 +06:00
Another strategy involves using `stash`. Stash all the good changes, reset working copy, and reapply good changes.
```sh
$ git stash -p
# Select all of the snippets you want to save
$ git reset --hard
$ git stash pop
```
2017-06-05 17:08:56 +06:00
Alternatively, stash your undesired changes, and then drop stash.
```sh
$ git stash -p
# Select all of the snippets you don't want to save
2017-06-05 17:08:56 +06:00
$ git stash drop
```
<a href="i-want-to-discard-specific-unstaged-files"></a>
### I want to discard specific unstaged files
When you want to get rid of one specific file in your working copy.
```sh
$ git checkout myFile
```
Alternatively, to discard multiple files in your working copy, list them all.
```sh
$ git checkout myFirstFile mySecondFile
```
<a href="i-want-to-discard-only-my-unstaged-local-changes"></a>
### I want to discard only my unstaged local changes
When you want to get rid of all of your unstaged local uncommitted changes
```sh
$ git checkout .
```
<a href="i-want-to-discard-all-my-untracked-files"></a>
### I want to discard all of my untracked files
When you want to get rid of all of your untracked files
```sh
$ git clean -f
```
2015-09-24 21:13:56 +06:00
## Branches
2017-12-16 22:50:00 +06:00
<a name="i-want-to-list-all-branches"></a>
### I want to list all branches
List local branches
```sh
$ git branch
```
List remote branches
```sh
$ git branch -r
```
List all branches (both local and remote)
```sh
$ git branch -a
```
<a name="create-branch-from-commit"></a>
### Create a branch from a commit
```sh
$ git checkout -b <branch> <SHA1_OF_COMMIT>
```
2015-09-24 21:13:56 +06:00
<a name="pull-wrong-branch"></a>
### I pulled from/into the wrong branch
2014-07-23 02:33:36 +06:00
2015-09-24 21:13:56 +06:00
This is another chance to use `git reflog` to see where your HEAD pointed before the bad pull.
2014-07-23 02:33:36 +06:00
```sh
2015-09-24 21:13:56 +06:00
(master)$ git reflog
ab7555f HEAD@{0}: pull origin wrong-branch: Fast-forward
c5bc55a HEAD@{1}: checkout: checkout message goes here
2014-07-23 02:33:36 +06:00
```
2015-09-24 21:13:56 +06:00
Simply reset your branch back to the desired commit:
2014-07-30 00:53:00 +06:00
```sh
2015-09-24 21:13:56 +06:00
$ git reset --hard c5bc55a
2014-07-30 00:53:00 +06:00
```
2015-09-24 21:13:56 +06:00
Done.
2014-07-23 02:33:36 +06:00
2015-09-24 21:13:56 +06:00
<a href="discard-local-commits"></a>
### I want to discard local commits so my branch is the same as one on the server
2014-07-23 02:33:36 +06:00
2015-09-24 21:13:56 +06:00
Confirm that you haven't pushed your changes to the server.
`git status` should show how many commits you are ahead of origin:
```sh
2015-09-24 21:13:56 +06:00
(my-branch)$ git status
# On branch my-branch
# Your branch is ahead of 'origin/my-branch' by 2 commits.
# (use "git push" to publish your local commits)
#
2014-07-23 02:33:36 +06:00
```
2016-11-12 21:10:29 +06:00
One way of resetting to match origin (to have the same as what is on the remote) is to do this:
```sh
2015-09-24 21:13:56 +06:00
(master)$ git reset --hard origin/my-branch
```
<a name="commit-wrong-branch"></a>
2015-09-24 21:13:56 +06:00
### I committed to master instead of a new branch
2014-07-23 02:33:36 +06:00
Create the new branch while remaining on master:
2014-07-23 02:33:36 +06:00
```sh
2017-03-29 15:55:42 +06:00
(master)$ git branch my-branch
2014-07-23 02:33:36 +06:00
```
Reset the branch master to the previous commit:
```sh
(master)$ git reset --hard HEAD^
```
2014-07-23 02:33:36 +06:00
2017-11-15 20:08:38 +06:00
`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).
2017-11-15 20:08:38 +06:00
Note 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).
Alternatively, if you don't want to use `HEAD^`, find out what the commit hash you want to set your master 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.
2014-07-23 02:33:36 +06:00
For example, if the hash of the commit that your master branch is supposed to be at is `a13b85e`:
```sh
2014-07-23 02:33:36 +06:00
(master)$ git reset --hard a13b85e
HEAD is now at a13b85e
```
Checkout the new branch to continue working:
```sh
(master)$ git checkout my-branch
```
<a name="keep-whole-file"></a>
### I want to keep the whole file from another ref-ish
2016-08-02 07:20:33 +06:00
Say you have a working spike (see note), with hundreds of changes. Everything is working. Now, you commit into another branch to save that work:
```sh
(solution)$ git add -A && git commit -m "Adding all changes from this spike into one big commit."
```
When 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.
Say you have:
* branch `solution`, with the solution to your spike. One ahead of `develop`.
* branch `develop`, where you want to add your changes.
You can solve it bringing the contents to your branch:
```sh
(develop)$ git checkout solution -- file1.txt
```
2016-08-02 07:20:33 +06:00
This will get the contents of that file in branch `solution` to your branch `develop`:
```sh
# On branch develop
# Your branch is up-to-date with 'origin/develop'.
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# modified: file1.txt
```
2016-08-02 07:20:33 +06:00
Then, commit as usual.
Note: 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).
<a name="cherry-pick"></a>
2015-09-24 21:13:56 +06:00
### I made several commits on a single branch that should be on different branches
2014-07-23 02:33:36 +06:00
Say you are on your master branch. Running `git log`, you see you have made two commits:
```sh
2014-07-23 02:33:36 +06:00
(master)$ git log
commit e3851e817c451cc36f2e6f3049db528415e3c114
Author: Alex Lee <alexlee@example.com>
2014-07-23 02:33:36 +06:00
Date: Tue Jul 22 15:39:27 2014 -0400
Bug #21 - Added CSRF protection
2014-08-01 22:14:57 +06:00
2014-07-23 02:33:36 +06:00
commit 5ea51731d150f7ddc4a365437931cd8be3bf3131
Author: Alex Lee <alexlee@example.com>
2014-07-23 02:33:36 +06:00
Date: Tue Jul 22 15:39:12 2014 -0400
Bug #14 - Fixed spacing on title
commit a13b85e984171c6e2a1729bb061994525f626d14
Author: Aki Rose <akirose@example.com>
2014-07-23 02:33:36 +06:00
Date: Tue Jul 21 01:12:48 2014 -0400
First commit
```
Let's take note of our commit hashes for each bug (`e3851e8` for #21, `5ea5173` for #14).
First, let's reset our master branch to the correct commit (`a13b85e`):
```sh
2014-07-23 02:33:36 +06:00
(master)$ git reset --hard a13b85e
HEAD is now at a13b85e
```
Now, we can create a fresh branch for our bug #21 branch:
```sh
2014-07-23 02:33:36 +06:00
(master)$ git checkout -b 21
(21)$
```
Now, 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.
```sh
2014-07-23 02:33:36 +06:00
(21)$ git cherry-pick e3851e8
```
At 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.
2014-07-23 02:33:36 +06:00
Now let's create a new branch for bug #14, also based on master
```sh
2014-07-23 02:33:36 +06:00
(21)$ git checkout master
(master)$ git checkout -b 14
(14)$
```
And finally, let's cherry-pick the commit for bug #14:
```sh
2014-07-23 02:33:36 +06:00
(14)$ git cherry-pick 5ea5173
```
2014-07-30 04:54:50 +06:00
<a name="delete-stale-local-branches"></a>
2015-09-24 21:13:56 +06:00
### I want to delete local branches that were deleted upstream
A few cleanups (#163) * Consolidate the name of the subsystem The name of the subsytem is considered to be 'Git' and not 'git'. There was inconsistency in the document by referring to the subsystem using both 'git' and 'Git'. Consolidate the usages to 'Git' which is generally considered to be the name of the subsystem. * Consolidate the name of GitHub They website is generally called 'GitHub' and not 'github'. * Quote the git commands correctly * Avoid fullstops in section names This is done for the sake of consistency. Most of the section names don't have a fullstop at the end. So, ... * Update the ToC The Table of Contents seems to have been out of date with the section titles. So, update the ToC with 'doctoc'. * Clarify that the changes are removed only for the previous commit * Showcase the flexibility of `git fetch -p` The example for that exhibits the way to 'prune' remote branches that were deleted upstream wasn't flexible as it relied on the command defaulting to the upstream of the current branch. This might lead the reader into overlooking the flexibility of the `git fetch`. Show that the 'upstream' can be mentioned in the command thus show casing the flexibility of `git fetch`. * Exemplify the safer version of branch deletion It's not good for newbies to start using 'force deletion' when they want to delete a branch as it might lead them to them into 'accidentally' deleting their branches often without merging them into other branches or pushing them to an upstream. So, exemplify the safer version of branch deletion (branch -d) and warn them about what `git branch -D` does. * Improve readability of a few phrases It's not required to use 'git' a lot as this a document about Git, after all. * Use HTTPS links for sites that serve using HTTPS * Clarify that rebasing just re-writes history Rebasing fast-forwards when the tip of the branch is a descendent of the tip of the upstream. In other cases it re-writes the history. This re-write is what actually leads the user to 'force' update the remote. So, clarify that a user has to force update only when the history is re-written regardless of whether the branch was fast-forwarded. * Attribute both the authors of the second edition of Pro Git * Try a different form of emphasizing text Capitalizing words seems to be over emphasizing words. Italicize the words, instead to see if works. * Mention what 'upstream' means for the sake of clarity * Simplify the way to create a remote branch from the local one The commands were needlessly complex by not considering the fact that the command defaults to HEAD when no branch is specified and changing configuration when it wasn't required. Simplify the commands to make readers more happy! * Remove a character cruft left over while editing This is an instance of a carelessly edited document getting into version control. ;) * Improve a sentence ... by, - expanding acronyms - quoting a command line parameter
2017-12-08 22:47:41 +06:00
Once 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.
2015-09-24 21:13:56 +06:00
```sh
A few cleanups (#163) * Consolidate the name of the subsystem The name of the subsytem is considered to be 'Git' and not 'git'. There was inconsistency in the document by referring to the subsystem using both 'git' and 'Git'. Consolidate the usages to 'Git' which is generally considered to be the name of the subsystem. * Consolidate the name of GitHub They website is generally called 'GitHub' and not 'github'. * Quote the git commands correctly * Avoid fullstops in section names This is done for the sake of consistency. Most of the section names don't have a fullstop at the end. So, ... * Update the ToC The Table of Contents seems to have been out of date with the section titles. So, update the ToC with 'doctoc'. * Clarify that the changes are removed only for the previous commit * Showcase the flexibility of `git fetch -p` The example for that exhibits the way to 'prune' remote branches that were deleted upstream wasn't flexible as it relied on the command defaulting to the upstream of the current branch. This might lead the reader into overlooking the flexibility of the `git fetch`. Show that the 'upstream' can be mentioned in the command thus show casing the flexibility of `git fetch`. * Exemplify the safer version of branch deletion It's not good for newbies to start using 'force deletion' when they want to delete a branch as it might lead them to them into 'accidentally' deleting their branches often without merging them into other branches or pushing them to an upstream. So, exemplify the safer version of branch deletion (branch -d) and warn them about what `git branch -D` does. * Improve readability of a few phrases It's not required to use 'git' a lot as this a document about Git, after all. * Use HTTPS links for sites that serve using HTTPS * Clarify that rebasing just re-writes history Rebasing fast-forwards when the tip of the branch is a descendent of the tip of the upstream. In other cases it re-writes the history. This re-write is what actually leads the user to 'force' update the remote. So, clarify that a user has to force update only when the history is re-written regardless of whether the branch was fast-forwarded. * Attribute both the authors of the second edition of Pro Git * Try a different form of emphasizing text Capitalizing words seems to be over emphasizing words. Italicize the words, instead to see if works. * Mention what 'upstream' means for the sake of clarity * Simplify the way to create a remote branch from the local one The commands were needlessly complex by not considering the fact that the command defaults to HEAD when no branch is specified and changing configuration when it wasn't required. Simplify the commands to make readers more happy! * Remove a character cruft left over while editing This is an instance of a carelessly edited document getting into version control. ;) * Improve a sentence ... by, - expanding acronyms - quoting a command line parameter
2017-12-08 22:47:41 +06:00
$ git fetch -p upstream
```
A few cleanups (#163) * Consolidate the name of the subsystem The name of the subsytem is considered to be 'Git' and not 'git'. There was inconsistency in the document by referring to the subsystem using both 'git' and 'Git'. Consolidate the usages to 'Git' which is generally considered to be the name of the subsystem. * Consolidate the name of GitHub They website is generally called 'GitHub' and not 'github'. * Quote the git commands correctly * Avoid fullstops in section names This is done for the sake of consistency. Most of the section names don't have a fullstop at the end. So, ... * Update the ToC The Table of Contents seems to have been out of date with the section titles. So, update the ToC with 'doctoc'. * Clarify that the changes are removed only for the previous commit * Showcase the flexibility of `git fetch -p` The example for that exhibits the way to 'prune' remote branches that were deleted upstream wasn't flexible as it relied on the command defaulting to the upstream of the current branch. This might lead the reader into overlooking the flexibility of the `git fetch`. Show that the 'upstream' can be mentioned in the command thus show casing the flexibility of `git fetch`. * Exemplify the safer version of branch deletion It's not good for newbies to start using 'force deletion' when they want to delete a branch as it might lead them to them into 'accidentally' deleting their branches often without merging them into other branches or pushing them to an upstream. So, exemplify the safer version of branch deletion (branch -d) and warn them about what `git branch -D` does. * Improve readability of a few phrases It's not required to use 'git' a lot as this a document about Git, after all. * Use HTTPS links for sites that serve using HTTPS * Clarify that rebasing just re-writes history Rebasing fast-forwards when the tip of the branch is a descendent of the tip of the upstream. In other cases it re-writes the history. This re-write is what actually leads the user to 'force' update the remote. So, clarify that a user has to force update only when the history is re-written regardless of whether the branch was fast-forwarded. * Attribute both the authors of the second edition of Pro Git * Try a different form of emphasizing text Capitalizing words seems to be over emphasizing words. Italicize the words, instead to see if works. * Mention what 'upstream' means for the sake of clarity * Simplify the way to create a remote branch from the local one The commands were needlessly complex by not considering the fact that the command defaults to HEAD when no branch is specified and changing configuration when it wasn't required. Simplify the commands to make readers more happy! * Remove a character cruft left over while editing This is an instance of a carelessly edited document getting into version control. ;) * Improve a sentence ... by, - expanding acronyms - quoting a command line parameter
2017-12-08 22:47:41 +06:00
where, `upstream` is the remote you want to fetch from.
<a name='restore-a-deleted-branch'></a>
2015-09-24 21:13:56 +06:00
### I accidentally deleted my branch
If 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:
```sh
2015-09-24 21:13:56 +06:00
(master)$ git checkout -b my-branch
(my-branch)$ git branch
(my-branch)$ touch foo.txt
(my-branch)$ ls
README.md foo.txt
```
2015-09-24 21:13:56 +06:00
Let's add it and commit.
2015-09-24 21:13:56 +06:00
```sh
(my-branch)$ git add .
(my-branch)$ git commit -m 'foo.txt added'
(my-branch)$ foo.txt added
1 files changed, 1 insertions(+)
create mode 100644 foo.txt
(my-branch)$ git log
2014-08-23 02:33:56 +06:00
2015-09-24 21:13:56 +06:00
commit 4e3cd85a670ced7cc17a2b5d8d3d809ac88d5012
2016-02-15 05:45:06 +06:00
Author: siemiatj <siemiatj@example.com>
2015-09-24 21:13:56 +06:00
Date: Wed Jul 30 00:34:10 2014 +0200
2014-08-23 02:33:56 +06:00
2015-09-24 21:13:56 +06:00
foo.txt added
2015-09-24 21:13:56 +06:00
commit 69204cdf0acbab201619d95ad8295928e7f411d5
2016-02-15 05:45:06 +06:00
Author: Kate Hudson <katehudson@example.com>
2015-09-24 21:13:56 +06:00
Date: Tue Jul 29 13:14:46 2014 -0400
2015-09-24 21:13:56 +06:00
Fixes #6: Force pushing after amending commits
```
Now we're switching back to master and 'accidentally' removing our branch.
```sh
2015-09-24 21:13:56 +06:00
(my-branch)$ git checkout master
Switched to branch 'master'
Your branch is up-to-date with 'origin/master'.
(master)$ git branch -D my-branch
Deleted branch my-branch (was 4e3cd85).
(master)$ echo oh noes, deleted my branch!
oh noes, deleted my branch!
```
At this point you should get familiar with 'reflog', an upgraded logger. It stores the history of all the action in the repo.
```
(master)$ git reflog
2015-09-24 21:13:56 +06:00
69204cd HEAD@{0}: checkout: moving from my-branch to master
4e3cd85 HEAD@{1}: commit: foo.txt added
69204cd HEAD@{2}: checkout: moving from master to my-branch
```
2015-09-24 21:13:56 +06:00
As you can see we have commit hash from our deleted branch. Let's see if we can restore our deleted branch.
```sh
2015-09-24 21:13:56 +06:00
(master)$ git checkout -b my-branch-help
Switched to a new branch 'my-branch-help'
(my-branch-help)$ git reset --hard 4e3cd85
HEAD is now at 4e3cd85 foo.txt added
(my-branch-help)$ ls
README.md foo.txt
```
A few cleanups (#163) * Consolidate the name of the subsystem The name of the subsytem is considered to be 'Git' and not 'git'. There was inconsistency in the document by referring to the subsystem using both 'git' and 'Git'. Consolidate the usages to 'Git' which is generally considered to be the name of the subsystem. * Consolidate the name of GitHub They website is generally called 'GitHub' and not 'github'. * Quote the git commands correctly * Avoid fullstops in section names This is done for the sake of consistency. Most of the section names don't have a fullstop at the end. So, ... * Update the ToC The Table of Contents seems to have been out of date with the section titles. So, update the ToC with 'doctoc'. * Clarify that the changes are removed only for the previous commit * Showcase the flexibility of `git fetch -p` The example for that exhibits the way to 'prune' remote branches that were deleted upstream wasn't flexible as it relied on the command defaulting to the upstream of the current branch. This might lead the reader into overlooking the flexibility of the `git fetch`. Show that the 'upstream' can be mentioned in the command thus show casing the flexibility of `git fetch`. * Exemplify the safer version of branch deletion It's not good for newbies to start using 'force deletion' when they want to delete a branch as it might lead them to them into 'accidentally' deleting their branches often without merging them into other branches or pushing them to an upstream. So, exemplify the safer version of branch deletion (branch -d) and warn them about what `git branch -D` does. * Improve readability of a few phrases It's not required to use 'git' a lot as this a document about Git, after all. * Use HTTPS links for sites that serve using HTTPS * Clarify that rebasing just re-writes history Rebasing fast-forwards when the tip of the branch is a descendent of the tip of the upstream. In other cases it re-writes the history. This re-write is what actually leads the user to 'force' update the remote. So, clarify that a user has to force update only when the history is re-written regardless of whether the branch was fast-forwarded. * Attribute both the authors of the second edition of Pro Git * Try a different form of emphasizing text Capitalizing words seems to be over emphasizing words. Italicize the words, instead to see if works. * Mention what 'upstream' means for the sake of clarity * Simplify the way to create a remote branch from the local one The commands were needlessly complex by not considering the fact that the command defaults to HEAD when no branch is specified and changing configuration when it wasn't required. Simplify the commands to make readers more happy! * Remove a character cruft left over while editing This is an instance of a carelessly edited document getting into version control. ;) * Improve a sentence ... by, - expanding acronyms - quoting a command line parameter
2017-12-08 22:47:41 +06:00
Voila! We got our removed file back. `git reflog` is also useful when rebasing goes terribly wrong.
2014-08-01 09:16:43 +06:00
2015-09-24 21:13:56 +06:00
<a name="i-want-to-delete-a-branch"></a>
### I want to delete a branch
2014-08-01 09:16:43 +06:00
2015-09-24 21:13:56 +06:00
To delete a remote branch:
2014-08-01 09:16:43 +06:00
```sh
2015-09-24 21:13:56 +06:00
(master)$ git push origin --delete my-branch
2014-08-01 09:16:43 +06:00
```
2015-09-24 21:13:56 +06:00
You can also do:
```sh
2015-09-24 21:13:56 +06:00
(master)$ git push origin :my-branch
2014-08-01 09:16:43 +06:00
```
2015-09-24 21:13:56 +06:00
To delete a local branch:
A few cleanups (#163) * Consolidate the name of the subsystem The name of the subsytem is considered to be 'Git' and not 'git'. There was inconsistency in the document by referring to the subsystem using both 'git' and 'Git'. Consolidate the usages to 'Git' which is generally considered to be the name of the subsystem. * Consolidate the name of GitHub They website is generally called 'GitHub' and not 'github'. * Quote the git commands correctly * Avoid fullstops in section names This is done for the sake of consistency. Most of the section names don't have a fullstop at the end. So, ... * Update the ToC The Table of Contents seems to have been out of date with the section titles. So, update the ToC with 'doctoc'. * Clarify that the changes are removed only for the previous commit * Showcase the flexibility of `git fetch -p` The example for that exhibits the way to 'prune' remote branches that were deleted upstream wasn't flexible as it relied on the command defaulting to the upstream of the current branch. This might lead the reader into overlooking the flexibility of the `git fetch`. Show that the 'upstream' can be mentioned in the command thus show casing the flexibility of `git fetch`. * Exemplify the safer version of branch deletion It's not good for newbies to start using 'force deletion' when they want to delete a branch as it might lead them to them into 'accidentally' deleting their branches often without merging them into other branches or pushing them to an upstream. So, exemplify the safer version of branch deletion (branch -d) and warn them about what `git branch -D` does. * Improve readability of a few phrases It's not required to use 'git' a lot as this a document about Git, after all. * Use HTTPS links for sites that serve using HTTPS * Clarify that rebasing just re-writes history Rebasing fast-forwards when the tip of the branch is a descendent of the tip of the upstream. In other cases it re-writes the history. This re-write is what actually leads the user to 'force' update the remote. So, clarify that a user has to force update only when the history is re-written regardless of whether the branch was fast-forwarded. * Attribute both the authors of the second edition of Pro Git * Try a different form of emphasizing text Capitalizing words seems to be over emphasizing words. Italicize the words, instead to see if works. * Mention what 'upstream' means for the sake of clarity * Simplify the way to create a remote branch from the local one The commands were needlessly complex by not considering the fact that the command defaults to HEAD when no branch is specified and changing configuration when it wasn't required. Simplify the commands to make readers more happy! * Remove a character cruft left over while editing This is an instance of a carelessly edited document getting into version control. ;) * Improve a sentence ... by, - expanding acronyms - quoting a command line parameter
2017-12-08 22:47:41 +06:00
```sh
(master)$ git branch -d my-branch
```
To delete a local branch that *has not* been merged to the current branch or an upstream:
```sh
2015-09-24 21:13:56 +06:00
(master)$ git branch -D my-branch
2014-08-01 09:16:43 +06:00
```
<a name="i-want-to-delete-multiple-branches"></a>
### I want to delete multiple branches
Say you want to delete all branches that start with `fix/`:
```sh
(master)$ git branch | grep 'fix/' | xargs git branch -d
```
<a name="i-want-to-rename-a-branch"></a>
### I want to rename a branch
A few cleanups (#163) * Consolidate the name of the subsystem The name of the subsytem is considered to be 'Git' and not 'git'. There was inconsistency in the document by referring to the subsystem using both 'git' and 'Git'. Consolidate the usages to 'Git' which is generally considered to be the name of the subsystem. * Consolidate the name of GitHub They website is generally called 'GitHub' and not 'github'. * Quote the git commands correctly * Avoid fullstops in section names This is done for the sake of consistency. Most of the section names don't have a fullstop at the end. So, ... * Update the ToC The Table of Contents seems to have been out of date with the section titles. So, update the ToC with 'doctoc'. * Clarify that the changes are removed only for the previous commit * Showcase the flexibility of `git fetch -p` The example for that exhibits the way to 'prune' remote branches that were deleted upstream wasn't flexible as it relied on the command defaulting to the upstream of the current branch. This might lead the reader into overlooking the flexibility of the `git fetch`. Show that the 'upstream' can be mentioned in the command thus show casing the flexibility of `git fetch`. * Exemplify the safer version of branch deletion It's not good for newbies to start using 'force deletion' when they want to delete a branch as it might lead them to them into 'accidentally' deleting their branches often without merging them into other branches or pushing them to an upstream. So, exemplify the safer version of branch deletion (branch -d) and warn them about what `git branch -D` does. * Improve readability of a few phrases It's not required to use 'git' a lot as this a document about Git, after all. * Use HTTPS links for sites that serve using HTTPS * Clarify that rebasing just re-writes history Rebasing fast-forwards when the tip of the branch is a descendent of the tip of the upstream. In other cases it re-writes the history. This re-write is what actually leads the user to 'force' update the remote. So, clarify that a user has to force update only when the history is re-written regardless of whether the branch was fast-forwarded. * Attribute both the authors of the second edition of Pro Git * Try a different form of emphasizing text Capitalizing words seems to be over emphasizing words. Italicize the words, instead to see if works. * Mention what 'upstream' means for the sake of clarity * Simplify the way to create a remote branch from the local one The commands were needlessly complex by not considering the fact that the command defaults to HEAD when no branch is specified and changing configuration when it wasn't required. Simplify the commands to make readers more happy! * Remove a character cruft left over while editing This is an instance of a carelessly edited document getting into version control. ;) * Improve a sentence ... by, - expanding acronyms - quoting a command line parameter
2017-12-08 22:47:41 +06:00
To rename the current (local) branch:
```sh
(master)$ git branch -m new-name
```
A few cleanups (#163) * Consolidate the name of the subsystem The name of the subsytem is considered to be 'Git' and not 'git'. There was inconsistency in the document by referring to the subsystem using both 'git' and 'Git'. Consolidate the usages to 'Git' which is generally considered to be the name of the subsystem. * Consolidate the name of GitHub They website is generally called 'GitHub' and not 'github'. * Quote the git commands correctly * Avoid fullstops in section names This is done for the sake of consistency. Most of the section names don't have a fullstop at the end. So, ... * Update the ToC The Table of Contents seems to have been out of date with the section titles. So, update the ToC with 'doctoc'. * Clarify that the changes are removed only for the previous commit * Showcase the flexibility of `git fetch -p` The example for that exhibits the way to 'prune' remote branches that were deleted upstream wasn't flexible as it relied on the command defaulting to the upstream of the current branch. This might lead the reader into overlooking the flexibility of the `git fetch`. Show that the 'upstream' can be mentioned in the command thus show casing the flexibility of `git fetch`. * Exemplify the safer version of branch deletion It's not good for newbies to start using 'force deletion' when they want to delete a branch as it might lead them to them into 'accidentally' deleting their branches often without merging them into other branches or pushing them to an upstream. So, exemplify the safer version of branch deletion (branch -d) and warn them about what `git branch -D` does. * Improve readability of a few phrases It's not required to use 'git' a lot as this a document about Git, after all. * Use HTTPS links for sites that serve using HTTPS * Clarify that rebasing just re-writes history Rebasing fast-forwards when the tip of the branch is a descendent of the tip of the upstream. In other cases it re-writes the history. This re-write is what actually leads the user to 'force' update the remote. So, clarify that a user has to force update only when the history is re-written regardless of whether the branch was fast-forwarded. * Attribute both the authors of the second edition of Pro Git * Try a different form of emphasizing text Capitalizing words seems to be over emphasizing words. Italicize the words, instead to see if works. * Mention what 'upstream' means for the sake of clarity * Simplify the way to create a remote branch from the local one The commands were needlessly complex by not considering the fact that the command defaults to HEAD when no branch is specified and changing configuration when it wasn't required. Simplify the commands to make readers more happy! * Remove a character cruft left over while editing This is an instance of a carelessly edited document getting into version control. ;) * Improve a sentence ... by, - expanding acronyms - quoting a command line parameter
2017-12-08 22:47:41 +06:00
To rename a different (local) branch:
```sh
(master)$ git branch -m old-name new-name
```
2017-11-06 07:31:09 +06:00
<a name="i-want-to-checkout-to-a-remote-branch-that-someone-else-is-working-on"></a>
### I want to checkout to a remote branch that someone else is working on
First, fetch all branches from remote:
```sh
(master)$ git fetch --all
```
Say you want to checkout to `daves` from the remote.
```sh
(master)$ git checkout --track origin/daves
Branch daves set up to track remote branch daves from origin.
Switched to a new branch 'daves'
```
(`--track` is shorthand for `git checkout -b [branch] [remotename]/[branch]`)
This will give you a local copy of the branch `daves`, and any update that has been pushed will also show up remotely.
<a name="i-want-to-create-a-new-remote-branch-from-current-local-one"></a>
### I want to create a new remote branch from current local one
```sh
A few cleanups (#163) * Consolidate the name of the subsystem The name of the subsytem is considered to be 'Git' and not 'git'. There was inconsistency in the document by referring to the subsystem using both 'git' and 'Git'. Consolidate the usages to 'Git' which is generally considered to be the name of the subsystem. * Consolidate the name of GitHub They website is generally called 'GitHub' and not 'github'. * Quote the git commands correctly * Avoid fullstops in section names This is done for the sake of consistency. Most of the section names don't have a fullstop at the end. So, ... * Update the ToC The Table of Contents seems to have been out of date with the section titles. So, update the ToC with 'doctoc'. * Clarify that the changes are removed only for the previous commit * Showcase the flexibility of `git fetch -p` The example for that exhibits the way to 'prune' remote branches that were deleted upstream wasn't flexible as it relied on the command defaulting to the upstream of the current branch. This might lead the reader into overlooking the flexibility of the `git fetch`. Show that the 'upstream' can be mentioned in the command thus show casing the flexibility of `git fetch`. * Exemplify the safer version of branch deletion It's not good for newbies to start using 'force deletion' when they want to delete a branch as it might lead them to them into 'accidentally' deleting their branches often without merging them into other branches or pushing them to an upstream. So, exemplify the safer version of branch deletion (branch -d) and warn them about what `git branch -D` does. * Improve readability of a few phrases It's not required to use 'git' a lot as this a document about Git, after all. * Use HTTPS links for sites that serve using HTTPS * Clarify that rebasing just re-writes history Rebasing fast-forwards when the tip of the branch is a descendent of the tip of the upstream. In other cases it re-writes the history. This re-write is what actually leads the user to 'force' update the remote. So, clarify that a user has to force update only when the history is re-written regardless of whether the branch was fast-forwarded. * Attribute both the authors of the second edition of Pro Git * Try a different form of emphasizing text Capitalizing words seems to be over emphasizing words. Italicize the words, instead to see if works. * Mention what 'upstream' means for the sake of clarity * Simplify the way to create a remote branch from the local one The commands were needlessly complex by not considering the fact that the command defaults to HEAD when no branch is specified and changing configuration when it wasn't required. Simplify the commands to make readers more happy! * Remove a character cruft left over while editing This is an instance of a carelessly edited document getting into version control. ;) * Improve a sentence ... by, - expanding acronyms - quoting a command line parameter
2017-12-08 22:47:41 +06:00
$ git push <remote>
```
A few cleanups (#163) * Consolidate the name of the subsystem The name of the subsytem is considered to be 'Git' and not 'git'. There was inconsistency in the document by referring to the subsystem using both 'git' and 'Git'. Consolidate the usages to 'Git' which is generally considered to be the name of the subsystem. * Consolidate the name of GitHub They website is generally called 'GitHub' and not 'github'. * Quote the git commands correctly * Avoid fullstops in section names This is done for the sake of consistency. Most of the section names don't have a fullstop at the end. So, ... * Update the ToC The Table of Contents seems to have been out of date with the section titles. So, update the ToC with 'doctoc'. * Clarify that the changes are removed only for the previous commit * Showcase the flexibility of `git fetch -p` The example for that exhibits the way to 'prune' remote branches that were deleted upstream wasn't flexible as it relied on the command defaulting to the upstream of the current branch. This might lead the reader into overlooking the flexibility of the `git fetch`. Show that the 'upstream' can be mentioned in the command thus show casing the flexibility of `git fetch`. * Exemplify the safer version of branch deletion It's not good for newbies to start using 'force deletion' when they want to delete a branch as it might lead them to them into 'accidentally' deleting their branches often without merging them into other branches or pushing them to an upstream. So, exemplify the safer version of branch deletion (branch -d) and warn them about what `git branch -D` does. * Improve readability of a few phrases It's not required to use 'git' a lot as this a document about Git, after all. * Use HTTPS links for sites that serve using HTTPS * Clarify that rebasing just re-writes history Rebasing fast-forwards when the tip of the branch is a descendent of the tip of the upstream. In other cases it re-writes the history. This re-write is what actually leads the user to 'force' update the remote. So, clarify that a user has to force update only when the history is re-written regardless of whether the branch was fast-forwarded. * Attribute both the authors of the second edition of Pro Git * Try a different form of emphasizing text Capitalizing words seems to be over emphasizing words. Italicize the words, instead to see if works. * Mention what 'upstream' means for the sake of clarity * Simplify the way to create a remote branch from the local one The commands were needlessly complex by not considering the fact that the command defaults to HEAD when no branch is specified and changing configuration when it wasn't required. Simplify the commands to make readers more happy! * Remove a character cruft left over while editing This is an instance of a carelessly edited document getting into version control. ;) * Improve a sentence ... by, - expanding acronyms - quoting a command line parameter
2017-12-08 22:47:41 +06:00
If you would also like to set that remote branch as upstream for the current one, use the following instead:
```sh
A few cleanups (#163) * Consolidate the name of the subsystem The name of the subsytem is considered to be 'Git' and not 'git'. There was inconsistency in the document by referring to the subsystem using both 'git' and 'Git'. Consolidate the usages to 'Git' which is generally considered to be the name of the subsystem. * Consolidate the name of GitHub They website is generally called 'GitHub' and not 'github'. * Quote the git commands correctly * Avoid fullstops in section names This is done for the sake of consistency. Most of the section names don't have a fullstop at the end. So, ... * Update the ToC The Table of Contents seems to have been out of date with the section titles. So, update the ToC with 'doctoc'. * Clarify that the changes are removed only for the previous commit * Showcase the flexibility of `git fetch -p` The example for that exhibits the way to 'prune' remote branches that were deleted upstream wasn't flexible as it relied on the command defaulting to the upstream of the current branch. This might lead the reader into overlooking the flexibility of the `git fetch`. Show that the 'upstream' can be mentioned in the command thus show casing the flexibility of `git fetch`. * Exemplify the safer version of branch deletion It's not good for newbies to start using 'force deletion' when they want to delete a branch as it might lead them to them into 'accidentally' deleting their branches often without merging them into other branches or pushing them to an upstream. So, exemplify the safer version of branch deletion (branch -d) and warn them about what `git branch -D` does. * Improve readability of a few phrases It's not required to use 'git' a lot as this a document about Git, after all. * Use HTTPS links for sites that serve using HTTPS * Clarify that rebasing just re-writes history Rebasing fast-forwards when the tip of the branch is a descendent of the tip of the upstream. In other cases it re-writes the history. This re-write is what actually leads the user to 'force' update the remote. So, clarify that a user has to force update only when the history is re-written regardless of whether the branch was fast-forwarded. * Attribute both the authors of the second edition of Pro Git * Try a different form of emphasizing text Capitalizing words seems to be over emphasizing words. Italicize the words, instead to see if works. * Mention what 'upstream' means for the sake of clarity * Simplify the way to create a remote branch from the local one The commands were needlessly complex by not considering the fact that the command defaults to HEAD when no branch is specified and changing configuration when it wasn't required. Simplify the commands to make readers more happy! * Remove a character cruft left over while editing This is an instance of a carelessly edited document getting into version control. ;) * Improve a sentence ... by, - expanding acronyms - quoting a command line parameter
2017-12-08 22:47:41 +06:00
$ git push -u <remote>
```
A few cleanups (#163) * Consolidate the name of the subsystem The name of the subsytem is considered to be 'Git' and not 'git'. There was inconsistency in the document by referring to the subsystem using both 'git' and 'Git'. Consolidate the usages to 'Git' which is generally considered to be the name of the subsystem. * Consolidate the name of GitHub They website is generally called 'GitHub' and not 'github'. * Quote the git commands correctly * Avoid fullstops in section names This is done for the sake of consistency. Most of the section names don't have a fullstop at the end. So, ... * Update the ToC The Table of Contents seems to have been out of date with the section titles. So, update the ToC with 'doctoc'. * Clarify that the changes are removed only for the previous commit * Showcase the flexibility of `git fetch -p` The example for that exhibits the way to 'prune' remote branches that were deleted upstream wasn't flexible as it relied on the command defaulting to the upstream of the current branch. This might lead the reader into overlooking the flexibility of the `git fetch`. Show that the 'upstream' can be mentioned in the command thus show casing the flexibility of `git fetch`. * Exemplify the safer version of branch deletion It's not good for newbies to start using 'force deletion' when they want to delete a branch as it might lead them to them into 'accidentally' deleting their branches often without merging them into other branches or pushing them to an upstream. So, exemplify the safer version of branch deletion (branch -d) and warn them about what `git branch -D` does. * Improve readability of a few phrases It's not required to use 'git' a lot as this a document about Git, after all. * Use HTTPS links for sites that serve using HTTPS * Clarify that rebasing just re-writes history Rebasing fast-forwards when the tip of the branch is a descendent of the tip of the upstream. In other cases it re-writes the history. This re-write is what actually leads the user to 'force' update the remote. So, clarify that a user has to force update only when the history is re-written regardless of whether the branch was fast-forwarded. * Attribute both the authors of the second edition of Pro Git * Try a different form of emphasizing text Capitalizing words seems to be over emphasizing words. Italicize the words, instead to see if works. * Mention what 'upstream' means for the sake of clarity * Simplify the way to create a remote branch from the local one The commands were needlessly complex by not considering the fact that the command defaults to HEAD when no branch is specified and changing configuration when it wasn't required. Simplify the commands to make readers more happy! * Remove a character cruft left over while editing This is an instance of a carelessly edited document getting into version control. ;) * Improve a sentence ... by, - expanding acronyms - quoting a command line parameter
2017-12-08 22:47:41 +06:00
With 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`:
```sh
$ git push
```
The behavior of the other modes of ```git push``` is described in the doc of push.default.
<a name="i-want-to-set-a-remote-branch-as-the-upstream-for-a-local-branch"></a>
### I want to set a remote branch as the upstream for a local branch
You can set a remote branch as the upstream for the current local branch using:
```sh
$ git branch --set-upstream-to [remotename]/[branch]
# or, using the shorthand:
$ git branch -u [remotename]/[branch]
```
To set the upstream remote branch for another local branch:
```sh
$ git branch -u [remotename]/[branch] [local-branch]
```
<a name="i-want-to-set-my-HEAD-to-track-the-default-remote-branch"></a>
### I want to set my HEAD to track the default remote branch
By checking your remote branches, you can see which remote branch your HEAD is tracking. In some cases, this is not the desired branch.
```sh
$ git branch -r
origin/HEAD -> origin/gh-pages
origin/master
```
To change `origin/HEAD` to track `origin/master`, you can run this command:
```sh
$ git remote set-head origin --auto
origin/HEAD set to master
```
2015-09-24 21:13:56 +06:00
## Rebasing and Merging
<a name="undo-rebase"></a>
### I want to undo rebase/merge
2017-01-08 05:32:15 +06:00
You 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.
```sh
(my-branch)$ git reset --hard ORIG_HEAD
```
2015-09-24 21:13:56 +06:00
<a name="force-push-rebase"></a>
A few cleanups (#163) * Consolidate the name of the subsystem The name of the subsytem is considered to be 'Git' and not 'git'. There was inconsistency in the document by referring to the subsystem using both 'git' and 'Git'. Consolidate the usages to 'Git' which is generally considered to be the name of the subsystem. * Consolidate the name of GitHub They website is generally called 'GitHub' and not 'github'. * Quote the git commands correctly * Avoid fullstops in section names This is done for the sake of consistency. Most of the section names don't have a fullstop at the end. So, ... * Update the ToC The Table of Contents seems to have been out of date with the section titles. So, update the ToC with 'doctoc'. * Clarify that the changes are removed only for the previous commit * Showcase the flexibility of `git fetch -p` The example for that exhibits the way to 'prune' remote branches that were deleted upstream wasn't flexible as it relied on the command defaulting to the upstream of the current branch. This might lead the reader into overlooking the flexibility of the `git fetch`. Show that the 'upstream' can be mentioned in the command thus show casing the flexibility of `git fetch`. * Exemplify the safer version of branch deletion It's not good for newbies to start using 'force deletion' when they want to delete a branch as it might lead them to them into 'accidentally' deleting their branches often without merging them into other branches or pushing them to an upstream. So, exemplify the safer version of branch deletion (branch -d) and warn them about what `git branch -D` does. * Improve readability of a few phrases It's not required to use 'git' a lot as this a document about Git, after all. * Use HTTPS links for sites that serve using HTTPS * Clarify that rebasing just re-writes history Rebasing fast-forwards when the tip of the branch is a descendent of the tip of the upstream. In other cases it re-writes the history. This re-write is what actually leads the user to 'force' update the remote. So, clarify that a user has to force update only when the history is re-written regardless of whether the branch was fast-forwarded. * Attribute both the authors of the second edition of Pro Git * Try a different form of emphasizing text Capitalizing words seems to be over emphasizing words. Italicize the words, instead to see if works. * Mention what 'upstream' means for the sake of clarity * Simplify the way to create a remote branch from the local one The commands were needlessly complex by not considering the fact that the command defaults to HEAD when no branch is specified and changing configuration when it wasn't required. Simplify the commands to make readers more happy! * Remove a character cruft left over while editing This is an instance of a carelessly edited document getting into version control. ;) * Improve a sentence ... by, - expanding acronyms - quoting a command line parameter
2017-12-08 22:47:41 +06:00
### I rebased, but I don't want to force push
2015-07-04 02:22:51 +06:00
A few cleanups (#163) * Consolidate the name of the subsystem The name of the subsytem is considered to be 'Git' and not 'git'. There was inconsistency in the document by referring to the subsystem using both 'git' and 'Git'. Consolidate the usages to 'Git' which is generally considered to be the name of the subsystem. * Consolidate the name of GitHub They website is generally called 'GitHub' and not 'github'. * Quote the git commands correctly * Avoid fullstops in section names This is done for the sake of consistency. Most of the section names don't have a fullstop at the end. So, ... * Update the ToC The Table of Contents seems to have been out of date with the section titles. So, update the ToC with 'doctoc'. * Clarify that the changes are removed only for the previous commit * Showcase the flexibility of `git fetch -p` The example for that exhibits the way to 'prune' remote branches that were deleted upstream wasn't flexible as it relied on the command defaulting to the upstream of the current branch. This might lead the reader into overlooking the flexibility of the `git fetch`. Show that the 'upstream' can be mentioned in the command thus show casing the flexibility of `git fetch`. * Exemplify the safer version of branch deletion It's not good for newbies to start using 'force deletion' when they want to delete a branch as it might lead them to them into 'accidentally' deleting their branches often without merging them into other branches or pushing them to an upstream. So, exemplify the safer version of branch deletion (branch -d) and warn them about what `git branch -D` does. * Improve readability of a few phrases It's not required to use 'git' a lot as this a document about Git, after all. * Use HTTPS links for sites that serve using HTTPS * Clarify that rebasing just re-writes history Rebasing fast-forwards when the tip of the branch is a descendent of the tip of the upstream. In other cases it re-writes the history. This re-write is what actually leads the user to 'force' update the remote. So, clarify that a user has to force update only when the history is re-written regardless of whether the branch was fast-forwarded. * Attribute both the authors of the second edition of Pro Git * Try a different form of emphasizing text Capitalizing words seems to be over emphasizing words. Italicize the words, instead to see if works. * Mention what 'upstream' means for the sake of clarity * Simplify the way to create a remote branch from the local one The commands were needlessly complex by not considering the fact that the command defaults to HEAD when no branch is specified and changing configuration when it wasn't required. Simplify the commands to make readers more happy! * Remove a character cruft left over while editing This is an instance of a carelessly edited document getting into version control. ;) * Improve a sentence ... by, - expanding acronyms - quoting a command line parameter
2017-12-08 22:47:41 +06:00
Unfortunately, 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:
2015-07-04 02:22:51 +06:00
```sh
2015-09-24 21:13:56 +06:00
(master)$ git checkout my-branch
(my-branch)$ git rebase -i master
(my-branch)$ git checkout master
(master)$ git merge --ff-only my-branch
2015-07-04 02:22:51 +06:00
```
A few cleanups (#163) * Consolidate the name of the subsystem The name of the subsytem is considered to be 'Git' and not 'git'. There was inconsistency in the document by referring to the subsystem using both 'git' and 'Git'. Consolidate the usages to 'Git' which is generally considered to be the name of the subsystem. * Consolidate the name of GitHub They website is generally called 'GitHub' and not 'github'. * Quote the git commands correctly * Avoid fullstops in section names This is done for the sake of consistency. Most of the section names don't have a fullstop at the end. So, ... * Update the ToC The Table of Contents seems to have been out of date with the section titles. So, update the ToC with 'doctoc'. * Clarify that the changes are removed only for the previous commit * Showcase the flexibility of `git fetch -p` The example for that exhibits the way to 'prune' remote branches that were deleted upstream wasn't flexible as it relied on the command defaulting to the upstream of the current branch. This might lead the reader into overlooking the flexibility of the `git fetch`. Show that the 'upstream' can be mentioned in the command thus show casing the flexibility of `git fetch`. * Exemplify the safer version of branch deletion It's not good for newbies to start using 'force deletion' when they want to delete a branch as it might lead them to them into 'accidentally' deleting their branches often without merging them into other branches or pushing them to an upstream. So, exemplify the safer version of branch deletion (branch -d) and warn them about what `git branch -D` does. * Improve readability of a few phrases It's not required to use 'git' a lot as this a document about Git, after all. * Use HTTPS links for sites that serve using HTTPS * Clarify that rebasing just re-writes history Rebasing fast-forwards when the tip of the branch is a descendent of the tip of the upstream. In other cases it re-writes the history. This re-write is what actually leads the user to 'force' update the remote. So, clarify that a user has to force update only when the history is re-written regardless of whether the branch was fast-forwarded. * Attribute both the authors of the second edition of Pro Git * Try a different form of emphasizing text Capitalizing words seems to be over emphasizing words. Italicize the words, instead to see if works. * Mention what 'upstream' means for the sake of clarity * Simplify the way to create a remote branch from the local one The commands were needlessly complex by not considering the fact that the command defaults to HEAD when no branch is specified and changing configuration when it wasn't required. Simplify the commands to make readers more happy! * Remove a character cruft left over while editing This is an instance of a carelessly edited document getting into version control. ;) * Improve a sentence ... by, - expanding acronyms - quoting a command line parameter
2017-12-08 22:47:41 +06:00
For more, see [this SO thread](https://stackoverflow.com/questions/11058312/how-can-i-use-git-rebase-without-requiring-a-forced-push).
2015-07-04 02:22:51 +06:00
2015-09-24 21:13:56 +06:00
<a name="interactive-rebase"></a>
### I need to combine commits
2015-07-04 02:22:51 +06:00
Let's suppose you are working in a branch that is/will become a pull-request against `master`. 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 master branch is up to date and all your changes committed, then:
2015-07-04 02:22:51 +06:00
```sh
(my-branch)$ git reset --soft master
(my-branch)$ git commit -am "New awesome feature"
```
2016-02-22 20:54:12 +06:00
If you want more control, and also to preserve timestamps, you need to do something called an interactive rebase:
```sh
2015-09-24 21:13:56 +06:00
(my-branch)$ git rebase -i master
```
2015-09-24 21:13:56 +06:00
If 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.
```sh
2015-09-24 21:13:56 +06:00
(master)$ git rebase -i HEAD~2
```
2015-09-24 21:13:56 +06:00
After you run the interactive rebase command, you will see something like this in your text editor:
2014-08-15 00:46:47 +06:00
2015-09-24 21:13:56 +06:00
```vim
pick a9c8a1d Some refactoring
2015-09-24 21:13:56 +06:00
pick 01b2fd8 New awesome feature
pick b729ad5 fixup
pick e3851e8 another fix
2014-08-15 00:46:47 +06:00
2015-09-24 21:13:56 +06:00
# Rebase 8074d12..b729ad5 onto 8074d12
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out
2014-08-15 00:46:47 +06:00
```
2015-04-26 16:18:50 +06:00
2015-09-24 21:13:56 +06:00
All the lines beginning with a `#` are comments, they won't affect your rebase.
2014-08-01 09:16:43 +06:00
Then you replace `pick` commands with any in the list above, and you can also remove commits by removing corresponding lines.
For 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`:
2014-08-01 09:16:43 +06:00
2015-09-24 21:13:56 +06:00
```vim
pick a9c8a1d Some refactoring
2015-09-24 21:13:56 +06:00
pick 01b2fd8 New awesome feature
f b729ad5 fixup
f e3851e8 another fix
2014-08-01 09:16:43 +06:00
```
If 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`:
2015-09-24 21:13:56 +06:00
```vim
pick a9c8a1d Some refactoring
pick 01b2fd8 New awesome feature
s b729ad5 fixup
s e3851e8 another fix
```
2015-09-24 21:13:56 +06:00
You can then rename the commit in the next text prompt that pops up.
2015-09-24 21:13:56 +06:00
```vim
Newer, awesomer features
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
# rebase in progress; onto 8074d12
# You are currently editing a commit while rebasing branch 'master' on '8074d12'.
#
# Changes to be committed:
# modified: README.md
2015-09-24 21:13:56 +06:00
#
```
2015-09-24 21:13:56 +06:00
If everything is successful, you should see something like this:
2015-03-23 09:15:55 +06:00
```sh
2015-09-24 21:13:56 +06:00
(master)$ Successfully rebased and updated refs/heads/master.
2015-03-23 09:15:55 +06:00
```
2015-09-24 21:13:56 +06:00
#### Safe merging strategy
`--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.
2015-03-23 09:15:55 +06:00
```sh
2015-09-24 21:13:56 +06:00
(master)$ git merge --no-ff --no-commit my-branch
2015-03-23 09:15:55 +06:00
```
2015-09-24 21:13:56 +06:00
#### I need to merge a branch into a single commit
2015-03-23 09:15:55 +06:00
```sh
2015-09-24 21:13:56 +06:00
(master)$ git merge --squash my-branch
2015-03-23 09:15:55 +06:00
```
2015-09-24 21:13:56 +06:00
<a name="rebase-unpushed-commits"></a>
#### I want to combine only unpushed commits
Sometimes 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.
2015-03-23 09:15:55 +06:00
```sh
2015-09-24 21:13:56 +06:00
(master)$ git rebase -i @{u}
```
2015-03-23 09:15:55 +06:00
2015-09-24 21:13:56 +06:00
This 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.
2015-03-23 09:15:55 +06:00
<a name="i-need-to-abort-the-merge"></a>
2017-11-28 09:02:58 +06:00
#### I need to abort the merge
Sometimes 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.
```sh
(my-branch)$ git merge --abort
```
A few cleanups (#163) * Consolidate the name of the subsystem The name of the subsytem is considered to be 'Git' and not 'git'. There was inconsistency in the document by referring to the subsystem using both 'git' and 'Git'. Consolidate the usages to 'Git' which is generally considered to be the name of the subsystem. * Consolidate the name of GitHub They website is generally called 'GitHub' and not 'github'. * Quote the git commands correctly * Avoid fullstops in section names This is done for the sake of consistency. Most of the section names don't have a fullstop at the end. So, ... * Update the ToC The Table of Contents seems to have been out of date with the section titles. So, update the ToC with 'doctoc'. * Clarify that the changes are removed only for the previous commit * Showcase the flexibility of `git fetch -p` The example for that exhibits the way to 'prune' remote branches that were deleted upstream wasn't flexible as it relied on the command defaulting to the upstream of the current branch. This might lead the reader into overlooking the flexibility of the `git fetch`. Show that the 'upstream' can be mentioned in the command thus show casing the flexibility of `git fetch`. * Exemplify the safer version of branch deletion It's not good for newbies to start using 'force deletion' when they want to delete a branch as it might lead them to them into 'accidentally' deleting their branches often without merging them into other branches or pushing them to an upstream. So, exemplify the safer version of branch deletion (branch -d) and warn them about what `git branch -D` does. * Improve readability of a few phrases It's not required to use 'git' a lot as this a document about Git, after all. * Use HTTPS links for sites that serve using HTTPS * Clarify that rebasing just re-writes history Rebasing fast-forwards when the tip of the branch is a descendent of the tip of the upstream. In other cases it re-writes the history. This re-write is what actually leads the user to 'force' update the remote. So, clarify that a user has to force update only when the history is re-written regardless of whether the branch was fast-forwarded. * Attribute both the authors of the second edition of Pro Git * Try a different form of emphasizing text Capitalizing words seems to be over emphasizing words. Italicize the words, instead to see if works. * Mention what 'upstream' means for the sake of clarity * Simplify the way to create a remote branch from the local one The commands were needlessly complex by not considering the fact that the command defaults to HEAD when no branch is specified and changing configuration when it wasn't required. Simplify the commands to make readers more happy! * Remove a character cruft left over while editing This is an instance of a carelessly edited document getting into version control. ;) * Improve a sentence ... by, - expanding acronyms - quoting a command line parameter
2017-12-08 22:47:41 +06:00
This command is available since Git version >= 1.7.4
2017-11-28 09:02:58 +06:00
2015-09-24 21:13:56 +06:00
<a name="check-if-all-commits-on-a-branch-are-merged"></a>
### Check if all commits on a branch are merged
2015-03-23 09:15:55 +06:00
2015-09-24 21:13:56 +06:00
To check if all commits on a branch are merged into another branch, you should diff between the heads (or any commits) of those branches:
2015-03-23 09:15:55 +06:00
2015-09-24 21:13:56 +06:00
```sh
(master)$ git log --graph --left-right --cherry-pick --oneline HEAD...feature/120-on-scroll
2015-03-23 09:15:55 +06:00
```
2015-09-24 21:13:56 +06:00
This 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:
2015-03-23 09:15:55 +06:00
```sh
2015-09-24 21:13:56 +06:00
(master)$ git log master ^feature/120-on-scroll --no-merges
2015-03-23 09:15:55 +06:00
```
2015-09-24 21:13:56 +06:00
### Possible issues with interactive rebases
<a name="noop"></a>
#### The rebase editing screen says 'noop'
2015-03-23 09:15:55 +06:00
2015-09-24 21:13:56 +06:00
If you're seeing this:
2015-03-23 09:15:55 +06:00
```
2015-09-24 21:13:56 +06:00
noop
2015-03-23 09:15:55 +06:00
```
2015-09-24 21:13:56 +06:00
That 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:
2015-03-23 09:15:55 +06:00
2015-09-24 21:13:56 +06:00
* making sure your master branch is where it should be
* rebase against `HEAD~2` or earlier instead
2015-03-23 09:15:55 +06:00
2015-09-24 21:13:56 +06:00
<a name="merge-conflict"></a>
#### There were conflicts
2015-09-24 21:13:56 +06:00
If you are unable to successfully complete the rebase, you may have to resolve conflicts.
2015-09-24 21:13:56 +06:00
First run `git status` to see which files have conflicts in them:
2015-03-23 09:15:55 +06:00
```sh
2015-09-24 21:13:56 +06:00
(my-branch)$ git status
On branch my-branch
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
both modified: README.md
2014-12-01 07:21:34 +06:00
```
2015-09-24 21:13:56 +06:00
In this example, `README.md` has conflicts. Open that file and look for the following:
2015-09-24 21:13:56 +06:00
```vim
<<<<<<< HEAD
some code
=========
some code
>>>>>>> new-commit
```
2015-09-24 21:13:56 +06:00
You 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`.
If you want to keep one branch's version of the code, you can use `--ours` or `--theirs`:
```sh
(master*)$ git checkout --ours README.md
```
- When *merging*, use `--ours` to keep changes from the local branch, or `--theirs` to keep changes from the other branch.
A few cleanups (#163) * Consolidate the name of the subsystem The name of the subsytem is considered to be 'Git' and not 'git'. There was inconsistency in the document by referring to the subsystem using both 'git' and 'Git'. Consolidate the usages to 'Git' which is generally considered to be the name of the subsystem. * Consolidate the name of GitHub They website is generally called 'GitHub' and not 'github'. * Quote the git commands correctly * Avoid fullstops in section names This is done for the sake of consistency. Most of the section names don't have a fullstop at the end. So, ... * Update the ToC The Table of Contents seems to have been out of date with the section titles. So, update the ToC with 'doctoc'. * Clarify that the changes are removed only for the previous commit * Showcase the flexibility of `git fetch -p` The example for that exhibits the way to 'prune' remote branches that were deleted upstream wasn't flexible as it relied on the command defaulting to the upstream of the current branch. This might lead the reader into overlooking the flexibility of the `git fetch`. Show that the 'upstream' can be mentioned in the command thus show casing the flexibility of `git fetch`. * Exemplify the safer version of branch deletion It's not good for newbies to start using 'force deletion' when they want to delete a branch as it might lead them to them into 'accidentally' deleting their branches often without merging them into other branches or pushing them to an upstream. So, exemplify the safer version of branch deletion (branch -d) and warn them about what `git branch -D` does. * Improve readability of a few phrases It's not required to use 'git' a lot as this a document about Git, after all. * Use HTTPS links for sites that serve using HTTPS * Clarify that rebasing just re-writes history Rebasing fast-forwards when the tip of the branch is a descendent of the tip of the upstream. In other cases it re-writes the history. This re-write is what actually leads the user to 'force' update the remote. So, clarify that a user has to force update only when the history is re-written regardless of whether the branch was fast-forwarded. * Attribute both the authors of the second edition of Pro Git * Try a different form of emphasizing text Capitalizing words seems to be over emphasizing words. Italicize the words, instead to see if works. * Mention what 'upstream' means for the sake of clarity * Simplify the way to create a remote branch from the local one The commands were needlessly complex by not considering the fact that the command defaults to HEAD when no branch is specified and changing configuration when it wasn't required. Simplify the commands to make readers more happy! * Remove a character cruft left over while editing This is an instance of a carelessly edited document getting into version control. ;) * Improve a sentence ... by, - expanding acronyms - quoting a command line parameter
2017-12-08 22:47:41 +06:00
- 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#git-rebase---merge).
If the merges are more complicated, you can use a visual diff editor:
2015-09-24 21:13:56 +06:00
```sh
(master*)$ git mergetool -t opendiff
```
2015-09-24 21:13:56 +06:00
After 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`
2015-03-23 09:15:55 +06:00
```sh
2015-09-24 21:13:56 +06:00
(my-branch)$ git add README.md
(my-branch)$ git rebase --continue
2014-12-01 07:21:34 +06:00
```
If 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.
2015-09-24 21:13:56 +06:00
If at any time you want to stop the entire rebase and go back to the original state of your branch, you can do so:
```sh
2015-09-24 21:13:56 +06:00
(my-branch)$ git rebase --abort
```
<a name="stashing"></a>
## Stash
<a name="stash-all-edits"></a>
### Stash all edits
To stash all the edits in your working directory
```sh
$ git stash
```
If you also want to stash untracked files, use `-u` option.
```sh
$ git stash -u
```
<a name="stash-specific-files"></a>
### Stash specific files
To stash only one file from your working directory
```sh
$ git stash push working-directory-path/filename.ext
```
To stash multiple files from your working directory
```sh
$ git stash push working-directory-path/filename1.ext working-directory-path/filename2.ext
```
<a name="stash-msg"></a>
### Stash with message
```sh
$ git stash save <message>
```
<a name="stash-apply-specific"></a>
### Apply a specific stash from list
First check your list of stashes with message using
```sh
$ git stash list
```
Then apply a specific stash from the list using
```sh
$ git stash apply "stash@{n}"
```
Here, 'n' indicates the position of the stash in the stack. The topmost stash will be position 0.
<a name="finding"></a>
## Finding
<a name="i-want-to-find-a-string-in-any-commit"></a>
### I want to find a string in any commit
To find a certain string which was introduced in any commit, you can use the following structure:
```sh
$ git log -S "string to find"
```
Commons parameters:
* `--source` means to show the ref name given on the command line by which each commit was reached.
* `--all` means to start from every branch.
* `--reverse` prints in reverse order, it means that will show the first commit that made the change.
<a name="i-want-to-find-by-author-committer"></a>
### I want to find by author/committer
To find all commits by author/committer you can use:
```sh
$ git log --author=<name or email>
$ git log --committer=<name or email>
```
Keep 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.
<a name="i-want-to-list-commits-containing-specific-files"></a>
### I want to list commits containing specific files
To find all commits containing a specific file you can use:
```sh
$ git log -- <path to file>
```
You would usually specify an exact path, but you may also use wild cards in the path and file name:
```sh
$ git log -- **/*.js
```
While using wildcards, it's useful to inform `--name-status` to see the list of commited files:
```sh
$ git log --name-status -- **/*.js
```
<a name="find-a-tag-where-a-commit-is-referenced"></a>
### Find a tag where a commit is referenced
2017-12-31 00:26:44 +06:00
To find all tags containing a specific commit:
```sh
$ git tag --contains <commitid>
```
## Submodules
2015-09-24 21:13:56 +06:00
<a name="clone-submodules"></a>
### Clone all submodules
```sh
2015-09-24 21:13:56 +06:00
$ git clone --recursive git://github.com/foo/bar.git
```
2015-09-24 21:13:56 +06:00
If already cloned:
```sh
2015-09-24 21:13:56 +06:00
$ git submodule update --init --recursive
```
<a name="delete-submodule"></a>
### Remove a submodule
Creating a submodule is pretty straight-forward, but deleting them less so. The commands you need are:
```sh
$ git submodule deinit submodulename
$ git rm submodulename
$ git rm --cached submodulename
$ rm -rf .git/modules/submodulename
```
<a name="miscellaneous-objects"></a>
## Miscellaneous Objects
### Restore a deleted file
First find the commit when the file last existed:
```sh
$ git rev-list -n 1 HEAD -- filename
```
Then checkout that file:
```
git checkout deletingcommitid^ -- filename
```
2014-12-01 07:21:34 +06:00
<a name="delete-tag"></a>
### Delete tag
```sh
$ git tag -d <tag_name>
$ git push <remote> :refs/tags/<tag_name>
2014-12-01 07:21:34 +06:00
```
2015-05-19 13:23:25 +06:00
<a name="recover-tag"></a>
### Recover a deleted tag
If 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:
```sh
$ git fsck --unreachable | grep tag
```
A few cleanups (#163) * Consolidate the name of the subsystem The name of the subsytem is considered to be 'Git' and not 'git'. There was inconsistency in the document by referring to the subsystem using both 'git' and 'Git'. Consolidate the usages to 'Git' which is generally considered to be the name of the subsystem. * Consolidate the name of GitHub They website is generally called 'GitHub' and not 'github'. * Quote the git commands correctly * Avoid fullstops in section names This is done for the sake of consistency. Most of the section names don't have a fullstop at the end. So, ... * Update the ToC The Table of Contents seems to have been out of date with the section titles. So, update the ToC with 'doctoc'. * Clarify that the changes are removed only for the previous commit * Showcase the flexibility of `git fetch -p` The example for that exhibits the way to 'prune' remote branches that were deleted upstream wasn't flexible as it relied on the command defaulting to the upstream of the current branch. This might lead the reader into overlooking the flexibility of the `git fetch`. Show that the 'upstream' can be mentioned in the command thus show casing the flexibility of `git fetch`. * Exemplify the safer version of branch deletion It's not good for newbies to start using 'force deletion' when they want to delete a branch as it might lead them to them into 'accidentally' deleting their branches often without merging them into other branches or pushing them to an upstream. So, exemplify the safer version of branch deletion (branch -d) and warn them about what `git branch -D` does. * Improve readability of a few phrases It's not required to use 'git' a lot as this a document about Git, after all. * Use HTTPS links for sites that serve using HTTPS * Clarify that rebasing just re-writes history Rebasing fast-forwards when the tip of the branch is a descendent of the tip of the upstream. In other cases it re-writes the history. This re-write is what actually leads the user to 'force' update the remote. So, clarify that a user has to force update only when the history is re-written regardless of whether the branch was fast-forwarded. * Attribute both the authors of the second edition of Pro Git * Try a different form of emphasizing text Capitalizing words seems to be over emphasizing words. Italicize the words, instead to see if works. * Mention what 'upstream' means for the sake of clarity * Simplify the way to create a remote branch from the local one The commands were needlessly complex by not considering the fact that the command defaults to HEAD when no branch is specified and changing configuration when it wasn't required. Simplify the commands to make readers more happy! * Remove a character cruft left over while editing This is an instance of a carelessly edited document getting into version control. ;) * Improve a sentence ... by, - expanding acronyms - quoting a command line parameter
2017-12-08 22:47:41 +06:00
Make 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):
```sh
$ git update-ref refs/tags/<tag_name> <hash>
```
Your tag should now have been restored.
2015-05-19 13:23:25 +06:00
<a name="deleted-patch"></a>
2015-09-24 21:13:56 +06:00
### Deleted Patch
2015-05-19 13:23:25 +06:00
If 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:
2015-05-19 13:23:25 +06:00
```sh
$ git fetch origin refs/pull/1/head:pr_1
From github.com:foo/bar
* [new ref] refs/pull/1/head -> pr_1
```
2015-05-19 23:40:03 +06:00
### Exporting a repository as a Zip file
```sh
$ git archive --format zip --output /full/path/to/zipfile.zip master
```
2015-09-24 21:13:56 +06:00
## Tracking Files
2015-09-24 21:13:56 +06:00
<a href="i-want-to-change-a-file-names-capitalization-without-changing-the-contents-of-the-file"></a>
A few cleanups (#163) * Consolidate the name of the subsystem The name of the subsytem is considered to be 'Git' and not 'git'. There was inconsistency in the document by referring to the subsystem using both 'git' and 'Git'. Consolidate the usages to 'Git' which is generally considered to be the name of the subsystem. * Consolidate the name of GitHub They website is generally called 'GitHub' and not 'github'. * Quote the git commands correctly * Avoid fullstops in section names This is done for the sake of consistency. Most of the section names don't have a fullstop at the end. So, ... * Update the ToC The Table of Contents seems to have been out of date with the section titles. So, update the ToC with 'doctoc'. * Clarify that the changes are removed only for the previous commit * Showcase the flexibility of `git fetch -p` The example for that exhibits the way to 'prune' remote branches that were deleted upstream wasn't flexible as it relied on the command defaulting to the upstream of the current branch. This might lead the reader into overlooking the flexibility of the `git fetch`. Show that the 'upstream' can be mentioned in the command thus show casing the flexibility of `git fetch`. * Exemplify the safer version of branch deletion It's not good for newbies to start using 'force deletion' when they want to delete a branch as it might lead them to them into 'accidentally' deleting their branches often without merging them into other branches or pushing them to an upstream. So, exemplify the safer version of branch deletion (branch -d) and warn them about what `git branch -D` does. * Improve readability of a few phrases It's not required to use 'git' a lot as this a document about Git, after all. * Use HTTPS links for sites that serve using HTTPS * Clarify that rebasing just re-writes history Rebasing fast-forwards when the tip of the branch is a descendent of the tip of the upstream. In other cases it re-writes the history. This re-write is what actually leads the user to 'force' update the remote. So, clarify that a user has to force update only when the history is re-written regardless of whether the branch was fast-forwarded. * Attribute both the authors of the second edition of Pro Git * Try a different form of emphasizing text Capitalizing words seems to be over emphasizing words. Italicize the words, instead to see if works. * Mention what 'upstream' means for the sake of clarity * Simplify the way to create a remote branch from the local one The commands were needlessly complex by not considering the fact that the command defaults to HEAD when no branch is specified and changing configuration when it wasn't required. Simplify the commands to make readers more happy! * Remove a character cruft left over while editing This is an instance of a carelessly edited document getting into version control. ;) * Improve a sentence ... by, - expanding acronyms - quoting a command line parameter
2017-12-08 22:47:41 +06:00
### I want to change a file name's capitalization, without changing the contents of the file
```sh
2015-09-24 21:13:56 +06:00
(master)$ git mv --force myfile MyFile
```
<a href="i-want-to-overwrite-local-files-when-doing-a-git-pull"></a>
A few cleanups (#163) * Consolidate the name of the subsystem The name of the subsytem is considered to be 'Git' and not 'git'. There was inconsistency in the document by referring to the subsystem using both 'git' and 'Git'. Consolidate the usages to 'Git' which is generally considered to be the name of the subsystem. * Consolidate the name of GitHub They website is generally called 'GitHub' and not 'github'. * Quote the git commands correctly * Avoid fullstops in section names This is done for the sake of consistency. Most of the section names don't have a fullstop at the end. So, ... * Update the ToC The Table of Contents seems to have been out of date with the section titles. So, update the ToC with 'doctoc'. * Clarify that the changes are removed only for the previous commit * Showcase the flexibility of `git fetch -p` The example for that exhibits the way to 'prune' remote branches that were deleted upstream wasn't flexible as it relied on the command defaulting to the upstream of the current branch. This might lead the reader into overlooking the flexibility of the `git fetch`. Show that the 'upstream' can be mentioned in the command thus show casing the flexibility of `git fetch`. * Exemplify the safer version of branch deletion It's not good for newbies to start using 'force deletion' when they want to delete a branch as it might lead them to them into 'accidentally' deleting their branches often without merging them into other branches or pushing them to an upstream. So, exemplify the safer version of branch deletion (branch -d) and warn them about what `git branch -D` does. * Improve readability of a few phrases It's not required to use 'git' a lot as this a document about Git, after all. * Use HTTPS links for sites that serve using HTTPS * Clarify that rebasing just re-writes history Rebasing fast-forwards when the tip of the branch is a descendent of the tip of the upstream. In other cases it re-writes the history. This re-write is what actually leads the user to 'force' update the remote. So, clarify that a user has to force update only when the history is re-written regardless of whether the branch was fast-forwarded. * Attribute both the authors of the second edition of Pro Git * Try a different form of emphasizing text Capitalizing words seems to be over emphasizing words. Italicize the words, instead to see if works. * Mention what 'upstream' means for the sake of clarity * Simplify the way to create a remote branch from the local one The commands were needlessly complex by not considering the fact that the command defaults to HEAD when no branch is specified and changing configuration when it wasn't required. Simplify the commands to make readers more happy! * Remove a character cruft left over while editing This is an instance of a carelessly edited document getting into version control. ;) * Improve a sentence ... by, - expanding acronyms - quoting a command line parameter
2017-12-08 22:47:41 +06:00
### I want to overwrite local files when doing a git pull
```sh
(master)$ git fetch --all
(master)$ git reset --hard origin/master
```
2015-09-24 21:13:56 +06:00
<a href="remove-from-git"></a>
A few cleanups (#163) * Consolidate the name of the subsystem The name of the subsytem is considered to be 'Git' and not 'git'. There was inconsistency in the document by referring to the subsystem using both 'git' and 'Git'. Consolidate the usages to 'Git' which is generally considered to be the name of the subsystem. * Consolidate the name of GitHub They website is generally called 'GitHub' and not 'github'. * Quote the git commands correctly * Avoid fullstops in section names This is done for the sake of consistency. Most of the section names don't have a fullstop at the end. So, ... * Update the ToC The Table of Contents seems to have been out of date with the section titles. So, update the ToC with 'doctoc'. * Clarify that the changes are removed only for the previous commit * Showcase the flexibility of `git fetch -p` The example for that exhibits the way to 'prune' remote branches that were deleted upstream wasn't flexible as it relied on the command defaulting to the upstream of the current branch. This might lead the reader into overlooking the flexibility of the `git fetch`. Show that the 'upstream' can be mentioned in the command thus show casing the flexibility of `git fetch`. * Exemplify the safer version of branch deletion It's not good for newbies to start using 'force deletion' when they want to delete a branch as it might lead them to them into 'accidentally' deleting their branches often without merging them into other branches or pushing them to an upstream. So, exemplify the safer version of branch deletion (branch -d) and warn them about what `git branch -D` does. * Improve readability of a few phrases It's not required to use 'git' a lot as this a document about Git, after all. * Use HTTPS links for sites that serve using HTTPS * Clarify that rebasing just re-writes history Rebasing fast-forwards when the tip of the branch is a descendent of the tip of the upstream. In other cases it re-writes the history. This re-write is what actually leads the user to 'force' update the remote. So, clarify that a user has to force update only when the history is re-written regardless of whether the branch was fast-forwarded. * Attribute both the authors of the second edition of Pro Git * Try a different form of emphasizing text Capitalizing words seems to be over emphasizing words. Italicize the words, instead to see if works. * Mention what 'upstream' means for the sake of clarity * Simplify the way to create a remote branch from the local one The commands were needlessly complex by not considering the fact that the command defaults to HEAD when no branch is specified and changing configuration when it wasn't required. Simplify the commands to make readers more happy! * Remove a character cruft left over while editing This is an instance of a carelessly edited document getting into version control. ;) * Improve a sentence ... by, - expanding acronyms - quoting a command line parameter
2017-12-08 22:47:41 +06:00
### I want to remove a file from Git but keep the file
```sh
2015-09-24 21:13:56 +06:00
(master)$ git rm --cached log.txt
```
<a href="i-want-to-revert-a-file-to-a-specific-revision"></a>
### I want to revert a file to a specific revision
Assuming the hash of the commit you want is c5f567:
```sh
(master)$ git checkout c5f567 -- file1/to/restore file2/to/restore
```
If you want to revert to changes made just 1 commit before c5f567, pass the commit hash as c5f567~1:
```sh
(master)$ git checkout c5f567~1 -- file1/to/restore file2/to/restore
```
2015-09-24 21:13:56 +06:00
## Configuration
<a name="adding-command-aliases"></a>
A few cleanups (#163) * Consolidate the name of the subsystem The name of the subsytem is considered to be 'Git' and not 'git'. There was inconsistency in the document by referring to the subsystem using both 'git' and 'Git'. Consolidate the usages to 'Git' which is generally considered to be the name of the subsystem. * Consolidate the name of GitHub They website is generally called 'GitHub' and not 'github'. * Quote the git commands correctly * Avoid fullstops in section names This is done for the sake of consistency. Most of the section names don't have a fullstop at the end. So, ... * Update the ToC The Table of Contents seems to have been out of date with the section titles. So, update the ToC with 'doctoc'. * Clarify that the changes are removed only for the previous commit * Showcase the flexibility of `git fetch -p` The example for that exhibits the way to 'prune' remote branches that were deleted upstream wasn't flexible as it relied on the command defaulting to the upstream of the current branch. This might lead the reader into overlooking the flexibility of the `git fetch`. Show that the 'upstream' can be mentioned in the command thus show casing the flexibility of `git fetch`. * Exemplify the safer version of branch deletion It's not good for newbies to start using 'force deletion' when they want to delete a branch as it might lead them to them into 'accidentally' deleting their branches often without merging them into other branches or pushing them to an upstream. So, exemplify the safer version of branch deletion (branch -d) and warn them about what `git branch -D` does. * Improve readability of a few phrases It's not required to use 'git' a lot as this a document about Git, after all. * Use HTTPS links for sites that serve using HTTPS * Clarify that rebasing just re-writes history Rebasing fast-forwards when the tip of the branch is a descendent of the tip of the upstream. In other cases it re-writes the history. This re-write is what actually leads the user to 'force' update the remote. So, clarify that a user has to force update only when the history is re-written regardless of whether the branch was fast-forwarded. * Attribute both the authors of the second edition of Pro Git * Try a different form of emphasizing text Capitalizing words seems to be over emphasizing words. Italicize the words, instead to see if works. * Mention what 'upstream' means for the sake of clarity * Simplify the way to create a remote branch from the local one The commands were needlessly complex by not considering the fact that the command defaults to HEAD when no branch is specified and changing configuration when it wasn't required. Simplify the commands to make readers more happy! * Remove a character cruft left over while editing This is an instance of a carelessly edited document getting into version control. ;) * Improve a sentence ... by, - expanding acronyms - quoting a command line parameter
2017-12-08 22:47:41 +06:00
### I want to add aliases for some Git commands
2015-09-24 21:13:56 +06:00
On 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:
2015-09-24 21:13:56 +06:00
```vim
[alias]
2015-09-24 21:13:56 +06:00
a = add
2016-11-12 21:11:50 +06:00
amend = commit --amend
2015-09-24 21:13:56 +06:00
c = commit
ca = commit --amend
ci = commit -a
co = checkout
d = diff
dc = diff --changed
ds = diff --staged
f = fetch
loll = log --graph --decorate --pretty=oneline --abbrev-commit
m = merge
one = log --pretty=oneline
outstanding = rebase -i @{u}
s = status
unpushed = log @{u}
wc = whatchanged
wip = rebase -i @{u}
zap = fetch -p
```
<a name="adding-empty-repository"></a>
### I want to add an empty directory to my repository
You cant! Git doesnt support this, but theres a hack. You can create a .gitignore file in the directory with the following contents:
```
# Ignore everything in this directory
*
# Except this file
!.gitignore
```
Another common convention is to make an empty file in the folder, titled .gitkeep.
```sh
$ mkdir mydir
$ touch mydir/.gitkeep
```
You can also name the file as just .keep , in which case the second line above would be ```touch mydir/.keep```
<a name="credential-helper"></a>
### I want to cache a username and password for a repository
You 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 / pull. Credential helper can do this for you.
```sh
$ git config --global credential.helper cache
# Set git to use the credential memory cache
```
```sh
$ git config --global credential.helper 'cache --timeout=3600'
# Set the cache to timeout after 1 hour (setting is in seconds)
```
2015-09-24 21:13:56 +06:00
<a name="i-want-to-make-git-ignore-permissions-and-filemode-changes"></a>
### I want to make Git ignore permissions and filemode changes
```sh
$ git config core.fileMode false
```
If you want to make this the default behaviour for logged-in users, then use:
```sh
$ git config --global core.fileMode false
```
<a href="#ive-no-idea-what-i-did-wrong"></a>
## I've no idea what I did wrong
So, 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.
This 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).
```sh
(master)$ git reflog
0a2e358 HEAD@{0}: reset: moving to HEAD~2
0254ea7 HEAD@{1}: checkout: moving from 2.2 to master
c10f740 HEAD@{2}: checkout: moving from master to 2.2
```
The reflog above shows a checkout from master 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}`.
2016-11-12 21:10:29 +06:00
If it turns out that you accidentally moved back, the reflog will contain the commit master pointed to (0254ea7) before you accidentally dropped 2 commits.
```sh
$ git reset --hard 0254ea7
```
A few cleanups (#163) * Consolidate the name of the subsystem The name of the subsytem is considered to be 'Git' and not 'git'. There was inconsistency in the document by referring to the subsystem using both 'git' and 'Git'. Consolidate the usages to 'Git' which is generally considered to be the name of the subsystem. * Consolidate the name of GitHub They website is generally called 'GitHub' and not 'github'. * Quote the git commands correctly * Avoid fullstops in section names This is done for the sake of consistency. Most of the section names don't have a fullstop at the end. So, ... * Update the ToC The Table of Contents seems to have been out of date with the section titles. So, update the ToC with 'doctoc'. * Clarify that the changes are removed only for the previous commit * Showcase the flexibility of `git fetch -p` The example for that exhibits the way to 'prune' remote branches that were deleted upstream wasn't flexible as it relied on the command defaulting to the upstream of the current branch. This might lead the reader into overlooking the flexibility of the `git fetch`. Show that the 'upstream' can be mentioned in the command thus show casing the flexibility of `git fetch`. * Exemplify the safer version of branch deletion It's not good for newbies to start using 'force deletion' when they want to delete a branch as it might lead them to them into 'accidentally' deleting their branches often without merging them into other branches or pushing them to an upstream. So, exemplify the safer version of branch deletion (branch -d) and warn them about what `git branch -D` does. * Improve readability of a few phrases It's not required to use 'git' a lot as this a document about Git, after all. * Use HTTPS links for sites that serve using HTTPS * Clarify that rebasing just re-writes history Rebasing fast-forwards when the tip of the branch is a descendent of the tip of the upstream. In other cases it re-writes the history. This re-write is what actually leads the user to 'force' update the remote. So, clarify that a user has to force update only when the history is re-written regardless of whether the branch was fast-forwarded. * Attribute both the authors of the second edition of Pro Git * Try a different form of emphasizing text Capitalizing words seems to be over emphasizing words. Italicize the words, instead to see if works. * Mention what 'upstream' means for the sake of clarity * Simplify the way to create a remote branch from the local one The commands were needlessly complex by not considering the fact that the command defaults to HEAD when no branch is specified and changing configuration when it wasn't required. Simplify the commands to make readers more happy! * Remove a character cruft left over while editing This is an instance of a carelessly edited document getting into version control. ;) * Improve a sentence ... by, - expanding acronyms - quoting a command line parameter
2017-12-08 22:47:41 +06:00
Using `git reset` it is then possible to change master back to the commit it was before. This provides a safety net in case history was accidentally changed.
(copied and edited from [Source](https://www.atlassian.com/git/tutorials/rewriting-history/git-reflog)).
# Other Resources
2015-05-22 08:02:08 +06:00
## Books
A few cleanups (#163) * Consolidate the name of the subsystem The name of the subsytem is considered to be 'Git' and not 'git'. There was inconsistency in the document by referring to the subsystem using both 'git' and 'Git'. Consolidate the usages to 'Git' which is generally considered to be the name of the subsystem. * Consolidate the name of GitHub They website is generally called 'GitHub' and not 'github'. * Quote the git commands correctly * Avoid fullstops in section names This is done for the sake of consistency. Most of the section names don't have a fullstop at the end. So, ... * Update the ToC The Table of Contents seems to have been out of date with the section titles. So, update the ToC with 'doctoc'. * Clarify that the changes are removed only for the previous commit * Showcase the flexibility of `git fetch -p` The example for that exhibits the way to 'prune' remote branches that were deleted upstream wasn't flexible as it relied on the command defaulting to the upstream of the current branch. This might lead the reader into overlooking the flexibility of the `git fetch`. Show that the 'upstream' can be mentioned in the command thus show casing the flexibility of `git fetch`. * Exemplify the safer version of branch deletion It's not good for newbies to start using 'force deletion' when they want to delete a branch as it might lead them to them into 'accidentally' deleting their branches often without merging them into other branches or pushing them to an upstream. So, exemplify the safer version of branch deletion (branch -d) and warn them about what `git branch -D` does. * Improve readability of a few phrases It's not required to use 'git' a lot as this a document about Git, after all. * Use HTTPS links for sites that serve using HTTPS * Clarify that rebasing just re-writes history Rebasing fast-forwards when the tip of the branch is a descendent of the tip of the upstream. In other cases it re-writes the history. This re-write is what actually leads the user to 'force' update the remote. So, clarify that a user has to force update only when the history is re-written regardless of whether the branch was fast-forwarded. * Attribute both the authors of the second edition of Pro Git * Try a different form of emphasizing text Capitalizing words seems to be over emphasizing words. Italicize the words, instead to see if works. * Mention what 'upstream' means for the sake of clarity * Simplify the way to create a remote branch from the local one The commands were needlessly complex by not considering the fact that the command defaults to HEAD when no branch is specified and changing configuration when it wasn't required. Simplify the commands to make readers more happy! * Remove a character cruft left over while editing This is an instance of a carelessly edited document getting into version control. ;) * Improve a sentence ... by, - expanding acronyms - quoting a command line parameter
2017-12-08 22:47:41 +06:00
* [Pro Git](https://git-scm.com/book/en/v2) - Scott Chacon and Ben Straub's excellent book about Git
* [Git Internals](https://github.com/pluralsight/git-internals-pdf) - Scott Chacon's other excellent book about Git
2015-05-22 08:26:54 +06:00
## Tutorials
* [Atlassian's Git tutorial](https://www.atlassian.com/git/tutorials) Get Git right with tutorials from beginner to advanced.
2017-11-07 06:22:35 +06:00
* [Learn Git branching](https://learngitbranching.js.org/) An interactive web based branching/merging/rebasing tutorial
2015-06-30 23:29:52 +06:00
* [Getting solid at Git rebase vs. merge](https://medium.com/@porteneuve/getting-solid-at-git-rebase-vs-merge-4fa1a48c53aa)
A few cleanups (#163) * Consolidate the name of the subsystem The name of the subsytem is considered to be 'Git' and not 'git'. There was inconsistency in the document by referring to the subsystem using both 'git' and 'Git'. Consolidate the usages to 'Git' which is generally considered to be the name of the subsystem. * Consolidate the name of GitHub They website is generally called 'GitHub' and not 'github'. * Quote the git commands correctly * Avoid fullstops in section names This is done for the sake of consistency. Most of the section names don't have a fullstop at the end. So, ... * Update the ToC The Table of Contents seems to have been out of date with the section titles. So, update the ToC with 'doctoc'. * Clarify that the changes are removed only for the previous commit * Showcase the flexibility of `git fetch -p` The example for that exhibits the way to 'prune' remote branches that were deleted upstream wasn't flexible as it relied on the command defaulting to the upstream of the current branch. This might lead the reader into overlooking the flexibility of the `git fetch`. Show that the 'upstream' can be mentioned in the command thus show casing the flexibility of `git fetch`. * Exemplify the safer version of branch deletion It's not good for newbies to start using 'force deletion' when they want to delete a branch as it might lead them to them into 'accidentally' deleting their branches often without merging them into other branches or pushing them to an upstream. So, exemplify the safer version of branch deletion (branch -d) and warn them about what `git branch -D` does. * Improve readability of a few phrases It's not required to use 'git' a lot as this a document about Git, after all. * Use HTTPS links for sites that serve using HTTPS * Clarify that rebasing just re-writes history Rebasing fast-forwards when the tip of the branch is a descendent of the tip of the upstream. In other cases it re-writes the history. This re-write is what actually leads the user to 'force' update the remote. So, clarify that a user has to force update only when the history is re-written regardless of whether the branch was fast-forwarded. * Attribute both the authors of the second edition of Pro Git * Try a different form of emphasizing text Capitalizing words seems to be over emphasizing words. Italicize the words, instead to see if works. * Mention what 'upstream' means for the sake of clarity * Simplify the way to create a remote branch from the local one The commands were needlessly complex by not considering the fact that the command defaults to HEAD when no branch is specified and changing configuration when it wasn't required. Simplify the commands to make readers more happy! * Remove a character cruft left over while editing This is an instance of a carelessly edited document getting into version control. ;) * Improve a sentence ... by, - expanding acronyms - quoting a command line parameter
2017-12-08 22:47:41 +06:00
* [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
* [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
* [Githug](https://github.com/Gazler/githug) - A game to learn more common Git workflows
2015-09-24 21:13:56 +06:00
## Scripts and Tools
2015-05-19 23:47:29 +06:00
* [firstaidgit.io](http://firstaidgit.io/) A searchable selection of the most frequently asked Git questions
A few cleanups (#163) * Consolidate the name of the subsystem The name of the subsytem is considered to be 'Git' and not 'git'. There was inconsistency in the document by referring to the subsystem using both 'git' and 'Git'. Consolidate the usages to 'Git' which is generally considered to be the name of the subsystem. * Consolidate the name of GitHub They website is generally called 'GitHub' and not 'github'. * Quote the git commands correctly * Avoid fullstops in section names This is done for the sake of consistency. Most of the section names don't have a fullstop at the end. So, ... * Update the ToC The Table of Contents seems to have been out of date with the section titles. So, update the ToC with 'doctoc'. * Clarify that the changes are removed only for the previous commit * Showcase the flexibility of `git fetch -p` The example for that exhibits the way to 'prune' remote branches that were deleted upstream wasn't flexible as it relied on the command defaulting to the upstream of the current branch. This might lead the reader into overlooking the flexibility of the `git fetch`. Show that the 'upstream' can be mentioned in the command thus show casing the flexibility of `git fetch`. * Exemplify the safer version of branch deletion It's not good for newbies to start using 'force deletion' when they want to delete a branch as it might lead them to them into 'accidentally' deleting their branches often without merging them into other branches or pushing them to an upstream. So, exemplify the safer version of branch deletion (branch -d) and warn them about what `git branch -D` does. * Improve readability of a few phrases It's not required to use 'git' a lot as this a document about Git, after all. * Use HTTPS links for sites that serve using HTTPS * Clarify that rebasing just re-writes history Rebasing fast-forwards when the tip of the branch is a descendent of the tip of the upstream. In other cases it re-writes the history. This re-write is what actually leads the user to 'force' update the remote. So, clarify that a user has to force update only when the history is re-written regardless of whether the branch was fast-forwarded. * Attribute both the authors of the second edition of Pro Git * Try a different form of emphasizing text Capitalizing words seems to be over emphasizing words. Italicize the words, instead to see if works. * Mention what 'upstream' means for the sake of clarity * Simplify the way to create a remote branch from the local one The commands were needlessly complex by not considering the fact that the command defaults to HEAD when no branch is specified and changing configuration when it wasn't required. Simplify the commands to make readers more happy! * Remove a character cruft left over while editing This is an instance of a carelessly edited document getting into version control. ;) * Improve a sentence ... by, - expanding acronyms - quoting a command line parameter
2017-12-08 22:47:41 +06:00
* [git-extra-commands](https://github.com/unixorn/git-extra-commands) - a collection of useful extra Git scripts
2015-07-16 18:05:27 +06:00
* [git-extras](https://github.com/tj/git-extras) - GIT utilities -- repo summary, repl, changelog population, author commit percentages and more
2015-10-05 20:40:27 +06:00
* [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).
A few cleanups (#163) * Consolidate the name of the subsystem The name of the subsytem is considered to be 'Git' and not 'git'. There was inconsistency in the document by referring to the subsystem using both 'git' and 'Git'. Consolidate the usages to 'Git' which is generally considered to be the name of the subsystem. * Consolidate the name of GitHub They website is generally called 'GitHub' and not 'github'. * Quote the git commands correctly * Avoid fullstops in section names This is done for the sake of consistency. Most of the section names don't have a fullstop at the end. So, ... * Update the ToC The Table of Contents seems to have been out of date with the section titles. So, update the ToC with 'doctoc'. * Clarify that the changes are removed only for the previous commit * Showcase the flexibility of `git fetch -p` The example for that exhibits the way to 'prune' remote branches that were deleted upstream wasn't flexible as it relied on the command defaulting to the upstream of the current branch. This might lead the reader into overlooking the flexibility of the `git fetch`. Show that the 'upstream' can be mentioned in the command thus show casing the flexibility of `git fetch`. * Exemplify the safer version of branch deletion It's not good for newbies to start using 'force deletion' when they want to delete a branch as it might lead them to them into 'accidentally' deleting their branches often without merging them into other branches or pushing them to an upstream. So, exemplify the safer version of branch deletion (branch -d) and warn them about what `git branch -D` does. * Improve readability of a few phrases It's not required to use 'git' a lot as this a document about Git, after all. * Use HTTPS links for sites that serve using HTTPS * Clarify that rebasing just re-writes history Rebasing fast-forwards when the tip of the branch is a descendent of the tip of the upstream. In other cases it re-writes the history. This re-write is what actually leads the user to 'force' update the remote. So, clarify that a user has to force update only when the history is re-written regardless of whether the branch was fast-forwarded. * Attribute both the authors of the second edition of Pro Git * Try a different form of emphasizing text Capitalizing words seems to be over emphasizing words. Italicize the words, instead to see if works. * Mention what 'upstream' means for the sake of clarity * Simplify the way to create a remote branch from the local one The commands were needlessly complex by not considering the fact that the command defaults to HEAD when no branch is specified and changing configuration when it wasn't required. Simplify the commands to make readers more happy! * Remove a character cruft left over while editing This is an instance of a carelessly edited document getting into version control. ;) * Improve a sentence ... by, - expanding acronyms - quoting a command line parameter
2017-12-08 22:47:41 +06:00
* [git-tips](https://github.com/git-tips/tips) - Small Git tips
2017-05-24 19:50:55 +06:00
* [git-town](https://github.com/Originate/git-town) - Generic, high-level Git workflow support! http://www.git-town.com
2015-05-25 11:35:34 +06:00
## GUI Clients
2017-01-12 03:46:43 +06:00
* [GitKraken](https://www.gitkraken.com/) - The downright luxurious Git client,for Windows, Mac & Linux
A few cleanups (#163) * Consolidate the name of the subsystem The name of the subsytem is considered to be 'Git' and not 'git'. There was inconsistency in the document by referring to the subsystem using both 'git' and 'Git'. Consolidate the usages to 'Git' which is generally considered to be the name of the subsystem. * Consolidate the name of GitHub They website is generally called 'GitHub' and not 'github'. * Quote the git commands correctly * Avoid fullstops in section names This is done for the sake of consistency. Most of the section names don't have a fullstop at the end. So, ... * Update the ToC The Table of Contents seems to have been out of date with the section titles. So, update the ToC with 'doctoc'. * Clarify that the changes are removed only for the previous commit * Showcase the flexibility of `git fetch -p` The example for that exhibits the way to 'prune' remote branches that were deleted upstream wasn't flexible as it relied on the command defaulting to the upstream of the current branch. This might lead the reader into overlooking the flexibility of the `git fetch`. Show that the 'upstream' can be mentioned in the command thus show casing the flexibility of `git fetch`. * Exemplify the safer version of branch deletion It's not good for newbies to start using 'force deletion' when they want to delete a branch as it might lead them to them into 'accidentally' deleting their branches often without merging them into other branches or pushing them to an upstream. So, exemplify the safer version of branch deletion (branch -d) and warn them about what `git branch -D` does. * Improve readability of a few phrases It's not required to use 'git' a lot as this a document about Git, after all. * Use HTTPS links for sites that serve using HTTPS * Clarify that rebasing just re-writes history Rebasing fast-forwards when the tip of the branch is a descendent of the tip of the upstream. In other cases it re-writes the history. This re-write is what actually leads the user to 'force' update the remote. So, clarify that a user has to force update only when the history is re-written regardless of whether the branch was fast-forwarded. * Attribute both the authors of the second edition of Pro Git * Try a different form of emphasizing text Capitalizing words seems to be over emphasizing words. Italicize the words, instead to see if works. * Mention what 'upstream' means for the sake of clarity * Simplify the way to create a remote branch from the local one The commands were needlessly complex by not considering the fact that the command defaults to HEAD when no branch is specified and changing configuration when it wasn't required. Simplify the commands to make readers more happy! * Remove a character cruft left over while editing This is an instance of a carelessly edited document getting into version control. ;) * Improve a sentence ... by, - expanding acronyms - quoting a command line parameter
2017-12-08 22:47:41 +06:00
* [git-cola](https://git-cola.github.io/) - another Git client for Windows and OS X
* [GitUp](https://github.com/git-up/GitUp) - A newish GUI that has some very opinionated ways of dealing with Git's complications
* [gitx-dev](https://rowanj.github.io/gitx/) - another graphical Git client for OS X
* [Sourcetree](https://www.sourcetreeapp.com/) - Simplicity meets power in a beautiful and free Git GUI. For Windows and Mac.
A few cleanups (#163) * Consolidate the name of the subsystem The name of the subsytem is considered to be 'Git' and not 'git'. There was inconsistency in the document by referring to the subsystem using both 'git' and 'Git'. Consolidate the usages to 'Git' which is generally considered to be the name of the subsystem. * Consolidate the name of GitHub They website is generally called 'GitHub' and not 'github'. * Quote the git commands correctly * Avoid fullstops in section names This is done for the sake of consistency. Most of the section names don't have a fullstop at the end. So, ... * Update the ToC The Table of Contents seems to have been out of date with the section titles. So, update the ToC with 'doctoc'. * Clarify that the changes are removed only for the previous commit * Showcase the flexibility of `git fetch -p` The example for that exhibits the way to 'prune' remote branches that were deleted upstream wasn't flexible as it relied on the command defaulting to the upstream of the current branch. This might lead the reader into overlooking the flexibility of the `git fetch`. Show that the 'upstream' can be mentioned in the command thus show casing the flexibility of `git fetch`. * Exemplify the safer version of branch deletion It's not good for newbies to start using 'force deletion' when they want to delete a branch as it might lead them to them into 'accidentally' deleting their branches often without merging them into other branches or pushing them to an upstream. So, exemplify the safer version of branch deletion (branch -d) and warn them about what `git branch -D` does. * Improve readability of a few phrases It's not required to use 'git' a lot as this a document about Git, after all. * Use HTTPS links for sites that serve using HTTPS * Clarify that rebasing just re-writes history Rebasing fast-forwards when the tip of the branch is a descendent of the tip of the upstream. In other cases it re-writes the history. This re-write is what actually leads the user to 'force' update the remote. So, clarify that a user has to force update only when the history is re-written regardless of whether the branch was fast-forwarded. * Attribute both the authors of the second edition of Pro Git * Try a different form of emphasizing text Capitalizing words seems to be over emphasizing words. Italicize the words, instead to see if works. * Mention what 'upstream' means for the sake of clarity * Simplify the way to create a remote branch from the local one The commands were needlessly complex by not considering the fact that the command defaults to HEAD when no branch is specified and changing configuration when it wasn't required. Simplify the commands to make readers more happy! * Remove a character cruft left over while editing This is an instance of a carelessly edited document getting into version control. ;) * Improve a sentence ... by, - expanding acronyms - quoting a command line parameter
2017-12-08 22:47:41 +06:00
* [Tower](https://www.git-tower.com/) - graphical Git client for OS X (paid)
* [tig](https://jonas.github.io/tig/) - terminal text-mode interface for Git
A few cleanups (#163) * Consolidate the name of the subsystem The name of the subsytem is considered to be 'Git' and not 'git'. There was inconsistency in the document by referring to the subsystem using both 'git' and 'Git'. Consolidate the usages to 'Git' which is generally considered to be the name of the subsystem. * Consolidate the name of GitHub They website is generally called 'GitHub' and not 'github'. * Quote the git commands correctly * Avoid fullstops in section names This is done for the sake of consistency. Most of the section names don't have a fullstop at the end. So, ... * Update the ToC The Table of Contents seems to have been out of date with the section titles. So, update the ToC with 'doctoc'. * Clarify that the changes are removed only for the previous commit * Showcase the flexibility of `git fetch -p` The example for that exhibits the way to 'prune' remote branches that were deleted upstream wasn't flexible as it relied on the command defaulting to the upstream of the current branch. This might lead the reader into overlooking the flexibility of the `git fetch`. Show that the 'upstream' can be mentioned in the command thus show casing the flexibility of `git fetch`. * Exemplify the safer version of branch deletion It's not good for newbies to start using 'force deletion' when they want to delete a branch as it might lead them to them into 'accidentally' deleting their branches often without merging them into other branches or pushing them to an upstream. So, exemplify the safer version of branch deletion (branch -d) and warn them about what `git branch -D` does. * Improve readability of a few phrases It's not required to use 'git' a lot as this a document about Git, after all. * Use HTTPS links for sites that serve using HTTPS * Clarify that rebasing just re-writes history Rebasing fast-forwards when the tip of the branch is a descendent of the tip of the upstream. In other cases it re-writes the history. This re-write is what actually leads the user to 'force' update the remote. So, clarify that a user has to force update only when the history is re-written regardless of whether the branch was fast-forwarded. * Attribute both the authors of the second edition of Pro Git * Try a different form of emphasizing text Capitalizing words seems to be over emphasizing words. Italicize the words, instead to see if works. * Mention what 'upstream' means for the sake of clarity * Simplify the way to create a remote branch from the local one The commands were needlessly complex by not considering the fact that the command defaults to HEAD when no branch is specified and changing configuration when it wasn't required. Simplify the commands to make readers more happy! * Remove a character cruft left over while editing This is an instance of a carelessly edited document getting into version control. ;) * Improve a sentence ... by, - expanding acronyms - quoting a command line parameter
2017-12-08 22:47:41 +06:00
* [Magit](https://magit.vc/) - Interface to Git implemented as an Emacs package.
2017-11-11 03:41:56 +06:00
* [GitExtensions](https://github.com/gitextensions/gitextensions) - a shell extension, a Visual Studio 2010-2015 plugin and a standalone Git repository tool.
A few cleanups (#163) * Consolidate the name of the subsystem The name of the subsytem is considered to be 'Git' and not 'git'. There was inconsistency in the document by referring to the subsystem using both 'git' and 'Git'. Consolidate the usages to 'Git' which is generally considered to be the name of the subsystem. * Consolidate the name of GitHub They website is generally called 'GitHub' and not 'github'. * Quote the git commands correctly * Avoid fullstops in section names This is done for the sake of consistency. Most of the section names don't have a fullstop at the end. So, ... * Update the ToC The Table of Contents seems to have been out of date with the section titles. So, update the ToC with 'doctoc'. * Clarify that the changes are removed only for the previous commit * Showcase the flexibility of `git fetch -p` The example for that exhibits the way to 'prune' remote branches that were deleted upstream wasn't flexible as it relied on the command defaulting to the upstream of the current branch. This might lead the reader into overlooking the flexibility of the `git fetch`. Show that the 'upstream' can be mentioned in the command thus show casing the flexibility of `git fetch`. * Exemplify the safer version of branch deletion It's not good for newbies to start using 'force deletion' when they want to delete a branch as it might lead them to them into 'accidentally' deleting their branches often without merging them into other branches or pushing them to an upstream. So, exemplify the safer version of branch deletion (branch -d) and warn them about what `git branch -D` does. * Improve readability of a few phrases It's not required to use 'git' a lot as this a document about Git, after all. * Use HTTPS links for sites that serve using HTTPS * Clarify that rebasing just re-writes history Rebasing fast-forwards when the tip of the branch is a descendent of the tip of the upstream. In other cases it re-writes the history. This re-write is what actually leads the user to 'force' update the remote. So, clarify that a user has to force update only when the history is re-written regardless of whether the branch was fast-forwarded. * Attribute both the authors of the second edition of Pro Git * Try a different form of emphasizing text Capitalizing words seems to be over emphasizing words. Italicize the words, instead to see if works. * Mention what 'upstream' means for the sake of clarity * Simplify the way to create a remote branch from the local one The commands were needlessly complex by not considering the fact that the command defaults to HEAD when no branch is specified and changing configuration when it wasn't required. Simplify the commands to make readers more happy! * Remove a character cruft left over while editing This is an instance of a carelessly edited document getting into version control. ;) * Improve a sentence ... by, - expanding acronyms - quoting a command line parameter
2017-12-08 22:47:41 +06:00
* [Fork](https://git-fork.com/) - a fast and friendly Git client for Mac (beta)
* [gmaster](https://gmaster.io/) - a Git client for Windows that has 3-way merge, analyze refactors, semantic diff and merge (beta)
* [gitk](https://git-scm.com/docs/gitk) - a Git client for linux to allow simple view of repo state.