Git is easier than you imagine

Abdelfattah Atef
tajawal
Published in
8 min readJul 11, 2019

We all heard about Git and in the first, we confused, but let me explain it step by step.

What is Git?

Git is a version control system and it’s like a content tracker to store contents and we always use it to store our codebase so several developers can work on the same project parallel in the same time without being afraid of code conflicts.

And they can store the code on a remote repository and they have a copy locally on their machines.

Why should all companies and developers work with Git?

Like I said above git will make the developers able to maintain, add and store a new code collectively without being afraid of the conflicts and they will be able to revert and roll back if any conflicts happened and git has concept of branching so every developer can take a branch and do his / her work in this branch and this is so important to prevent conflicts

So, now, how can we start with git?

Before talking about how we can start with, git let’s talk first about the levels of git.

So, we have 3 main levels in git:

1- Working Area

It’s the area that contains all files that I changed but git doesn’t know anything about them. and called untracked files.

2- Staging Area

It’s the area that contains all files that I changed but git knows about them. and these files are ready to be a part of the next commit.

3- Repository

The final place that we store our code in it, it’s called repository, and it contains all our files, codes and commits as well.

Working Area

So, now let's talk about some commands for each area.

So, the first thing we will create a new project folder with this command

Mkdir first-git-folder

Then, go into this project folder using:

cd first-git-folder

Then, we should add a local Git repository to the project using:

git init

Now, let’s create a simple file.txt inside the project folder:

touch file.txt

After creating it, write any text inside it manually or using a command line like:

echo 'Hello, world.' >file.txt

So now this file is in our local machine we want to move it to the next area (staging area).

But before talking about how can we move it? let’s talk firstly about branching.

So in every git repository, we have a main branch called master.

Why we should use branches?

Multiple branches are needed to help multiple developers to work together in a parallel way in the same project.

So when a developer starts working on a new feature he should take a new branch from the master branch then do it in his branch to prevent conflicts with the other guys’ codes.

And to be able to create a new branch you should run:

git checkout -b YOUR_BRANCH_NAME

And if the branch is existing and you just want to switch to this branch you can use this command:

git checkout BRANCH_NAME

If you want to list all the existing branches you can use this command:

git branch -a

Staging Area

To add this file to staging area we can run this command:

git add YOUR_FILE_NAME

So in our case, the file is called file.txt so the command will be:

git add file.txt

And if I have multiple files and I want to move them to the staging area I can use:

git add .

So . means all

And if you want to remove it you can use this command:

git rm file.txt

And to update the file name you can use:

git mv old_filename new_filename

And if you by mistake added files that you shouldn’t add them you can return it back from the staging area to the working area using this command:

git reset HEAD

and then if you want to discard all your changes in these files you can use this command

git checkout -- .

So after adding the files, we should add a message to explain what this file do? and moving to the repository area, use this command

git commit -m 'PUT YOUR MESSAGE HERE'

Now your file is in the staging area 🎉

Some rules to write a great commit message

  1. Limit the subject line to 50 characters
  2. Capitalize the subject line
  3. Do not end the subject line with a period or number
  4. Use the imperative mood in the subject line like Fix sliding issue

So Now let’s discuss some important commands that will help you.

To list all commits in your branch use:

git log

And if you want to show the changes in this commit you can use

git show commit_hash

If you apply some changes in your files then you want to know these files you can use this command

git status

It will list to you all files that have changed then you can repeat what we did above for adding these files and commit a message for them.

Remote Repository

So now to make your code in the remote repository then anyone can go to it and check your changes.

You should first pull the latest from your main branch (master) using this command:

git pull origin master

Then you can push it using this command:

git push origin YOUR_BRANCH_Name

And if there is a remote repository you want to take a copy from it you can tun this command:

git clone Repository_URL

The last step is merging your changes and branch to the main branch (master) then anyone from your teammates can pull the master branch and your changes and feature will be there in their machines.

So, first of all, you should switch to the master branch then merge your branch using this command:

git checkout mastergit merge YOUR_BRANCH_NAME

Now let’s talk about one of the most important features in git.

Stashing

It’s something like a place to store your untracked changes in it.

And always developers using the stashing if they make some changes in a branch then they want to switch to another branch to fix something so they just stash their changes then switch to another branch and make the fixes then they can switch again to their branch and apply the stashing and continue their work.

So how can you do stash your changes?
you can use this command in the first branch before switching:

git stash

And after returning back to it you can use:

git stash apply

It will apply only the last stash.

Ok, but what if there are multiple stashes and you want to select one of them but not the last one.

They can run:

git stash list

To list all stashes then you will choose the number of this stash and apply it using:

git stash apply The_Number_of_stash

To remove all stashing list

git stash clear

The development process in Git

The development process in git is so easy we have only 3 levels

1- Master branch
2- Release branch
3- Feature branch

Master branch

it has a copy of the code in the products and no one should code in the master branch directly even tech leads

Release branch

Let’s say that we divide our project into multiple releases so we will create a new branch from the master branch and let’s called it release1

Feature branch

This is the branch that contains any new feature so every developer when working on a new feature should create a new branch and take this branch from the release branch and after applying your feature.

Then let’s move to the next step and it’s called pull request

Pull request

In first don’t be confused between pull request and git pull because a pull request is a just let you compare your branch with the release branch or master so if you will compare your feature branch you will compare it with the release branch but if you will compare the release branch itself you will compare it with the master branch and your teammates can check your code and review it and put their comments for your code to make it better.

and before making any pull request try to pull latest from your base branch (Release or Master) to avoid the conflicts.

Congratulations 🎉

Now, we just finished all the basics in git so, let’s talk about some of the common issues that you may face later.

Common issues

1- If you added a wrong commit message or it not descriptive

It’s ok no problem you can just update your previous commit using this command.

git commit --amend -m 'YOUR NEW MESSAGE HERE'

2- If you didn’t add all files to the commit

Let’s say that you forget to add all files for the commit so if you have 3 files you just add and commit only 2

you can add a new commit with the new file but it’s not the nicest way you can just add this file to the previous commit without duplicate your commit using git commit --amend as well

git add <The missed file name>
git commit --amend --no-edit

--no-edit means no edits in the commit message.

3- If you added a wrong file

if this file in the stage area and you don’t commit it yet

git reset YOUR_FILE_NAME

if this file is already committed we will add a step before the above one

git reset --soft HEAD~1              # to get the file from commitgit reset YOUR_FILE_NAME             # to untrack the filegit rm YOUR_FILE_NAME                # to remove the filegit commit -m 'YOUR MESSAGE'         # to commit your message again

4- Discard local file modifications

git checkout -- YOUR_FILE_NAME

or

git checkout .

to discard all changes in all files

5- Undo local commits

git reset HEAD~1           # undo last commit, keep changesgit reset --hard HEAD~1    # undo last commit, discard changes

That’s it & About Me

I have experience in frontend development with a bachelor degree focused on the information system and I love technology and learning new things, playing football, travelling and exploring new cultures.

Feel free to connect with me on my LinkedIn to know more about my previous experience.
https://www.linkedin.com/in/abdelfatahatef/

--

--