How to Use Git for Beginners

Krisna Dwitama
6 min readJun 7, 2021

--

Hello everyone, for you who want to store your code and keep in track with the version of the code, Git is the solution. I will talk more about what is Git and how to use it in this article.

What is Git?

Git is a version control system created by Linus Torvalds designed for software projects. What it means by version control system is that git can be used to log any change that has been done on the project. The project itself can be individual project or group project. Git is also work as a distributed revision control, that is Git’s database store data not only in central storage, but also in all individuals involved in the project. The purpose of the git is to make users easier in managing their projects. Git can help us to save a file in the project and also save its changes on the database. The changes that the Git will saved is only the delta of the changes. Git also allows us to go back to the earlier version that we wanted.

How to Use Git

The first thing you need is download the Git here to make it available in your computer. After that, you should install the Git that you have downloaded and create a Git account.

Git Configuration

We can make configuration about your username and password that will associate with all of your commits on all repositories that is made in your system by doing this command:

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

You can check your configuration with this command:

git config --list

and it should showing you the configuration like this:

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

Make a Git Repository

Repository is a workplace contains your working code. To start working on a Git repository, we must first initialize it. To initialize the repository, go to a folder where you wanted to work on. After that, open the command prompt on that folder. Write this command to initialize the git repository:

the/path/to/the/folder git init

or you can also make a copy of an existing Git repository by writing this command:

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

Save Your Changes to Repository

After you make your repository, you can save your file or changes you make to your repository. To do that, you must first add the changes to your local repository. After that, you can export your local repository to the Git repository online. Here are the commands to update your repository:

Add changes of a file (The file_path is relative path from the command window):

git add <file_path>

Or to add changes of all file in this folder and its subfolder:

git add .

After you add the changes, you can make a commit message for that changes:

git commit -m “your commit message”

After that, you can push your changes to online/remote repository. If you haven’t push to your remote repository, you must first add the link of your remote repository by doing this command:

git remote add origin <link project git>

then you can push to your remote repository with this command:

git push origin master

Keep in mind that “master” is the name of a branch in your repository that act as default branch in every remote repository. You can change the word “master” to the name of other branch. We will see how to make a different branch and how does git branching work later in this article.

Sometime you want to switch to another branch while keep maintaining the changes of the file in the current branch. You can use Git stash to help you save your recent work temporary when you are not intend to commit your work.

You can stash your work with this command:

git stash

That command will save your work in a “stash” for you to use later so you can safely change to another branch without losing your changes. When you come back to the branch with work in “stash”, you can continue your working with this command:

git stash pop

You can also delete the changes in the stash by doing this command:

git stash drop

You can see the repository changes status that not yet commited by doing this:

git status

Branching in Git

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

Branch in Git is like working in some task in separate workspace in one repository. We can working on new feature without affecting the main part that already done in our project. If what we do in a branch is done, we can merge it’s work changes to the main part. Here are some commands related to branching in Git:

List all the branches that exist in local repository:

git branch

If you want to make new branch in local repository without go to that branch:

git branch <branch_name>

If you want to make new branch with respect to current branch in local repository and immediately go to that branch:

git checkout -b <branch_name>

If you want to switch between branch that already exist in your repository:

git checkout <branch name>

Merging Branch

Source: https://www.atlassian.com/git/tutorials/using-branches/git-merge

Git merge is used to merging one branch to another by creating a commit in target branch based on the commit in source branch. Here are the commands for merging branch:

# Creating new feature branch based on master branch
git checkout -b new_feature master
# Making changes to the files in new feature branch
git add <file>
git commit -m “Start a feature”
git add <file>
git commit -m “Finish a feature”
# Merging the new feature branch to the master branch
git checkout master
git merge new_feature
git branch -d new_feature

Git Rebase

Source: https://cms-assets.tutsplus.com/uploads/users/585/posts/23191/image/rebase.png

You can move some commits to be a new commit. This can be very useful in case that you make a new branch and create a commit in that branch, while your friend also makes a new commit on master branch. You can do rebase on the master branch. This will make the history from the branch will follow the latest master commit and then followed by commit on your branch.

Here is the commands for git rebase:

# Creating new feature branch based on master branch
git checkout -b new_feature master
# Make changes in new feature branch files
git commit -a -m “Adds new feature”
# Do the rebase
git rebase <base>
git rebase --interactive <base>

Git Merge vs Rebase

Source: https://medium.datadriveninvestor.com/git-rebase-vs-merge-cc5199edd77c

Git merge will save all the commits you made and make you can see the whole history. This is good if you are working on a public branch with other people. On the other hand, git rebase will make your history become linear. You also can delete unwanted commit, merging many commits into one, and even change the commit message with git rebase. This is good if you work by yourself on your own branch or project.

That’s all the things I share to you this time about how to use Git. I hope this article can be a help to you. Thank you for reading this article.

Sources:
https://www.petanikode.com/git-untuk-pemula/
https://www.atlassian.com/git/tutorials/setting-up-a-repository
https://linuxize.com/post/how-to-configure-git-username-and-email/
https://www.toolsqa.com/git/branch-in-git/
https://www.atlassian.com/git/tutorials/using-branches
https://www.atlassian.com/git/tutorials/saving-changes/git-stash
https://www.atlassian.com/git/tutorials/using-branches/git-merge

--

--