git source control with a local repository (no server)

Even when working alone or on simple projects, I like to have the benefits of source control – history, branching, and backup.

Usually I keep the repo on one drive (that is backed-up) and the project itself on another.

Git is the solution I decided upon at this point because it has become a open source industry standard. Here are the common steps that need to be taken to use git in this scenario.

Repository

Create a folder that will serve as the repository, let’s call it repo. (In my mind, this is the “server”, the destination where the files and changes are tracked, and where you can get the files in various states from.)

Enter this folder and connect it to git:

 git init 

Then you need to add a file to the repo.

touch readme.md
git add readme.md
git commit -m "init"

After this, the “master” branch exists and is checked-out in the repo. Now you need to unlock the master branch.

git branch live
git checkout live

(These two steps are missing in a lot of online guides for some reason. It’s a specific thing for git, and kind of strange. Only now, you will be able to start pushing to the master branch. The live branch you can merge from time to time, whenever you have a release ready for example.)

The repository is ready.

Working folder

Now clone the repository into a (non-existing) working folder:

 git clone <path_to_repo> <working_folder> 

Go into the newly created working folder and switch it to the master branch (it’s currently on the live one):

 git checkout master 

Now you are ready to start using git operations – commit, push and the rest of the operations will be reflected to the repo folder when you merge.

git merge master

Snippets

Common operations

 git add [-n] . 
 git commit -m "Commit message" 
 git push origin master 

Undo

Undo add

git reset <file>

Undo commit

 git reset HEAD~1 

Revert all changes

 git reset --hard HEAD~1 

(More info on Stack Overflow)

Repo moving

The repo folder needs to be copied entirely. Then, change the working folder to the new one:

git remote rm origin
git remote add origin <path_to_new_remote>

Branch info

 git branch -vv 

Ignore files

 touch .gitignore 

(Common ignore configurations created by GitHub)

Use https

 git config --global url."https://".insteadOf git:// 

Links

Bojan Bjelić

Working hard on bunch of stuff, positive future above all.
I’m blogging mostly about software, productivity and digital world.

Leave a Comment

Your email address will not be published. Required fields are marked *