Git Hacks

Srikanth Kavoori
2 min readJun 6, 2018


Here are some hacks that are helpful in day to day development when using git as source control. Some of these are from my notes and some are taken from other posts online. I find these to be a valuable reference and are most frequently used when you are working on multiple git repos based workflows.

Merging multiple commits into a single commit

git rebase -i HEAD~2 

The above command launches the rebase tool in interactive mode and will help you collapse the last 2 commits from head. rebase is one of the most powerful tools that is invaluable for day to day use.

Undo last commit

To preserve last made changes

git reset --soft HEAD~1

To throw away last made changes

git reset --hard HEAD~1


Amend last commit

git commit --amend 

This will help edit the last commit. This will only amend the local commit that has not been yet pushed to the repository.

Import all changes from an existing repo along with history as a sub-directory.

Using subtree

git subtree add -P SRC_DIRECTORY ssh://git@REPO_URL master

Import all changes from a sub-directory on an existing repo into a new repo with all history

Using filter-branch

Although you could use subtree let us explore a little detailed approach that is provided here in stack overflow

Prepare the changes in sub-directory. (SOURCE REPO)

git clone SOURCE_REPOcd SOURCE_REPOgit remote rm origin git filter-branch --subdirectory-filter SOURCE_REPO -- --all 

Merge the changes to the new repo

git clone DEST_REPO cd DEST_REPO git remote temp_origin PATH_TO_PREVIOUSLY_PREPARED_REPO_ABOVE git pull temp_origin master --allow-unrelated-histories git remote rm temp_origin git push 

When you have multiple directories to import to new repo use the following command.

git filter-branch --index-filter 'git rm --cached -qr --ignore-unmatch -- . && git reset -q $GIT_COMMIT -- $DIR1 $DIR2 $DIR3' --prune-empty -- --all\n



Srikanth Kavoori

A hands on engineer/leader with experience designing and developing embedded, cloud and enterprise software.