Time For Git

[Where it finally gits started]

Maria Aprillia Devira
UKM Heroes
7 min readNov 7, 2019

--

What is Git?

In short, Git is a distributed version control system in which every dev has a working copy of the code and full change history on their local machine. Version control systems are a category of software tools that help manage changes to source code over time. Unlike some other version control software, Git focuses on the file content itself, thus it would be able to determine exactly what the storage and version history of the file tree should be.

Now It’s Time To Git Started!

Before you can git started on using Git, you would first have to download Git which can be found here, which provides git for windows, macOS and Linux. Follow the installer as you would usually and now we can finally git started.

Git is comprised of various commands, some of which would be explained here, and the first thing to note is the command to configure the git itself as you cannot use it before configuring.

1. Configuring Git

Before can start using a repository [explanation in the next part], you would first need to register, or configure, git.

Open you preferred command shell and type in:

and

This configures your git to display your name and email and now that you’ve configured your git, you can try to view your git configurations by running the command:

This will return an output of your user.name and email.name.

2. Start a New Local Repository

What is a Repository? Repository is where you would store all your work in, a local one being in your computer and a remote would be hosted by either GitLab or GitHub. In order to create your remote repository, you would need to have an account in those sites first and create the repository. To connect the two repositories, while opening the command shell in your work folder you would need to input the command:

and

The init command stands for initialize. Once you’ve run the command, Git will initialize a hidden directory in the rot directory of your work folder. The second command is used to tell Git to connect your local repo with your remote one. To see the current status of your git you can input the command:

This would be used quite often to check on what is going on in your repository.

3. Adding Your Local Files to The Remote Repository

Let’s say you’ve already had a file in your work folder and you would like to put, or backup, the files there to the remote repository. To do that, you can use the command:

followed by,

and

With that, now your files would be in the remote, online, repository. To add multiple files, you can simply modify the add command by replacing filename with ‘.’ to only add files located in the root directory and ‘ — all’ or ‘-A’ to get all new and updated files everywhere throughout the project.

If there are any files you have mistakenly forgotten to add, after adding the file you can add the argument ‘ — amend’ before ‘ — m’ in the commit command to amend the last commit by adding new files and it would also overwrite the message of your last commit.

Additionally, you do not have to push your work to the master branch, you can push them to other branches as well. Branches would be discussed later on.

4. Getting Files from a Remote Repository to The Local Repository

Let’s say, your remote repository has been updated by a fellow project member and you would like to update your current local repository. To do that you can simply input the command:

This would pull changes in the current branch made by your fellow developer and synchronize your local repository with the remote one. Now you may ask “What if I don’t have any current version of the project and is starting it for the first time?”. Well, in that case you may use the command:

The different from the pull command, this command would download the entire project into your directory and create a remote repository called origin that points to <repo-url>. This would allow you to bypass the command “git remote add origin <your-git-repo-url>” and you can immediately be able to push to the remote repository.

A Branching Git Manual

It’s now time to step on a more in-depth level of git and as I’ve mentioned before, this is solely about Branch. To understand more about branches, you can imagine git as, like the name implies, a tree with multiple branches. Here we can say that the master branch is the trunk of the tree and any other ones as the usual branches of trees. If you continue to imagine it as a tree, we can say that a branch can be fully modified without any of it impacting or reflected on the trunk or in this case the master branch. At the end its branches can then be applied to the master with the merge option which will be covered soon.

Using a Branch

Creating a branch and switching to it is very easy, all you need to input is the command:

The command git checkout is also used to switch branches without creating a new one, and to do that all you need to do is omit the argument ‘-b’. Now how do you confirm which branch you are on? Simply input:

This will provide an output of all your branches, while indicating which branch you are on with a(n) ‘*’ symbol.

Now what do you do if you’ve completed your branch and want to update your master with the branch? You can either do that in your remote repository site, in this case we would use GitLab as an example, by creating a merge request or if you’re doing the project solo you can switch to the master branch and just input:

and after you’re finished you can delete that branch, if you so desire, with the command:

That is all I have for the branch command.

Pro-Level Git Manual

Now we are stepping on a more advanced territory, where we talk about some of the crucial commands in git that could help you immensely.

Git Stash

Simply put, git stash saves your current progress. Usually used to save any changes made after your last commit before changing branches. This way you do not need to commit your changes, but still have your progress saved. As you can deduce the command to stash your current progress is:

This command saves your current progress into a stack. To see what stashes you have stored in the stack simply input the command:

This would then give an output of the stashes along with the specific name it has been given, such as stash@{0} or stash@{1}, to differentiate each stash. Now what if you want to recover the stashed progress? To do that you would need to type in:

If for example you have 3 stashes saved, you can add the argument of “stash@{x}” at the end with the x corresponding to the stash name of your stash to specify which one you would like to restore. If nothing is specified, git would assume the most recent stash and apply it.

With the option of adding, comes the option of removing. To remove a certain stash from the stack you can input:

Followed by the specific stash name and if you would like to remove and apply it beforehand use:

Followed by the specific stash name. As you can see, the stash is saved in a stack and as such the commands that ties to it are also commands used for stacks.

Another method of removal is to clear the stash using:

Git Revert and Git Reset

Since all of us are human beings, I hope, we often make mistakes. Sometimes if you are not too careful with your commit you can easily make a mistake. Not to worry, git has our backs for this. Git offers the simple help of going back in time by using the revert and reset command. I, myself have not used these commands very often so all I can provide here would be a simple knowledge of them.

First thing, you would need to know your commit hash to be able to specify where to go back to and to do that all you need is:

This will give you the log of all your commits and from there you can see your hash and which commit it belongs to. Now the command reset allows you to use a hash or file name as an argument but to simply switch to the most recent commit type in:

Reset also allows you to clear any commits after the one you’ve switched to. This is extremely risky, but the command for that is:

And like the others, you can add the argument of <commit-hash> to specify which commit to go to and which to clear. Basically, reset is used to reset the current HEAD to the one specified. Revert however, is way more riskier than reset as it revert the changes of your work to the one specified. Its command include:

That is all I have for today folks. I would like to apologize for the lack of information given for the advanced git as that is all that I understand from it. Now it would be our usual time to depart and

see you next time space cowboy…

--

--