Git Gud

Ilman Nafian
UKM Heroes
Published in
4 min readSep 26, 2019
https://git-scm.com/downloads/logos

In this semester, I’m taking a Software Development Project. I got assigned into the team that will work on new website for UKM Indonesia. As per usual, we are going to use git as out version control.

I’ve personally have used git many times, from a course requirements to my personal project. Git have become a staple part to every project that I’m working. Since it has been a long holiday, lets refresh our (especially mine) knowledge about git.

Pull

The command git pull is used to update the local repository with new changes from the remote repository. If the branch that you tried to pull diverge from remote repository, git will try to merge the changes automatically. In case where there are conflicts, git will go into merge mode where developer must fix any conflicts then commit the changes.

TIL that git pull is actually a shorthand for git fetch then git merge FETCH_HEAD

Common usage:

git pull origin master

Push

This command is used to push your local changes to remote repository. If your head is behind the remote head, git will ask you to first to pull and merge those change.

Common usage:

git push origin dev

Clone

I personally rarely used this command, no specific reason. When I want to have a local repository, I’ll just pull the project manually. I think I did this because we never told to try cloning a repository when we first learn about git and then carry on by just using git pull. When I research about cloning, I found that clone is basically pull but clone will also create remote-tracking branches. Remote-tracking is basically so that git know how local and remote branches related. By having those tracking created, we don’t have to specify the branch when we are trying to pull and push.

Common usage:

git clone https://gitinstance.com/user/repository.git

Merge

Now this is command that I’ve been using more and more. When the first time we learn about git, we don’t really mess around with branching so we never used merge. I actually tried using branching after learning about git so I kinda have gotten used to it by then.

Merge is used to incorporate remote changes to local. Merge usually used “merge” a branch to current branch. Merge also accept a few fancy option that I sometimes used which is fast forward and squash. Fast forwarding occurs when merging a changes to a branch that does not got any new changes after the source branch diverge, therefore git will just update the pointer. When this kind of thing happen, I use the argument - -no-fast-forward simply because the final log does not look pretty :). Squashing a merge will make the commit series into one giant commit. I’ve used it a couple of times when working on big project so that the history is not too cluttered.

Common usage:

# while on master
git merge staging

Rebase

This one I definitely never used before. Quick research shows that rebase is used to update a branch code base to a refs. The purpose of doing this is to update current branch when the master branch got an update which you also want in your code. Rebase also will make your commits looks linear, as if you started your work from the latest commit.

Common usage:

git rebase master

Revert

Together with reset, I never use these command in real world because I personally always careful when committing and pushing changes. Tho I know how to use them since it was part of our git lesson. Revert is used to “revert” existing commits to a certain ref.

Common usage:

git revert HEAD~2

Stash

Stash is used to “stash” local changes. I’ve used this many times because I often forgot to switch branch when working on something. So in order to bring those uncommitted change to the correct branch I “stash” those changes, checkout to appropriate branch, than apply those stash.

Common usage:

git stash
git checkout dev
git stash pop

Remote

A git repository can have multiple remote repository to interact with. Commonly we have origin, but sometimes we need more than just that. For example if we have different repositories for development stages or maybe we have a source branch where we get updates but we want to push to our own repositories, remote can help us.

Common usage:

git remote add origin https://gitinstance.com/user/repository.git

Checkout

When working with our project, we usually have stuff worked on parallel whether by our own or by fellow developer. Without branching, there is going to be many chaos happening because of git nature. That’s why we have branching where we can work on our own environment. To move through branches, we use the checkout command. By the way, you can’t switch branch when there is/are uncommitted changes, you can either restore those file or commit them.

Common usage:

git checkout staging

Branch

Continuing from previous command this is the command related to git branching, you can create, a branch using this command.

Common usage:

git branch feature

So yeah, that’s most of the command that you’ll encounter a lot. Git is an awesome tool that help developer manage, collaborate, versioning, etc their code. I hope that by writing this blog, I’ll utilize git more so that it can benefit me and my team in this project.

Thank you for reading, have fun coding!

--

--