Tuesday, 11 July 2017

Git Tools

Change the Last Commit

$ git commit --amend

Change Multiple Commit Messages

$ git rebase -i HEAD~3
git commit --amend
git rebase --continue

but keep in mind that you’re actually designating four commits ago, the parent of the last commit you want to edit

Reordering Commits

You can also use interactive rebases to reorder or remove commits entirely.

Squashing Commits

It’s also possible to take a series of commits and squash them down into a single commit with the interactive rebasing tool.
# Commands:
#  p, pick = use commit
#  r, reword = use commit, but edit the commit message
#  e, edit = use commit, but stop for amending
#  s, squash = use commit, but meld into previous commit
#  f, fixup = like "squash", but discard this commit's log message
#  x, exec = run command (the rest of the line) using shell

Splitting a Commit

The Nuclear Option: filter-branch

There is another history-rewriting option that you can use if you need to rewrite a larger number of commits in some scriptable way – for instance, changing your email address globally or removing a file from every commit.

Removing a File from Every Commit

This occurs fairly commonly. Someone accidentally commits a huge binary file with a thoughtless git add ., and you want to remove it everywhere (--tree-filter option to filter-branch)
$ git filter-branch --tree-filter 'rm -f passwords.txt' HEAD

Making a Subdirectory the New Root

Changing Email Addresses Globally

Another common case is that you forgot to run git config to set your name and email address before you started working,
$ git filter-branch --commit-filter '
        if [ "$GIT_AUTHOR_EMAIL" = "schacon@localhost" ];
        then
                GIT_AUTHOR_NAME="Scott Chacon";
                GIT_AUTHOR_EMAIL="schacon@example.com";
                git commit-tree "$@";
        else
                git commit-tree "$@";
        fi' HEAD
https://git-scm.com/book/en/v2/Git-Tools-Rewriting-History#Changing-Multiple-Commit-Messages

No comments:

Post a Comment