Git Manual for Beginners

Riri Edwina
7 min readMar 20, 2021

Have you ever heard about Git? Version control system? Does it feel unfamiliar? This article will explain about Git and its basic command. Scroll down below to find more information.

Introduction to Git

Git is a version control system designed on software projects created by Linus Torvalds. A version control system is used to log any change on the project. The project might be done by an individual or a team. Git is also known as a distributed revision control which means Git’s database doesn’t store only in central storage but also all individuals involved in the project. It aims to make users easier in managing their projects either when they were online or offline.

What did Git do?

Before using Git:

source: https://www.petanikode.com/git-untuk-pemula/

After using Git:

source: https://www.petanikode.com/git-untuk-pemula/

You can see the difference between using and not using Git from the illustration above. When we want to save our newest version of a file without erasing the last version of a file we usually do a “save as” and a new file will be created, but it will make your file stacked on the folder just like the first illustration.

Git will help you to save a file in a project and its changes will be saved on the database. Git will only save the delta of the changes. Git allows us to go back to the version that we wanted.

How to use Git?

After the introduction about Git, now let’s get to know Git’s commands and how to use them. First, you should download Git so that you’ll have it on your computer. Next step, you should install Git and make sure you have a Git account.

Configuration

Make your username and password associate with commits on all repositories on your system by doing this command:

git config --global user.name “your_user_name”
git config --global user.email “youremail@yourdomain.com

To check your configuration, you can this command:

git config --list

It will show output:

user.name="your_user_name"
user.email="youremail@yourdomain.com"

Initialize a Git repository

Repository will be your workplace. We should initialize a Git repository to start working on it. You can go to a folder where you wanted to work on or created a new folder, then open the command prompt on that window. Write this command to initialize a git repo:

filepath/to/the/folder git init

You can also copy an existing repository by writing down this command:

filepath/to/the/folder git clone <git repo link>

Saving change to Repository

After changing any of your files, you should add them to your local repository to update the repo. The repo will be updated with your newest file version. Here are the commands that will be used to update your repository:

To add changes of a file:

git add <file_name>

To add changes of all file

git add .

After updating the change to a repo, you have to give a commit message for the changes you’ve made. It can be done by writing this command:

git commit -m “your commit message”

Now, your changes are ready to push. This step will update your remote repository. If your repository is a new repository, then you can use this command:

git remote add origin <link project git>

then push your changes to your remote repository by using this command:

git push origin master

“master” on the command above means you are pushing to branch “master” which is a default branch. If you wanted to push to another branch, you can change “master” to the name of your branch. And that’s a wrap! Your master branch is now up to date.

While editing your file, you might want to switch your branch or else, and come back and re-apply to them later on. Stashing will help you to solve it. It will help you to save your recent work temporarily when you aren’t ready to commit or even to undo your work.

Here is the command you can use to stash your work:

git stash

After doing this command, you can safely change your branch without discarding your unsaved work. When you ready to continue your work, you can write this command:

git stash pop

If you want to delete your unsaved work, you can write this command:

git stash drop

To see your repository status, you can use this command:

git status

Branching

https://hexquote.com/basic-git-branching/

A branch in Git is a feature in Git that allows users to create a new feature without affecting the main part of the project. Later on, a branch can be merged with the main part. Branch could be very useful in either an individual project or a team project. Here are the commands that related to branching and their function:

You can list all the branch that exists on your repository by using this command:

git branch

If you wanted to make a new branch, without checking out from your recent branch, you can use this command:

git branch <branch name>

We also can use checkout command to make a new branch, and switch to your new branch by writing this command:

git checkout -b <branch name>

Checkout command is used to switch from one branch to another, here is the command you can use to switch your branch:

git checkout <branch name>

Rebasing a project

Rebasing is a process of moving sequences of commits to a new commit. If you make a new branch and create a commit, and your friend also makes a new commit on the master branch then you do rebase on master. The history from the branch will follow the latest master commit followed by a commit on your branch.

Here are the commands to do git rebase:

# Create a feature branch based off of master 
git checkout -b feature_branch master
# Edit files
git commit -a -m “Adds new feature”
git rebase <base>git rebase --interactive <base>

Merging the project

source: https://stackoverflow.com/questions/55730292/how-git-maintains-commits-from-deleted-branch

Git merge is basically merging one feature to another feature of the project. Git merge is used to combine multiple sequences of commits into one. Here are commands to merge your branches:

#Start a new feature
git checkout -b <new-branch> master
# Edit some files
git add <file>
git commit -m “Start a feature”
# Edit some files
git add <file>
git commit -m “Finish a feature”
# Develop the master branch
git checkout master
# Edit some files
git add <file>
git commit -m “Make some super-stable changes to master”
# Merge in the new-feature branch
git merge <new-branch>
git branch -d <new-feature>

Usually, a merge can cause conflict, you can resolve the conflict by editing your code on your code editor. After solving the conflict by choosing which part to keep you can re-push your work and voila! your work is updated!

Rebase vs Merge

If you are working on a public branch with your developer friends, then use git merge, because merge will save all the commits you’ve made. You can use git merge if you want to see the whole history. But if you work by yourself, such as on your own branch or project, you can use git rebase because it will make your history linear. By using git rebase you also can delete an unwanted commit, merging two or more commits, and even changing a commit message.

Thank you for staying with me until the end. That’s all the basic command that I hope would be useful for you. Good luck with your work and keep exploring!

--

--