Git push -f origin medium

WAHYU ADT
UKM Heroes
Published in
8 min readOct 31, 2019

Hello ! After having a hiatus last week, i am going to continue to the next topic today. It’s time to discover GIT ! yay. In this IT project course, our group is required to use and implement the version control tools called git. This tools will track the process or the development of tasks across the group member. Code, Deployment, Readme(?), you name it all.

The benefits of git version control can be achieved by using GitLab, GitHub, or your own preferences. But in this course, it’s mandatory for us to use gitlab.cs.ui.ac.id . In gitlab or GitHub, we can have a repository where we store our task progress with master branch given as default.

Now let’s follow my short git tutorial adventure !

Initializing Git Repository

First thing first, in this short tutorial, i am using gitlab.com.

You will need to register or create an account for the first time. You can do it by yourself.

Okay so the first thing you will see (after you logged in) is the project tab

project tab

There you can find the “New Project” button with green color. You use it to create your own repository for storing your project or data. After you create you own, the home page of your repository will be shown. This includes the description, name, and few useful instruction to start doing git.

project details

Now let’s get back to your machine. Create a designated folder for this tutorial or you can use existed folder. Go inside your folder directory, and on the path bar, type cmd (for command prompt, if you keen on using powershell, type powershell instead).

cmd shortcut

it will open up the command prompt under the directory path. Initiating a git repo by running this command

git init

Config

And after that, if you are using git in your machine for the first time, you need to do this. Fill this out with username and email account connected to your gitlab account. You will be asked for the credential later on.

git config --global user.name "Your Username Here" 
git config --global user.email "youremail@email.com"

Clone

Then, we would like to connect the repository that we’ve made on the gitlab.com to the one we have in our machine by using git clone. for me it is

git clone https://gitlab.com/wahyuadt/my-project.git

git clone will copy or pull the repository that you have in gitlab.com.

Remote

The other way to do it is by using remote. Remote manages all the repositories that is not in our machine. it can be your own repository in GitLab, can be multiple repository, or people’s repository.

To add repo

git remote add origin https://gitlab.com/wahyuadt/my-project.git 

To see connected or listed repo

git remote -v

Working Around in Git Repository

After establishing the repository on you machine, you can start working on it.

Pull

In this tutorial, the repository is a brand new one, but when you have been working on it, we need to pull first. We need to make sure that you are working on the most recent changes available everytime you start to work on it. Or else, the last changes could be erased or produce some conflict that you will need to solve before continuing. Originally, git pull is a shortcut for git fetch + git merge FETCH_HEAD

The things you need to do

git pull origin master

or from your specified branch. Branch will be discussed later.

git pull origin yourbranchname

Add

Suppose you created a file Readme.md in your directory. Then you want the files to be available on the gitlab repository. the first thing to add your files to the repository is to git add. Before adding the changes or work, you might want to check which files is changed by using git status first.

git status
git add Readme.md
or
git add filename.filetype

but if you have lot of works to be added, you might want to replace the command with

git add .

With git add . all the changes will be added to the git efficiently.

Commit

I have added all the files i made, will it now be available on the gitlab ? Not yet. Adding is different from committing, adding is more like listing the files that soon will be added to git. And the commit is the confirmation or the actual addition of the files to the git tracking system. you can do commit by doing this command

git commit -m "Adding Readme.md"
or
git commit -m "Commit Message"

Then the files or changes you commited will be tracked down and connected to git. So no matter how many times you do add, if you just commiting once, git will recognize only one commit.

Push

So now it’s on gitlab ? No, just one more step before your files appears on the gitlab. it’s push time ! git push will upload or transfer your files in your machine to the repository in gitlab. you can do this

git push origin master
or
git push origin yourbranch

and voila ! you had your first push ! Now the readme files is available on the gitlab.

The Readme.md exist on gitlab

If you still confused about the add-commit-push trilogy, uses this Gofood analogy.

When you want to order some food, you will look up into the menus and eventually add your desirable menu (Git add). You can still remove or edit the quantity of your menu order at this point. After you have the final choice of food, you will proceed to order the menu (Git commit). The order that you made is processed by the driver and the restaurant. Finally, The food is being delivered to its destination (Git push).

Go Food

Some Advanced Stuff

There might be some strange stuff that i mentioned already like branch and checkout. The stuff that i put in this section is considered as tricky and require more knowledge and experience to do. All of these things is useful when you want to colaborate with people in a project. If you want to find out, keep on reading 👌.

Branch

Branch & Checkout

I’ve mentioned before that when you create a repository, you are given a branch called master branch . This branch is the main branch of the repo. When doing a group project with sharing repo to one or more members, it is recommended to use other branch than master to minimize conflict and to make fixing error easier.

To make one

git branch yourbranch

this command only create a branch. You will not suddenly moved into the branch you just created, you need to checkout to the branch you created.

git checkout yourbranch

To instantly move to the branch you just created, you can add an extension -b before the branch name.

git checkout -b mybranch

To see all available branches, use

git branch --list

Stash

Suppose you are doing your work on branch mybranch but accidentally, you need to move to branch brokenbranch to fix something. You can’t move to other branches if you still have uncommited changes or work. Then how can we solve this? By using a feature called stash. Stash is used to temporarily and safely store your works on the current branch and restore the state into the last condition. Do this :

git stash 

Then after you have done the work on brokenbranch, you can comeback and do stash apply. This will restore the mybranch and your work state when you first stashing.

git stash apply

Stash also useful when you forgot where you are working now and found out that you are in the wrong branch, you can safely move your work using this:

git stash
git checkout mybranch
git stash pop

Rebase

On the other side, other member is working on hisbranch and want to update or applying the fix that you make on brokenbranch . To make the hisbranch up to date with the new fix, he can do rebase. Rebase is to create a linear or similar working condition as the one being updated.

git rebase brokenbranch

Revert

When you are having fun with your code, you realized that there is something wrong with the code. You tried to undo or stashing the works you did right now but still, error. One conclusion, the error is from previous commit. What will u do ? Revert! Revert allows us to record or mirroring the effect of earlier commits. The earlier commits can be seen from log

git log
git revert commitnumber

Revert do not delete nor changing the history of your work. It just create a new commit or changes based on the one you choose to revert or “mirror”

Merge

merge

After a long, long time stashing branches fixing errors and reverting your branch. You might want to (finally) submitting your work to the main branch, to the master branch. We can do this by first moving to the target branch, and then merge the branch to the target

git checkout master 
# You are in master
git merge mybranch
# You merge mybranch -> master

The Git in IT Project

Coldfix Branch

The branch serve as the clean up and a rollback tools. If somehow we need to delete and revert back a lot of changes (assumed that we need to rollback some changes from all user), we need to use this branch.

Hotfix Branch

Hotfix branch serve as the bug remover tools. Like when we accidentally created a bug and error and without noticing it we merged with master. The error or bug in the master branch needs to be fixed ASAP. That’s why we have hotfix branch as our emergency.

So, that’s a brief explanation of git from me. There are still a lot more to discover. You can read more on the git documentation or other git guides across the internet. Thanks for reading !

--

--