Skip to main content

Git Basics

What is Git?

Git is a Version Control System - VCS widely used in source code management and software development. Git provides programmers with a repository containing the entire system's change history.

Git was created by Linus Torvalds in 2005, and quickly became one of the most popular and important version management tools globally.

Some of the best Git hosting services today include Github, Gitlab, Bitbucket...

How does Git work?

Git considers stored information as a set of snapshots – capture the entire content of all files at a certain time.

When the user runs a command, Git will "capture" the information and create a snapshot and a reference to that snapshot. To achieve high efficiency, Git only stores files if the file has changed, for files that have not changed, Git will only store a link to the file in the previous version that it stored.

With this way of working, Git will make archiving easier and not waste a lot of time reviewing unnecessary information.

Why does use Git?

  • Git helps track source code changes over time, control revision history.
  • Git allows multiple developers to work on a project together, helping to limit conflicts when they edit the source code together.
  • When encountering errors or unexpected changes, Git allows reverting to the previous version.
  • Git allows developers to work offline, remotely or distributedly.
  • Git is open source, widely used and free.
  • Git makes dividing and arranging tasks easier and more effective, allowing you to proceed multiple tasks at the same time.

Important terms in Git

Repository

Repository (also known as Repo) is the place where all the source code and change history of the project are stored.
Repository has 2 types:

  • Remote Repository: Placed on git server, capable of sharing with many people.
  • Local Repository: Placed on personal computers, reserved for users.

Branch

Branch is an independent working area of the repository.
When creating a repository, a main branch (master/main) will be created. Corresponding to each task, we will create a sub-branch and work on it. Changes in the child branch do not affect the main branch, which allows multiple tasks to be proceeded at the same time in the same repository with little conflict between each other. We can also merge branches together.

Similar to Repository, Branch is also divided into 2 types:

  • Remote Branch: Saved on git server, can be shared.
  • Local branch: Saved on personal computer, can be linked to remote branch or not.

Some other terms

TermDescription
CommitA record that stores the changes made in the repo, marked with information and commit codes.
Check outSwitch between specific branches or commits.
FetchFetch new information from the remote repository but do not automatically combine it with the local repository.
ForkCreate a copy of a public repository in your git account to contribute or fix bugs.
HeadPoints to the current commit in the working branch and is usually the last commit on the branch.
IndexThe intermediate area between the working directory and the repository, where files wait to be committed.
MasterThe default name of the main branch in a repository.
MergeIncorporate the changes from another branch into the current branch.
OriginThe default name of the remote repository when you clone or copy the repository.
PullIncorporate the changes from remote repository into local repository and merge automatically.
PushPush the changes from local repository to remote repository.
RebaseMove the current chain of commits to the beginning of another branch to create a more linear history.
RemoteRemote is a copy of the repository on another computer or server.
StashTemporarily stores uncommitted changes for transfer to other work.
TagsMark a specific point in the commit history for easy access later.
UpstreamRemote repository you clone from or monitor for changes.

Basic git commands

CommandDescriptionSyntax
git configConfigure user or repository information.git config [–-global] user.name "Name"
git config [–-global] user.email "Email"
git initInitializes a new repository in the current directory.git init
git cloneCopy a repository from remote to local machine.git clone remote_repository_URL
git statusDisplays the status of files in the working directory and staging area.git status
git addPut files from working directory into staging area.git add file_name
# add all files
git add .
git commitCreate a commit from files in the staging area.git commit -m “commit message”
git pushPush commits from local repository to remote repository.git push remote_name branch_name
git pullPull new commits from the remote repository to the local repository.git pull remote_name branch_name
git branchManage branches in the repository.# see branch list
git branch
# create branch
git branch branch_name
# delete branch
git branch -d branch_name
git checkoutSwitch between specific branches or commits.git checkout branch_name
git checkout commit_hash
git stashTemporarily stores uncommitted changes for later work.git stash
# apply temporary changes
git stash apply
git mergeMerge changes from another branch into the current branch.git merge branch_name
git resetResets the state of HEAD or staging area according to a specific commit.# retain changes in staging area
git reset –soft commit_hash
*# undo changes *
git reset –hard commit_hash
git remoteManage remote repositories linked to the local repository.git remote add remote_name URL_remote
# show list of remotes
git remote -v

Things to note when working with Git

To use Git effectively, you need to note:

  • Use Git Cheat Sheets to remember commands
  • Commit continuously and regularly
  • Check before committing to Git
  • Write clear notes when committing
  • Each different task should be created and done on different branches.

References