Understanding the basics of git.

Rizal Alfaridzi
UKM Heroes
Published in
6 min readOct 18, 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 Let’s Git Started!

Before you can git started on using Git, you would first have to download Git which can be found here [https://git-scm.com/downloads], which provides git for windows, macOS and Linux. Follow the installer as you would usually and now we can finally get started.

1. Configuring Git

Before can start using a repository, you would first need to register, or configure, git.

Open you preferred command shell and type in:

“ git config — global user.name “Fname Lname” “

“ git config — global user.email “your-email@email-provider” “

Now that you’ve configured your git, you can try to view your git configurations by running the command:

git config — list

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 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:

git init

Every change you make is tracked in a repository, so to use git this will be the first thing you’ll do. To initialize a git repository, use this command while inside the working folder.

git remote add origin <your-git-repo-url>

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:

git status

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:

git add filename

git commit -m “message”

git push origin <branch name>

With that now your files would be in the remote, online, repository. To add multiple files, you can 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.

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

For example, 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:

git pull

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:

git clone <repo-url>

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.

Branching Git Manual

Now that you’ve learned the basics, it’s time for some more in-depth commands. Git has a structure that resembles trees. With the “master” being its trunk or base and having multiple branches which can be modified without anything being reflected on the “master”. At the end its branches can be applied to the “master with the “merge” option.

· Creating a Branch

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

git checkout -b <branch-name>

Git checkout can also be used to switch branches without creating a new one, 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:

git branch

This will provide an output of all your branches, while indicating which branch you are on with an ‘*’ 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 input:

git merge <branch-to-merge>

and after you’re finish you can delete that branch, if you want, with the command:

git branch -d <branch-name>

Pro-Level Git Manual

· Git Stash

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. Some of its commands include:

git stash

Which stashes current work,

git stash pop

git stash apply

Which applies your stash, but in the case of pop it also removes your stash. You can add the argument “stash@{<number>}” to either pop or apply to specify which stash to apply,

git stash list

Which lists existing stash and,

git stash clear

Which clears the stash.

· Git Revert and Git Reset

If you are not too careful with your commit and made a mistake, you do not have to worry. Using the command revert or reset can help you “go back in time”. Some of their commands include:

git reset HEAD

This command allows you to switch to the most recent commit or you can add the argument “ — filename” for a specific file,

git reset — hard

This allows you to clear any uncommitted changes to the most recent commit,

git reset — hard <commit-hash>

This fully changes to the chosen commit hash and affects the commit history and repository and,

git revert <commit-hash>

This will revert back to a commit hash and make a new commit and to find your commit hash, input the command:

git log [<options>] [<revision range>] [[ — ] <path>…​]

Show commit logs

--

--