git reset --soft HEAD~2 # notice this is 2, not 3
git commit --amend
From:
commit 1
commit 2
commit 3
to:
commit 1 # this will include commit 2 and commit 3
git reset --soft HEAD~2
git commit --amend -C HEAD # this will automatically pick `commit 1` as the commit name
I have this in my git config:
fixup = "!f(){ git reset --soft HEAD~${1} && git commit --amend -C HEAD; };f"
And I use it as
git fixup 2 # merges the last 2 commits into their parent
for convenience you can make the parameter of the fixup git alias optional and set it to a default value of 1 by replacing ${1} with ${1-1}, e.g.:
fixup = "!f(){ git reset --soft HEAD~${1-1} && git commit --amend -C HEAD; };f"
So then you can just do:
git fixup # merges the last commit into its parent
$ git checkout --orphan NEWBRANCH
--orphan creates a new branch, but it starts without any commit. After running the above command you are on a new branch "NEWBRANCH", and the first commit you create from this state will start a new history without any ancestry.
The --orphan command keeps the index and the working tree files intact in order to make it convenient for creating a new history whose trees resemble the ones from the original branch.
Since you want to create a new empty branch that has nothing to do with the original branch, you can delete all files in the new working directory:
$ git rm -rf .
Now you can start adding files and commit them and they will live in their own branch. If you take a look at the log, you will see that it is isolated from the original log.
Using the checkout command you can switch back and forth between the different branches like this:
$ git checkout master (back at the master branch)
$ git checkout NEWBRANCH (back at the new isolated branch)
You need to run git version 1.7.2 or higher in order for the --orphan option to be supported.
Git diff with package-lock.json/yarn.lock is difficult since you need to scoll though too much noice, but we can’t git ignore those as well. Those files are suppose to be checked into the repository.
git diff
But those can be easily ignored only from the diff.
git diff -- ':!package-lock.json' ':!yarn.lock'
You can add this to your default alias for git diff.
alias gd="git diff -- ':!package-lock.json' ':!yarn.lock'"