Tag: version-control

Create Clean Branch in Git

Normally, whenever you create a new branch, you need the original branch and then you branch them. The problem is, they have a parent branch. The commit history from the original branch will be carried to your new branch too. This is intentional and not the git issue. That how git branch supposed to be work.

SourceTree new Branch

Normally, you don’t need to create a completely new branch. Rather, you create a new repository. It new project anyway, so it best to use a different repository. Or maybe just change it gradually.

But, sometimes you need or want to create a completely new branch without any parent but still in the same repository. For whatever your reason is.

You may don’t know this yet, but git has a command to checkout new branch without any parent. It called the orphan branch. With this command, you can create a completly new branch and its root won’t have any connection to the other branch. But it will still have previous files in the repository directory. Don’t worry about that, you can remove it later after the checkout process is done.

So in short, what you want to do is to create a new orphan branch, clear the file and remove file history and then add the new file for your new project. Here is how to do it.

First, create a new orphan branch. It has the same command as git checkout but with the --orphan parameter.

git checkout --orphan <newbranch>

Second, you want to remove any old file in the repository directory. Either you delete it using file explorer, command or by any method you prefer. If you are using git rm command, it may look like this.

git rm --cached -r .

Dot “.” character at the end of the command is intentional. That mean it will remove any files from the current directory which is your repository directory. -r parameter mean is it will remove recursively to ensure any files is deleted.

And that it. Simple right?

If you have any further question or suggestion, you can ask me in the comment bellow. Thank you for reading!

Why you should use Version Control?

My friend asking me why I’m using Version Control even if I’m working alone. It seems that for some new developer, version control like Git or SVN only for people that work in a team. In their mind, Version Control is only for this purpose and no other.

Well, in some sense they are not wrong. It’s like saying notes is only for taking notes. It’s true, but it’s not completely correct either. You can use notes for many other things, like write a diary, to do list, scribbling, etc. It’s only matter of use. Different people can use the same thing for the different thing.

But if you only know using Version Control for team development, here are some of useful thing you can do:

File Backup with Versioning

Have you ever make changes to a file and want to revert back? I already modify x file 6 times, but I want to revert back to file when it was still 3 times changed. Or maybe lost your code but your backup is too old? With Version Control, not only you can backup your files but it mark your changes too.

What more, you can compare the difference between the version of your code.

Your project has multiple version

Often in some project I work on like cashier application, I support multiple version of my project for a different company. Mostly they are similar, but they want a bit change to the code to fulfill their needs. Or brochure project you make for a client and the client asking you to make a several and similar version of the brochure. You can use Git branching for this, and it would be easier to manage the project.

Let you doing experimental changes

I often want to make a feature I like to add, but I’m not sure if it can be used or fit my system. I can just change my code, but I carrying risk my application being broken by my changes. Which is usually will break my application or at least introduce a bug.

With version control, you can just change them as you like, if it broke and want to change your mind, just revert it back to last working version. Or you can use a branch for that. Create a branch, make changes, if it broke remove the branch, if working merge the branch. Simple right?

Of course it not limited to these, you can use Version Control as you like. I always telling my friends to use Version Control to minimize the risk when they are working on their project. Nothing to lose right.

If you have more idea to use version control, feel free to tell me how you use version control for. Or if you haven’t convinced yet, you can ask me for more use of version control via comment bellow.