How to make a separate branch from some pictures?

There is an existing old branch in which the whole story is from the resulting and intermediate images. It is necessary to select from it only the resulting images and create on their basis a separate β€œclean” branch, for example, master .

The decision does not have to correspond to the drawings, perhaps there is a more competent option, I don’t know.

Fig. 1 - old branch.
Fig. 2 - option with merge.
Fig. 3 - copy option.
Black - the resulting images.
White - intermediate shots.

  • 2
    git cherry-pick hash will do? - Kostiantyn Okhotnyk
  • Use git cherry-pick hash - Dmitriy Simushev
  • one
    @KonstantinOkhotnick and git rebase -i there any better? - Pavel Mayorov
  • By the way, you have great illustrations. - Nick Volynkin ♦

1 answer 1

How to work so that such a problem does not arise?

"Intermediate" commits are needed and useful. It is very convenient to save somewhere in the middle of development: to try another solution, to transfer work to another machine, just for peace of mind.

But in the final story they are not very interesting. Moreover, I personally would not want anyone in months and years to watch how I invented bicycles, wrote by line per hour and made silly mistakes. No, I want one final commit, as if I sat down, laid my hands on the keyboard and created a masterpiece. Perhaps you too will be more comfortable.

What to do?

1. Use feature branches

To work on each task, make a separate branch from the master . Make as many commits as you want. When the result is ready, you can collect all these commits into one or several meaningful ones (see the instructions below) and hold them in master .

I recommend merging with --no-ff . This is a more informative story: you can see where the commits of one feature ended and the commits of the other began.

2. Use commit --amend for intermediate commits.

Fixed one line in the feature and again need to make a commit? Easier to rewrite the old one.

 git add path/filename git commit --amend --no-edit 

Here --no-edit means that we do not want to change the commit message - leave the old one.

Important: You need at least one new commit in the branch that you will be rewriting. If you have just made a new branch from master , for now you cannot do --amend . Otherwise you will rewrite the commit from master .

Important: commit --amend , rebase -i and other operations for rewriting history cannot be performed in common (stable) branches like master .

How to combine commits into groups in a new thread?

Suppose we have a history of commits. "Intermediate" commits are designated as bullshit , and "efficient" - result x . The newest commit is on top.

 git log --oneline --graph --decorate * 71e8ed5 (HEAD -> oldbranch) result 2 * 0955502 bullshit * cf30c6f bullshit * d590b3d result 1 * 8266a62 bullshit * ce5b35c bullshit 

The rebase rebase -i command will help us rebuild them into a more beautiful (which is very subjective) story.

First, we will create a new branch in which the result of the operation will be.

 git checkout -b newbranch 

Since here we want to borrow the entire branch as a whole, we add --root .

 git rebase -i --root 

If you need to rewrite history only to a certain commit (not including it), we indicate the hash of this commit.

 git rebase -i ce5b35c 

The editor opens with this content. Here, the order of commits is the reverse: the newest commit is at the bottom .

 pick ce5b35c bullshit pick 8266a62 bullshit pick d590b3d result 1 pick cf30c6f bullshit pick 0955502 bullshit pick 71e8ed5 result 2 # Rebase 71e8ed5 onto f7569aa (6 commands) ... 

What can be done with commits in this situation? We are interested in two teams:

 # p, pick = use commit # ΠΎΡΡ‚Π°Π²ΠΈΡ‚ΡŒ ΠΊΠΎΠΌΠΌΠΈΡ‚ ΠΊΠ°ΠΊ Π΅ΡΡ‚ΡŒ # s, squash = use commit, but meld into previous commit # ΡΠ»ΠΈΡ‚ΡŒ содСрТимоС ΠΊΠΎΠΌΠΌΠΈΡ‚Π° с ΠŸΠ Π•Π”Π«Π”Π£Π©Π˜Πœ ΠΊΠΎΠΌΠΌΠΈΡ‚ΠΎΠΌ 

We combine (squash) commits in groups: the first β€œintermediate” after each result will be the basis of a new result.

 pick ce5b35c bullshit s 8266a62 bullshit s d590b3d result 1 pick cf30c6f bullshit s 0955502 bullshit s 71e8ed5 result 2 

When you save this text and exit the editor, a new one will open, with the text of the message for the new result 1 commit, then for result 2 and so on - as many times as you want to leave as a result. The first will be the oldest, the last - the newest. By default, you are offered this pattern:

 # This is a combination of 3 commits. # This is the 1st commit message: bullshit # This is the commit message #2: bullshit # This is the commit message #3: result 1 # Please enter the commit message for your changes. Lines starting 

When you edit and save all new messages, rebase -i will end. It will turn out like this:

 * 588a7ac (HEAD -> newbranch) result 2 * 2713fda result 1 
  • @akp she is not from the end of the old, she is completely separate (orphaned). If you want the branches to connect somewhere below, then select the commit where they will fork and rebase- i abc123 before it. - Nick Volynkin ♦
  • @akp and the old branch remained unchanged, we didn’t touch it at all. - Nick Volynkin ♦