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.
1 answer
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.
- Read more about feature cards .
- About different models of work , including feature boards.
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 frommaster.Important:
commit --amend,rebase -iand other operations for rewriting history cannot be performed in common (stable) branches likemaster.
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 abc123before it. - Nick Volynkin β¦ - @akp and the old branch remained unchanged, we didnβt touch it at all. - Nick Volynkin β¦

git cherry-pick hash- Dmitriy Simushevgit rebase -ithere any better? - Pavel Mayorov