74
72

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Git使ってて困ったこんな時

Posted at

:astonished: ブランチの派生元間違えた!!

本当はdevelopブランチから切らないといけないのに、masterブランチからfeature/hogeブランチを切ってしまった:cry:
もう3回くらいコミットしてしまった...(´;ω;`)ブワッ ブランチ作りなおしてcherry-pickするか.......
こんなとき!!

$ git log --graph --all --decorate --oneline

* 08733a5 (HEAD -> feature/test) test 1
* e7872a3 (master) master 2
| * 09140ba (develop) develop 1
|/  
* 8c001b5 master 1

↑の例では、masterブランチからfeature/testブランチが切られていますが、これをdevelopブランチのHEADから派生させるようにします。

$ git rebase --onto develop master feature/test
First, rewinding head to replay your work on top of it...
Applying: test 1

$ git log --graph --all --decorate --oneline
* 17f14a6 (HEAD -> feature/test) test 1
* 09140ba (develop) develop 1
| * e7872a3 (master) master 2
|/  
* 8c001b5 master 1

developブランチのHEADからfeature/testブランチが派生しています :tada:

:astonished: PullRequestをMergeしたけど取り消したい!!

$ git revert -m 1 [commit-hash]

-mはmainlineの意でどちらの親コミットを採用するかを決定するもので、1を指定するとマージ先を残す(マージ元の変更点はRevert)。

:astonished: stash popしたものを再度applyしたい!!

bash
$ git stash pop
On branch master
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)

	modified:   a.txt

no changes added to commit (use "git add" and/or "git commit -a")
Dropped refs/stash@{0} (a851cb4046b262e0406d604c3f3aa66df789530d)

$ git checkout .
$ git fsck
Checking object directories: 100% (256/256), done.
dangling commit 1cb1f44731ccbccdd8109bda42ebbdaa1c8cb407
dangling blob e0fac643666921c86891292ad5d1689ad67b7347
dangling commit a851cb4046b262e0406d604c3f3aa66df789530d ← コイツ

$ git stash apply a851cb4046b262e0406d604c3f3aa66df789530d
$ git status
On branch master
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)

	modified:   a.txt

no changes added to commit (use "git add" and/or "git commit -a")
74
72
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
74
72

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?