Git Pocket Command Notes, for Beginner

Dipta Dwiyantoro
6 min readMar 21, 2021
Source : https://git-scm.com/downloads/logos

As a developer, we often work as a team with other people when we build an app whether it’s big or not. To achieve a good work flow, we need a Version Control System like Git to help us manage our work, progress, and handling error that we make when we do coding

In this article, i want to share about how git works, git setup and some useful command of git

How Git Works

Git has 3 parts of it systems called “trees”, there are :

  1. Working directory, this is the real working directory
  2. Index, act like a temporary place for files that want to be saved to the checkpoint
  3. HEAD, this is a last checkpoint pointer for the directory
Git Flow
Git System Illustration | Source : https://rogerdudler.github.io/git-guide/

So the main flow of using git are:

  1. Make change in the workspace
  2. Add the change to the index
  3. Commit all the files in the index to HEAD

Setup

Before you use git, your directory must be initialized with a git system using this command

#it will create .git folder
git init

After we initialize working directory with git, we can use git command in our directory. A directory which has initialized with git is called Repository.

If you want to clone a repository from Online Repository you can do that using this command

#this will clone the main branch of the repository
git clone <repository_url>
#this will clone specific branch that you want from the repository
git clone -b <branch_name> <repository_url>

Add File to Stage

If you want to add the file to index after you’ve done making change in the directory, use this command to do it

#this will add single file to stage
git add <file_name>
#this will add multiple file to stage by giving multiple filename
git add <file_name_1> <file_name_2> <file_name_3>
#this will add all files that have been changed in the directory
git add .

Commit

When you’ve done adding files to stage and you want to make checkpoint of your progress, commit is a correct way to do that. You can do that using this command

git commit -m "<your_commit_message>"

It will create a commit hash for identification.

For documentation, you may give a commit message to your commit. If you already commit but you want to change the commit message, you can change it using this command

git commit --amend -m "<your_new_commit_message>"

Save to Online Repository

After you’ve done committing your progress, you can upload it to your preferred Online Repository (ex: Gitlab, Github) using this command

git push origin master

If your Local repository doesn’t have any connected remote server, you can connect it using this command

git remote add origin <server_url>

The “origin” word act like a identifier for your registered Online Repository when you add a new server or push to a server, so you can change “origin” in to another word like “origin-2” or “origin-3” but you must remember the key of the server that you want to use later.

Usually, when you push to Online Repository, you will be asked the credentials for your Online Repository to authorize your “push”

Branching

When you code as a team, each person of your team have different task to do. You can handle it using branch system. Branch is a feature from git to make a new isolated place for making change without interrupt other people works

Branch Illustration | Source : https://rogerdudler.github.io/git-guide/

You can create a new branch using this command

#this will automatically create new branch and move the HEAD to the new branch
git checkout -b <your_new_branch_name>
#or you can do it manually step by step#create a new branch
git branch <your_new_branch_name>
#move to new branch
git checkout <your_new_branch_name>

In this example “checkout” command is to move the HEAD to the a branch that you choose, git checkout also can be used to move to a commit hash

If you want to delete a branch you can use this command

git branch -d <targeted_branch_name>

But before you delete that branch, you must move to different branch first

The new branch doesn’t appear in the Online Repository unless you’ve pushed it, you can push the new branch using this command

git push origin <new_branch>

If you want to sync all branch in the Online Repository to your Local Repository, you can use this command

git fetch origin

For seeing all branch that exist in your Local Repository, you can use this command

git branch

Sync & Merge the branches

To sync your branch with the Online Repository, you can use this command

git pull origin <branch_name>

To merge from another branch in your Local Repository, you can use this command

#this will merge a branch to current branch
git merge <branch_name>

Sometimes, after you merge or pull, there are some conflict in the files, you must fix that conflicted file and after that add it to the index and commit it

Check Your Changes

To check your changes in the current branch, you can use this command

#you can fill the value with branch name or commit hash or HEAD, after you've done see the difference, press q to quit
git diff <value>

Or if you want to compare two branches or two commits, you can use this format

git diff <value_a> <value_b>

Commit History

To see the history of your commits, use this command

git log

Undo your Commit

To undo your commit in Local Repository, you can use this command

# --soft to keep the changes
git reset --soft HEAD^
# --hard to delete the changes
git reset --hard HEAD^
# HEAD^ is the last commit of your local repository

If you already push the commit to Online Repository, you can use this command

git revert HEAD

It will create a new commit that remove the changes of the last commit, after that you may push it to the Online Repository

Remove The File Changes

If you haven’t commit your changes and want to restore the changed file to the original state in the last commit, you can use this command

git restore <file_path>

It also can be used to restore many files

git restore <file_path_1> <file_path_2> ....

Temporary Save Your Progress

If you want to move to another branch but you already make a changes and you don’t want to commit it, you can use this command to temporary save it

#this will temporary save the changes
git stash
#this will apply the temporary saved changes, execute this after you go back to the correct branch
git stash apply

Git In Our Software Project — Crowd+

In our project, we use git to help us managing and log each person progress and also create an isolated place for each person to prevent many conflict in the feature

We use the git diff feature to track our changes, we can see it in the merge request form

We use the git log feature to track the commit history and person who committed it

That’s all the explanation about some how git works, common git commands, and some example of the implementation of the commands. I hope this will be useful to everyone.

References

https://reyhanhamidi.medium.com/buku-saku-git-cheatsheet-git-bahasa-indonesia-3af42e42156e

--

--