GIT for dummies from dummies. All I need to know to say “I’m proficient with git”
Table of Contents
- Table of Contents
- Introduction : who am I and why I write this
- What is git : definition of git and what you need to know.
- GIT Must Know Command : git command and its practice
- TL;DR : too long didn’t read / summary / abstract
Introduction
First of all, I’m a computer science student and recently I’ve worked on several team project. So, I myself is pretty new with practicing the best practice and how not to messed up a git repository. I write this blog in hope you would have confidence in your GIT skill to join a bigger or more professional project. This blog is written from my learning and practice as a student. So, take it with a grain of salt.
What is GIT?
Git is a version control system program. To put it simply it keeps every change you save (a.k.a commit) as a version that you can access again later. Lets say if you write a document in example test.txt you start by writing “I’m ugly hix hix :(”. Then you change and save it to “maybe I’m ugly” and later save it again to “I’m so handsome”. Then suddenly at this point you realize you are actually ugly. but how do I change it back to the ugly version of this text, its already saved as “I’m so handsome”. For a file this small you can just write it back. But there’s no way you can do that if there’s a hundred to thousand file AND each file have hundred to thousand lines AND each save have a hundred to thousands change. With GIT you can see, compare, and even roll back to any version you ever save.
GIT can be used to coordinate works in team. you can save (a.k.a commit) in a remote (online) repository so everyone in the team can see each other changes/save (a.k.a commit).
There are several term you need to understand about the location you keep the version. First is “remote repository”, its an online repository used as the “exchange” for all the team members. Most of the time it is reside on a code hosting service like github, gitlab, etc. Second is “local repository” its the repository you keep in your side/in your device. You usually will extract all the data from the remote repository (a.k.a pull), make a change, commit the change, and then load it back to the remote repository (a.k.a push).
When you use git. Git will make a “.git” folder that keeps all the information it needs to know about every change. Git will only keep the version of file in the same level or below the folder you put the “.git”. When you pull and push git you are technically passing a folder (repository) and all file in it.
Other than that two term. you might also need to know “branch”. Branch is like a copy. Your can copy the main work and work it by yourself and later merge the solution. Or, you can even copy a copy. If you decide the modified version of the copy is bad you can just delete it without affecting the main copy. Here’s an example. Lets say you are in a team assignment with 8 question you need to answer. Your friend (A) start earlier and finish question 1 and 2. You (B) decide to participate in working on the assignment. But the paper is too small! You cant work it together in one paper. So, you copy A’s paper. You decide to work on number 7 and 8. In the middle of that, your other friend (C) also join the work and copy A’s work when he already finish 1,2,3,4,5,6. After that you merge your (B) work and your friend (A) works. Cool, seems like there is no conflict. But then C unknowingly already work on 7,8. he decide to merge it to the main answer too (A paper). Now there is a conflict. Between C paper and A paper now are both already answer question 7 and 8 with different answer. So it need to be resolved first. First it will be compared and then decided which answer will be kept.
GIT Must Know Command
below command is the command I feel commonly used and needed.
- git init
Create an empty git repository. To start a new project from scratch use git init. I will create an empty “.git” repository. - git clone
Clone a repository. Mostly used to clone (copy/download) a remote repository to local repository. Copying “.git” and all the file in the remote git repository. - git add
Add file to git index. Adding all the change you’ve done from the last version (a.k.a commit). But not committed yet just noted. - git commit
Record change to the repository. Saving a version of all the change you already add to the index by git add (commit the change). - git push
Push the change to the remote repository. Uploading your change (commit) to the remote repository. - git pull
pull the change from the remote repository. Downloading the change (any commit) from the remote repository to the local. Updating the local repository. You won’t lose file you already modify if its not changed in the remote repository. If there is a change both in the remote repository and local repository from the last time you pull there will be a conflict you need to resolve. Can be seen in the file conflicted. - git remote
Manage set of tracked repositories. “git remote” will give you the list of remote repositories you already add. “git remote add” will add remote repository to the list. - git branch
List, create, or delete branches - git checkout
Switch branches. can also be used to create a new branch. - git log
Show commits logs. can be used for git revert. - git revert
Revert some existing commits. It is used to go back to earlier commit as a new commit (not delete the latest commit).
Try this line per line to see how it works(for windows user). You need to change https://gitlab.com/irfanmaulananasution/git-practice to your own new practice repository. And fill the revert id accordingly.
F:\Documents\kuliah\PPL\gitpractice>git init Initialized empty Git repository in F:/Documents/kuliah/PPL/gitpractice/.git/ F:\Documents\kuliah\PPL\gitpractice>dir /a /b .git F:\Documents\kuliah\PPL\gitpractice>echo y|rmdir /s .git .git, Are you sure (Y/N)? y F:\Documents\kuliah\PPL\gitpractice>git clone https://gitlab.com/irfanmaulananasution/git-practice . Cloning into '.'... warning: redirecting to https://gitlab.com/irfanmaulananasution/git-practice.git/ remote: Enumerating objects: 6, done. remote: Counting objects: 100% (6/6), done. remote: Compressing objects: 100% (3/3), done. remote: Total 6 (delta 1), reused 0 (delta 0), pack-reused 0 Unpacking objects: 100% (6/6), done. F:\Documents\kuliah\PPL\gitpractice>dir /a /b .git README.md F:\Documents\kuliah\PPL\gitpractice>echo "git-practice" > test.txt F:\Documents\kuliah\PPL\gitpractice>dir /a /b .git README.md test.txt F:\Documents\kuliah\PPL\gitpractice>git remote origin F:\Documents\kuliah\PPL\gitpractice>git branch * master F:\Documents\kuliah\PPL\gitpractice>git checkout -b practice-branch Switched to a new branch 'practice-branch' F:\Documents\kuliah\PPL\gitpractice>git branch master * practice-branch F:\Documents\kuliah\PPL\gitpractice>git log commit 7c7d37e6649d7657ba0f2cfa3a012b73720d0d3f (HEAD -> practice-branch, origin/master, origin/HEAD, master) Author: Irfan Maulana Nasution <gitpractice@gmail.com> Date: Mon Mar 22 12:31:42 2021 +0000 Update README.md F:\Documents\kuliah\PPL\gitpractice>git add . F:\Documents\kuliah\PPL\gitpractice>git commit -m "git practice" [practice-branch 354c021] git practice 1 file changed, 1 insertion(+) create mode 100644 test.txt F:\Documents\kuliah\PPL\gitpractice>git push origin practice-branch git: 'credential-cache' is not a git command. See 'git --help'. warning: redirecting to https://gitlab.com/irfanmaulananasution/git-practice.git/ Enumerating objects: 4, done. Counting objects: 100% (4/4), done. Delta compression using up to 8 threads Compressing objects: 100% (2/2), done. Writing objects: 100% (3/3), 305 bytes | 305.00 KiB/s, done. Total 3 (delta 0), reused 0 (delta 0) remote: remote: To create a merge request for practice-branch, visit: remote: https://gitlab.com/irfanmaulananasution/git-practice/-/merge_requests/new?merge_request%5Bsource_branch%5D=practice-branch remote: To https://gitlab.com/irfanmaulananasution/git-practice * [new branch] practice-branch -> practice-branch F:\Documents\kuliah\PPL\gitpractice>git log commit 354c021ee912d5b911752d4eec2e1add1a7bcc5b (HEAD -> practice-branch, origin/practice-branch) Author: Irfan Maulana Nasution <gitpractice@gmail.com> Date: Sun Apr 4 00:07:40 2021 +0700 git practice commit 7c7d37e6649d7657ba0f2cfa3a012b73720d0d3f (origin/master, origin/HEAD, master) Author: Irfan Maulana Nasution <gitpractice@gmail.com> Date: Mon Mar 22 12:31:42 2021 +0000 Update README.md F:\Documents\kuliah\PPL\gitpractice>git revert 7c7d37e6649d7657ba0f2cfa3a012b73720d0d3f [practice-branch e206a85] Revert "Update README.md"
1 file changed, 1 deletion(-)
delete mode 100644 README.mdF:\Documents\kuliah\PPL\gitpractice>dir /a /b
.git
test.txtF:\Documents\kuliah\PPL\gitpractice>git log
commit e206a85dcc07c5a03c89c49b8d8e1e74af3fcfd1 (HEAD -> practice-branch)
Author: Irfan Maulana Nasution <gitpractice@gmail.com>
Date: Sun Apr 4 00:08:37 2021 +0700Revert "Update README.md"This reverts commit 7c7d37e6649d7657ba0f2cfa3a012b73720d0d3f.commit 354c021ee912d5b911752d4eec2e1add1a7bcc5b (origin/practice-branch)
Author: Irfan Maulana Nasution <gitpractice@gmail.com>
Date: Sun Apr 4 00:07:40 2021 +0700git practicecommit 7c7d37e6649d7657ba0f2cfa3a012b73720d0d3f (origin/master, origin/HEAD, master)
Author: Irfan Maulana Nasution <gitpractice@gmail.com>
Date: Mon Mar 22 12:31:42 2021 +0000Update README.md
TL;DR
Git is versioning control system program. You need to know what is “remote repository”, “local repository”, and “branch”. Understanding these command is generally enough for most project : git init, clone, pull, remote, branch, checkout, add, commit, push, log, revert.