Is it possible to pass commit to another branch

I created an additional branch for some testing purpose.

Before starting work, I switched to the master branch and, after tea, started adding files and changing some other files in the main branch.

Only after I committed, I remembered that I was in the main branch, and I had to switch to my second branch before starting the changes.

Could you tell me if this commit can be sent to the second branch and removed from the main branch?

thank

+3
source share
2 answers

If I understand correctly, you can do things like this:

git checkout master
git log
now check hash commit you want to move - for example: 0123456
git checkout branch
git cherry-pick 0123456
git checkout master
git revert 0123456

what all.

+6
source

The answer is "like, but maybe you don’t need to."

, git cherry-pick commit. git: " , , . , , . 1 ."

, master, newbranch:

$ git checkout newbranch
$ git cherry-pick master

newbranch, . - master.

( - ) , "":

$ git checkout master
$ git reset --hard HEAD^  # be careful here! I always run "git status" first

, git reset --hard .

, (. user2699113 answer). "" , , , : , , , , , . (, , , .)

( , SHA-1, - .)


1 -n --no-commit , git , , .


, ", "?

git ​​. git , , " ", . 2 ( , ), - " " - .

, commit -. - , ( git) , git . , ( DAG ) :

A <-- B <-- C     <-- master
            ^
             \
              D   <-- branch

commit D branch, commit C . Commit C B, A, (root, parent-less) commit.

" " , , , . , A C ( , , ):

A - B - C      <-- HEAD=master

HEAD=, , git , "on". ( HEAD .git : cat .git/HEAD, ref: refs/heads/master - .) git checkout -b branch, :

A - B - C      <-- master, HEAD=branch

( .git/HEAD branch master).

( , ), git HEAD, , , SHA -1 ID. " ". git , , , . (, , HEAD .)

, " branch" commit D, , :

A - B - C      <-- master
          \
            D  <-- HEAD=branch

" master", :

A - B - C      <-- branch
          \
            D  <-- HEAD=master

, , , . , , master, branch, branch , master, HEAD , branch master.

, ... , .:-) , git . branch, master. master branch branch master:

$ git branch -m branch temp
$ git branch -m master branch
$ git branch -m temp master

branch temp. master branch ( , HEAD). temp master, DAG , .

, ​​ " " () . ( : , --. " ", , -ID-, , , master bb71dde..., , , . git, , " ".)


2 , Mercurial .

+6

All Articles