Advanced git scripts

I have some advanced scripts that I would like to solve, but I do not know how to do this. Hope you can help:

  • A - B - C - D - E I would like to add the changes from commit D to commit B and remove commit D How?
  • A - B - C Commit B contains a file that should not have been installed. How to remove it from a commit?
  • A - B - C - D Commit C contains the file that should have been committed using commit B How to remove it from commit C and add it to commit B ?

Please note: all commits are only in my working copy and have not yet been pressed.

+6
source share
1 answer

My answers use git rebase quite a lot. If you are new to reinstalling, be sure to read this chapter at the beginning of git reba

  • git cherry-pick B As you yourself learned, you could rebase -i A and change C / D revisions, knocking B + D together.
  • git rebase -i B , mark B how to edit and apply the changes.

    Now, to really remove objects from the object database, you may need to use the filter branch for all branches containing the object. After this, it may take some time to complete the logging. If the object is security sensitive, you might want to git gc --aggressive --prune=tomorrow . The Pro git book has a section on deleting objects .
    With git-filter-branch always remember to include --tag-name-filter cat , see comments

  • If all C should be in B, just

    git rebase -i B mark C squash and it will crush the commits together.

    If it becomes more attractive than this (only one file from C should go to B), I would divide the task into smaller steps that can be solved as described above:

    • divide commit C into two parts: one (C.1) to merge into commit B and one (C.2) to save as the new 'C'. For instance. from the git Pro section linked above:

      If you want to split the commit, for example, you must specify "edit" for that commit:

       pick fc62e55 added file_size pick 9824bf4 fixed little thing edit 21d80a5 added number to log pick 76b9da6 added the apply command pick c264051 Revert "added file_size" 

      And then, when you go to the command line, you will return this message and create two (or more) new ones. Let's say 21d80a5 changed two files, file1 and file2, and you wanted to split them into separate commits. You can do this after rebase puts you on the command line:

       $ git reset HEAD^ $ git add file1 $ git commit 'first part of split commit' $ git add file2 $ git commit 'second part of split commit' $ git rebase --continue 

      And now instead of 5 commits you will have 6.

    • rebase to combine commits B and C.1
+8
source

Source: https://habr.com/ru/post/900861/


All Articles