Made a deleting category

This commit is contained in:
Richard Littauer 2015-03-23 11:15:55 +08:00
parent 08ced44cd3
commit caf8d3c8b4
1 changed files with 96 additions and 93 deletions

189
README.md
View File

@ -33,19 +33,19 @@ For clarity's sake all examples in this document use customized bash prompt in o
- [I committed with the wrong name and email configured](#i-committed-with-the-wrong-name-and-email-configured)
- [I committed to master instead of a new branch](#i-committed-to-master-instead-of-a-new-branch)
- [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 add aliases for some git commands](#i-want-to-add-aliases-for-some-git-commands)
- [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 want to discard my local, uncommitted changes](#i-want-to-discard-my-local-uncommitted-changes)
- [I want to add changes in one file to two different commits](#i-want-to-add-changes-in-one-file-to-two-different-commits)
- [I want to remove a file from git but keep the file](#i-want-to-remove-a-file-from-git-but-keep-the-file)
- [Deleting Commits and Tags](#deleting-commits-and-tags)
- [Clone all submodules](#clone-all-submodules)
- [Deleting Objects](#deleting-objects)
- [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)
- [Delete/remove last pushed commit](#deleteremove-last-pushed-commit)
- [Delete/remove arbitrary commit](#deleteremove-arbitrary-commit)
- [Delete tag](#delete-tag)
- [Clone all submodules](#clone-all-submodules)
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
@ -356,84 +356,6 @@ And finally, let's cherry-pick the commit for bug #14:
(14)$ git cherry-pick 5ea5173
```
<a name="delete-stale-local-branches">
## I want to delete local branches that were deleted upstream
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.
```bash
$ git fetch -p
```
<a name='restore-a-deleted-branch'>
## 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:
```
(master)$ git checkout -b branch-1
(branch-1)$ git branch
(branch-1)$ touch foo.txt
(branch-1)$ ls
README.md foo.txt
```
Let's add it and commit.
```
(branch-1)$ git add .
(branch-1)$ git commit -m 'foo.txt added'
(branch-1)$ foo.txt added
1 files changed, 1 insertions(+)
create mode 100644 foo.txt
(branch-1)$ git log
commit 4e3cd85a670ced7cc17a2b5d8d3d809ac88d5012
Author: siemiatj <kuba@saucelabs.com>
Date: Wed Jul 30 00:34:10 2014 +0200
foo.txt added
commit 69204cdf0acbab201619d95ad8295928e7f411d5
Author: Kate Hudson <k88hudson@gmail.com>
Date: Tue Jul 29 13:14:46 2014 -0400
Fixes #6: Force pushing after amending commits
```
Now we're switching back to master and 'accidentaly' removing our branch.
```
(branch-1)$ git checkout master
Switched to branch 'master'
Your branch is up-to-date with 'origin/master'.
(master)$ git branch -D branch-1
Deleted branch branch-1 (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
69204cd HEAD@{0}: checkout: moving from branch-1 to master
4e3cd85 HEAD@{1}: commit: foo.txt added
69204cd HEAD@{2}: checkout: moving from master to branch-1
```
As you can see we have commit hash from our deleted branch. Let's see if we can restore our deleted branch.
```
(master)$ git checkout -b branch-1-help
Switched to a new branch 'branch-1-help'
(branch-1-help)$ git reset --hard 4e3cd85
HEAD is now at 4e3cd85 foo.txt added
(branch-1-help)$ ls
README.md foo.txt
```
Voila! We got our removed file back. Git reflog is also useful when rebasing goes terribly wrong.
<a name="adding-command-aliases"></a>
## I want to add aliases for some git commands
@ -534,14 +456,101 @@ If you want to only reset to some commit between origin and your local, you can
(master)$ git rm --cached log.txt
```
<a name="deleting"></a>
## Deleting Commits and Tags
<a name="clone-submodules"></a>
## Clone all submodules
If you need to delete pushed commits, you can use the following. However, it will irreversabily change your history, and mess up the history of anyone else who had already pulled from the repository. In short, you should never do this, ever.
```sh
git clone --recursive git://github.com/foo/bar.git
# If already cloned:
git submodule update --init --recursive
```
<a name="deleting"></a>
## Deleting Objects
<a name="delete-stale-local-branches">
### I want to delete local branches that were deleted upstream
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.
```bash
$ git fetch -p
```
<a name='restore-a-deleted-branch'>
### 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:
```
(master)$ git checkout -b branch-1
(branch-1)$ git branch
(branch-1)$ touch foo.txt
(branch-1)$ ls
README.md foo.txt
```
Let's add it and commit.
```
(branch-1)$ git add .
(branch-1)$ git commit -m 'foo.txt added'
(branch-1)$ foo.txt added
1 files changed, 1 insertions(+)
create mode 100644 foo.txt
(branch-1)$ git log
commit 4e3cd85a670ced7cc17a2b5d8d3d809ac88d5012
Author: siemiatj <kuba@saucelabs.com>
Date: Wed Jul 30 00:34:10 2014 +0200
foo.txt added
commit 69204cdf0acbab201619d95ad8295928e7f411d5
Author: Kate Hudson <k88hudson@gmail.com>
Date: Tue Jul 29 13:14:46 2014 -0400
Fixes #6: Force pushing after amending commits
```
Now we're switching back to master and 'accidentaly' removing our branch.
```
(branch-1)$ git checkout master
Switched to branch 'master'
Your branch is up-to-date with 'origin/master'.
(master)$ git branch -D branch-1
Deleted branch branch-1 (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
69204cd HEAD@{0}: checkout: moving from branch-1 to master
4e3cd85 HEAD@{1}: commit: foo.txt added
69204cd HEAD@{2}: checkout: moving from master to branch-1
```
As you can see we have commit hash from our deleted branch. Let's see if we can restore our deleted branch.
```
(master)$ git checkout -b branch-1-help
Switched to a new branch 'branch-1-help'
(branch-1-help)$ git reset --hard 4e3cd85
HEAD is now at 4e3cd85 foo.txt added
(branch-1-help)$ ls
README.md foo.txt
```
Voila! We got our removed file back. Git reflog is also useful when rebasing goes terribly wrong.
<a name="delete-pushed-commit"></a>
### Delete/remove last pushed commit
If you need to delete pushed commits, you can use the following. However, it will irreversabily 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.
```sh
git reset HEAD^ --hard
git push -f [remote] [branch]
@ -550,6 +559,8 @@ git push -f [remote] [branch]
<a name="delete-any-commit"></a>
### Delete/remove arbitrary commit
The same warning applies as above. Never do this if possible.
```sh
git rebase --onto SHA1_OF_BAD_COMMIT^ SHA1_OF_BAD_COMMIT
git push -f [remote] [branch]
@ -563,11 +574,3 @@ git tag -d <tag_name>
git push <remote> :refs/tags/<tag_name>
```
<a name="clone-submodules"></a>
## Clone all submodules
```sh
git clone --recursive git://github.com/foo/bar.git
# If already cloned:
git submodule update --init --recursive
```