Rebasing is one of the features you probably want to have, if you plan to work on a neat git based project.
🍣 Where To Rebase?
If you know how many commits you make, to rebase you use git rebase with -i flag to enable interactive rebasing. The HEAD~<n> corresponds to the number of commits you have done (e.g. HEAD~4 if you have 4 commits to rollback to get to common ancestor commit).
git rebase -i HEAD~<n>
Sometimes, you commit a lot and forgot how many commits you’d make. To know the least common anscestor you have with master, you do git merge-base with your branch name as parameter.
git merge-base <your-branch> master
The above command will return a git hash which you can use on the git rebase command.
If you already know the git hash, then you can rollback to that specific commit and moving all current changes to unstaged. Once, the editor pop-ups you will choose which commit to retain, squash, and reword.
git rebase -i <git-ref-hash>
🍣 Merge Latest From Master
If you’ve already rebased your changes and needed to get lastest changes from master. All you have to do is rebase to the latest changes from master. This command will do that.
git rebase origin/master
In any case, you’ve encountered some conflict first resolve it then continue in rebasing instead of creating new merge commit.
git rebase --continue
🍣 Overwriting Remote Repo Changes
Once all is done, overwrite your remote repo latest changes if you’ve pushed it. This will do a force push ignoring current ref on remote repo.
git push -f
🍣 Did Something Wrong? In Need Of Rollback
Did something wrong on merging conflicts? Don’t worry you can still see your previous changes using the command git reflog short for reference log. You can checkout the reference hash then re-merge your changes.
Ever been in a situation where a sub-folder of your git repository needs to branch out as a new repository? Here in this article I’ve tried a new way using a python module to simplify the process. This steps is also recommended by the Git core team as ways to move a sub-folder to a new clean repository (link).
We’re just clones, sir. We’re meant to be expendable.
Come on let’s jump in! 🚀
First of all, you must have a Git1 on your machine. Second, must have existing test git repository and Python 32 installed.
If you don’t have Git yet, you can install git from its official sources, its available on all platforms even on android. Or if you have Visual Studio3 installed, just locate it from your drive. Python can also be installed using the Visual Studio installer.
So where do we start?
This will be our initial test repository structure:
Then install this python module named git-filter-repo. Install the module using the pip utility.
pip3 install git-filter-repo
This git-filter-repo simplifies the process of filtering files, directories and history. This tool as said on its Github page falls on the same category as git-filter-branch. You can check its Github repository page for pro’s and con’s against similar tools.
Next thing we do is go into the cloned test repository and filter the directory you want (in our case its the desired-directory) to separate into a new repository.
git filter-repo --path desired-directory --subdirectory-filter desired-directory
This will modify the cloned directory history and delete existing content that does not match the subdirectory filter. The new structure of the directory will be like this:
That’s all the steps needed, check if everything is working. Check the quick review below for a summarized setup.
Here are the simplified steps based on the above:
Make a copy or clone the current project where the sub-folder is located.
Install git-filter-repo using the command pip3 install git-filter-repo.
On the cloned project folder, filter it base on the directory you want to make new repository with the command git filter-repo --path <new-path> --subdirectory-filter <filtered-directory>
Go to your current project folder and delete the sub-folder using the command git rm -rf <filtered-directory>.
On the current project create the sub-module using git submodule add <new-repo-url> <filtered-directory>.
Check if everything is okay.
That’s all guys, always make a backup of your repository before proceeding. Anyways its on git version control system – you can go back and re-fix if there is something wrong.
There are many answers in the internet regarding this matter, but mostly they don’t explain what will occur when you run this command, this one I’ve personally tried it as before I was using a mono repository setup. But it became so large that its hard to maintain especially on cases of testing and checking the history.
Let me know in the comments if you have questions or queries, you can also DM me directly.
Follow me for similar article, tips, and tricks ❤.
I believe that the first test of a truly great man is his humility. Really great men have a curious feeling that the greatness is not in them but through them. And they see something divine in every other man and are endlessly, incredibly merciful.
— John Ruskin.
One day I was working on a driver port to macOS (Apple Macintosh OS) and the only opensource code for it can be found on Linux kernel.
Heck! The Linux kernel repository is around 2GB including all history and I only needed a specific directory inside the repository. After searching the whole internet I found an answer1.
Here are the steps to clone a specific directory from a git repository:
First and foremost you need to create a local blank repository on your workstation. git init <repo-url>
Inside the created bare repository, map the remote URL of the remote repository you want to clone. cd <repo-name> git remote add origin <remote-repo-url>
Then, setup the git config and specify that you’ll be doing a sparse checkout. git config core.sparsecheckout true
Create and add all the directories you want to checkout in the sparse-checkout file that can be found in .git/info/sparse-checkout. echo "<needed-directory>/*" >> .git/info/sparse-checkout
When all the above steps is done, finally pull the repository objects. git pull --depth=1 origin master
So guys if you have any questions? hit me up on my social media accounts. That’s all there is that is needed. Now its already cloned and can now be worked on.
Together we can change the world, just one random act of kindness at a time.
— Ron Hall.
Hi guys, I’ve been wondering before on how to create aliases in git (a version control system) but after reading and delving more into some documents. I found out that this is possible and can be done with a few global configs. Check out the commands below for some sample.
This commands will emulate an SVN (subversion) like keywords and can be called on command line like git ci to commit your changes. Additionally, here are some useful aliases that I’ve found while exploring the internet.