Standardized branch names to my-branch, added missing dollar signs, and fixed some cases where the order wasn't right. Also added sh to all code blocks where needed, and added `vim` to blocks showing the interactive editor. This closes #76.

This commit is contained in:
Richard Littauer 2015-08-19 13:48:26 -04:00
parent bad8c7f6b0
commit 48cce8af6c
1 changed files with 84 additions and 79 deletions

163
README.md
View File

@ -67,12 +67,12 @@ For clarity's sake all examples in this document use a customized bash prompt in
## I wrote the wrong thing in a commit message
```sh
git commit --amend
$ git commit --amend
```
## I need to add staged changes to the previous commit
```
```sh
(my-branch*)$ git commit --amend
```
@ -93,7 +93,7 @@ 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 (`-f`) your changes if you have already pushed the pre-amended commit to your remote. Be careful when you do this – *always* make sure you specify a branch!
```sh
(mybranch) $ git push origin mybranch -f
(my-branch)$ git push origin mybranch -f
```
In general, **avoid force pushing**. It is best to create and push a new commit rather than force-pushing the amended commit as it has will cause conflicts in the source history for any other developer who has interacted with the branch in question or any child branches.
@ -104,10 +104,10 @@ In general, **avoid force pushing**. It is best to create and push a new commit
Unfortunately, you have to force push, if you want those changes to be reflected on the remote branch. This is because you have fast forwarded your commit, and changed git 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:
```sh
git checkout branch
git rebase -i master
git checkout master
git merge --ff-only branch
(master)$ git checkout my-branch
(my-branch)$ git rebase -i master
(my-branch)$ git checkout master
(master)$ git merge --ff-only my-branch
```
For more, see [this SO thread](http://stackoverflow.com/questions/11058312/how-can-i-use-git-rebase-without-requiring-a-forced-push).
@ -119,19 +119,19 @@ You need to do something called an interactive rebase.
If you are working in a branch that is/will become a pull-request against `master`, you can rebase against your `master` branch. Make sure the master branch is up to date, then:
```
```sh
(my-branch)$ git rebase -i master
```
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
(master)$ git rebase -i HEAD~2
```
After you run the interactive rebase command, you will see something like this in your text editor:
```
```vim
pick 01b2fd8 New awesome feature
pick b729ad5 fixup
pick e3851e8 another fix
@ -159,7 +159,7 @@ All the lines beginning with a `#` are comments, they won't affect your rebase.
If you want to **combine all your commits with the oldest (first) commit**, you should edit the letter next to each commit except the first to say `f`:
```
```vim
pick 01b2fd8 New awesome feature
f b729ad5 fixup
f e3851e8 another fix
@ -167,7 +167,7 @@ f e3851e8 another fix
If you want to combine all your commit with the oldest commit **and rename the commit**, you should additionally add an `r` next to the first commit:
```
```vim
r 01b2fd8 New awesome feature
f b729ad5 fixup
f e3851e8 another fix
@ -175,7 +175,7 @@ f e3851e8 another fix
You can then rename the commit in the next text prompt that pops up.
```
```vim
Newer, awesomer features
# Please enter the commit message for your changes. Lines starting
@ -191,19 +191,22 @@ Newer, awesomer features
If everything is successful, you should see something like this:
```
```sh
(master)$ Successfully rebased and updated refs/heads/master.
```
### Possible issues with merging
#### 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.
```sh
(master)$ git merge --no-ff --no-commit featurebranch
(master)$ git merge --no-ff --no-commit my-branch
```
#### I need to merge a branch into a single commit
```sh
(master)$ git merge --squash featurebranch
(master)$ git merge --squash my-branch
```
<a name="rebase-unpushed-commits"></a>
@ -211,7 +214,7 @@ If everything is successful, you should see something like this:
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.
```
```sh
(master)$ git rebase -i @{u}
```
@ -240,7 +243,7 @@ If you are unable to successfully complete the rebase, you may have to resolve c
First run `git status` to see which files have conflicts in them:
```
```sh
(my-branch)$ git status
On branch my-branch
Changes not staged for commit:
@ -252,7 +255,7 @@ Changes not staged for commit:
In this example, `README.md` has conflicts. Open that file and look for the following:
```
```vim
<<<<<<< HEAD
some code
=========
@ -263,19 +266,21 @@ In this example, `README.md` has conflicts. Open that file and look for the foll
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`.
Sometimes these merges are complicated and you should use a visual diff editor:
```sh
(master*)$ git mergetool -t opendiff
```
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`
```
```sh
(my-branch)$ git add README.md
(my-branch)$ git rebase --continue
```
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
(my-branch)$ git rebase --abort
```
@ -284,7 +289,7 @@ If at any time you want to stop the entire rebase and go back to the original st
If it's a single commit, amend it
```
```sh
$ git commit --amend --author "New Authorname <authoremail@mydomain.com>"
```
@ -295,9 +300,9 @@ If you need to change all of history, see the man page for 'git filter-branch'
Create the new branch while remaining on master:
```
(master)$ git checkout -b new-branch
(new-branch)$ git checkout master
```sh
(master)$ git checkout -b my-branch
(my-branch)$ git checkout master
(master)$
```
@ -321,7 +326,7 @@ HEAD is now at a13b85e
Checkout the new branch to continue working:
```sh
(master)$ git checkout new-branch
(master)$ git checkout my-branch
```
<a name="cherry-pick"></a>
@ -329,7 +334,7 @@ Checkout the new branch to continue working:
Say you are on your master branch. Running `git log`, you see you have made two commits:
```
```sh
(master)$ git log
commit e3851e817c451cc36f2e6f3049db528415e3c114
@ -355,21 +360,21 @@ Let's take note of our commit hashes for each bug (`e3851e8` for #21, `5ea5173`
First, let's reset our master branch to the correct commit (`a13b85e`):
```
```sh
(master)$ git reset --hard a13b85e
HEAD is now at a13b85e
```
Now, we can create a fresh branch for our bug #21 branch:
```
```sh
(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
(21)$ git cherry-pick e3851e8
```
@ -378,7 +383,7 @@ At this point, there is a possibility there might be conflicts. See the [**There
Now let's create a new branch for bug #14, also based on master
```
```sh
(21)$ git checkout master
(master)$ git checkout -b 14
(14)$
@ -386,7 +391,7 @@ Now let's create a new branch for bug #14, also based on master
And finally, let's cherry-pick the commit for bug #14:
```
```sh
(14)$ git cherry-pick 5ea5173
```
@ -402,7 +407,7 @@ $ git add --patch filename.x
`-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:
```sh
git add -N filename.x
$ git add -N filename.x
```
Then, you will need to use the `e` option to manually choose which lines to add. Running `git diff --cached` will show you which lines you have staged compared to which are still saved locally.
@ -412,7 +417,7 @@ Then, you will need to use the `e` option to manually choose which lines to add.
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 ```[aliases]``` section as shown below:
```
```vim
[aliases]
a = add
amend = --amend
@ -440,7 +445,7 @@ On OS X and Linux, your git configuration file is stored in ```~/.gitconfig```.
This is another chance to use `git reflog` to see where your HEAD pointed before the bad pull.
```
```sh
(master)$ git reflog
ab7555f HEAD@{0}: pull origin wrong-branch: Fast-forward
c5bc55a HEAD@{1}: checkout: checkout message goes here
@ -448,8 +453,8 @@ c5bc55a HEAD@{1}: checkout: checkout message goes here
Simply reset your branch back to the desired commit:
```
git reset --hard c5bc55a
```sh
$ git reset --hard c5bc55a
```
Done.
@ -462,9 +467,9 @@ Confirm that you haven't pushed your changes to the server.
`git status` should show how many commits you are ahead of origin:
```sh
(bug24)$ git status
# On branch bug24
# Your branch is ahead of 'origin/bug24' by 2 commits.
(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)
#
```
@ -472,7 +477,7 @@ Confirm that you haven't pushed your changes to the server.
One way of reseting to match origin (to have the same as what is on the remote) is to do this:
```sh
(master)$ git reset --hard origin/bug24
(master)$ git reset --hard origin/my-branch
```
@ -483,11 +488,11 @@ If you want to only reset to some commit between origin and your local, you can
```sh
# one commit
(bug24)$ git reset --hard HEAD^
(my-branch)$ git reset --hard HEAD^
# two commits
(bug24)$ git reset --hard HEAD^^
(my-branch)$ git reset --hard HEAD^^
# four commits
(bug24)$ git reset --hard HEAD~4
(my-branch)$ git reset --hard HEAD~4
# or
(master)$ git checkout -f
```
@ -498,13 +503,13 @@ If you want to only reset to some commit between origin and your local, you can
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.
```sh
$(master) git reflog
(master)$ git reflog
```
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:
```sh
$(master) git reset --hard SHA1234
(master)$ git reset --hard SHA1234
```
And you should be good to go.
@ -513,7 +518,7 @@ And you should be good to go.
## I want to move my unstaged edits to a new branch
```sh
$ git checkout -b new-branch
$ git checkout -b my-branch
```
<a href="move-unstaged-edits-to-old-branch"></a>
@ -521,7 +526,7 @@ $ git checkout -b new-branch
```sh
$ git stash
$ git checkout branch2
$ git checkout my-branch
$ git stash pop
```
@ -530,7 +535,7 @@ $ git stash pop
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 check the difference between your current HEAD and what your HEAD just was with:
```
```sh
(master)$ git diff HEAD@{1} HEAD
```
@ -557,9 +562,9 @@ Let's say that you just blindly committed changes with `git commit -a` and you'r
## Clone all submodules
```sh
git clone --recursive git://github.com/foo/bar.git
$ git clone --recursive git://github.com/foo/bar.git
# If already cloned:
git submodule update --init --recursive
$ git submodule update --init --recursive
```
<a name="deleting"></a>
@ -569,7 +574,7 @@ git submodule update --init --recursive
### 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
```sh
$ git fetch -p
```
@ -578,23 +583,23 @@ $ git fetch -p
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
```sh
(master)$ git checkout -b my-branch
(my-branch)$ git branch
(my-branch)$ touch foo.txt
(my-branch)$ 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
```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
(branch-1)$ git log
(my-branch)$ git log
commit 4e3cd85a670ced7cc17a2b5d8d3d809ac88d5012
Author: siemiatj <kuba@saucelabs.com>
@ -611,12 +616,12 @@ Date: Tue Jul 29 13:14:46 2014 -0400
Now we're switching back to master and 'accidentaly' removing our branch.
```
(branch-1)$ git checkout master
```sh
(my-branch)$ 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)$ git branch -D my-branch
Deleted branch my-branch (was 4e3cd85).
(master)$ echo oh noes, deleted my branch!
oh noes, deleted my branch!
```
@ -625,19 +630,19 @@ At this point you should get familiar with 'reflog', an upgraded logger. It stor
```
(master)$ git reflog
69204cd HEAD@{0}: checkout: moving from branch-1 to master
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 branch-1
69204cd HEAD@{2}: checkout: moving from master to my-branch
```
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
```sh
(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
(branch-1-help)$ ls
(my-branch-help)$ ls
README.md foo.txt
```
@ -649,8 +654,8 @@ Voila! We got our removed file back. Git reflog is also useful when rebasing goe
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]
$ git reset HEAD^ --hard
$ git push -f [remote] [branch]
```
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):
@ -668,16 +673,16 @@ This only works if you haven't pushed. If you have pushed, the only truly safe t
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]
$ git rebase --onto SHA1_OF_BAD_COMMIT^ SHA1_OF_BAD_COMMIT
$ git push -f [remote] [branch]
```
<a name="delete-tag"></a>
### Delete tag
```sh
git tag -d <tag_name>
git push <remote> :refs/tags/<tag_name>
$ git tag -d <tag_name>
$ git push <remote> :refs/tags/<tag_name>
```
<a name="deleted-patch"></a>