From f160a96ef6b5223ead42fe43040ca41d4a85a1c2 Mon Sep 17 00:00:00 2001 From: Joe Block Date: Wed, 30 Jul 2014 14:57:27 -0700 Subject: [PATCH 1/3] Add entry for safe rebasing of unpushed commits --- README.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/README.md b/README.md index c745e82..0b4e237 100644 --- a/README.md +++ b/README.md @@ -112,6 +112,18 @@ If everything is successful, you should see something like this: (master)$ Successfully rebased and updated refs/heads/master. ``` + +#### 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. + +``` +(master)$ git rebase -i @{u} +``` + +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. + + ### Possible issues with interactive rebases From 6526fc148ff22ffa27160f19fa3881b0f801cd4a Mon Sep 17 00:00:00 2001 From: Christophe Naud-Dulude Date: Wed, 30 Jul 2014 14:59:12 -0700 Subject: [PATCH 2/3] Fix typo --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 0b4e237..4119f79 100644 --- a/README.md +++ b/README.md @@ -40,7 +40,7 @@ If you are working in a branch that is/will become a pull-request against `maste (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 squish the last 2 commits, for example, you'll have to rebase against `HEAD~2`. For the last 3, `HEAD~3`, etc. +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. ``` (master)$ git rebase -i HEAD~2 From 9b88f428a424c17ae3a03a4a029d59e93d618396 Mon Sep 17 00:00:00 2001 From: siemiatj Date: Wed, 30 Jul 2014 00:54:50 +0200 Subject: [PATCH 3/3] -add section for reflog --- README.md | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) diff --git a/README.md b/README.md index 4119f79..3a84979 100644 --- a/README.md +++ b/README.md @@ -286,3 +286,73 @@ And finally, let's cherry-pick the commit for bug #14: ``` (14)$ git cherry-pick 5ea5173 ``` + + +## I accidentaly 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 +Date: Wed Jul 30 00:34:10 2014 +0200 + + foo.txt added + +commit 69204cdf0acbab201619d95ad8295928e7f411d5 +Author: Kate Hudson +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.