Git Tutorial for Noob Devs
By: Figo Muhammad
Tags: Tutorial, Git, Gitflow, Version Control, Github

You can use the command git clone <HTTPS URL> to get the project's file to your local computer. The URL can be found by pressing the blue “Clone” button.

Step 3: Make the first commit in your project

Commit is another term for version in Git. Commit (verb) means to include the modified chosen/staged files in the new version/commit. For example, we have files A, B, and C. Then, we changed the contents of those files. If we commit file A and B, then the new version of the application includes the modified file A and B, but still contains the old C.

It is recommended to make the first commit after the initialization of the application where the application hasn’t been implemented yet.

git add <file name>, this command stages file(s) to be committed. We can use “.” to select all files. Thus, the command becomes git add . . git commit -m “<message>”, this command commits the staged files. The message can be anything. It is a description of the commit. Now, we have the first commit.

Step 4 (Optional): Push the project files to your online repository

Push means to upload the commits from our local computer to an online repository like GitLab so that the online repository has the latest commit and in sync with the local computer.

Commit lists of my repository

Make a new repository on the online repository service. Make reference to the online repository in the Git of our local project by adding the HTTPS URL (inside the blue clone button) of the online repository to the Git configurations of our project using git remote add <remote name> <URL>. The remote name of the online repository reference is usually called “origin”. If you cloned an existing project, you do not need to do this. git push <remote name> <branch name>, this command is going to push commits that haven’t been pushed to the online repository. I am going to explain the branch system later, just use “master”: git push origin master

That is the main cycle of the Git. You commit and push to make commits/versions of the project. The system is usually visualized using a directed acyclic graph, a circle represents a single commit. Next, I will show you how to to go back to the previous commit if to undo your works.

Undoing changes

Every commit has an ID. From the image above, you can see the ID of my commits, 7dd983b9, and so on. We can use this ID to specify our desired location when undoing changes. There are special commands to go undo changes in Git: git revert <id> and git reset <id>.

There is also a term called HEAD, which is a pointer that points to the latest commit.

git reset <ID>

This command moves HEAD to the chosen commit and unlinks the commits after it.

A => B => C => D[HEAD] (Before)

A => B[HEAD] (After git reset <ID of B>)

The reset command does not undo the changes in the files. It only makes the changes unstaged. Then, we can choose to restore the changes back to the B version in the above example or still use the changes and recommit them.

git revert <ID>

This command is different compared to git reset. It takes the modification information of the chosen commit and then applies it invertedly to the HEAD commit. Then, the command makes a new commit of the invert-modified HEAD commit. Let see the example!

A => B => C => D[HEAD] (Before)

A => B => C => D => C Revert[HEAD] (After git revert <ID of D>)

Collaboration

Collaboration is key. Application nowadays is mostly developed by a team. Using an online repository (repo) such as GitHub or GitLab enables us to collaborate with other people. I have explained the git clone command earlier. This command can be used for other team members so that each member has a copy of the project on his/her local computer.

The workflow is going to be:

Each member has their latest commit copy on his/her local computer. Each member developed some features. Each of them git commit and then git push and now they have a problem.

If member A, for example, has pushed a commit to the online repo first (earlier than other members), it makes the other members developing the application behind the latest commit. The HEAD position of the local project and the online repo is different. Hence, if the other members try to push their work to the online repo, it is going to return an error.

The other member has to “pull” commits that have been pushed by member A to their local computer so that the HEAD positions of the local and the online repo matches.

We can use git pull <remote name> <branch name> to get the latest commits from the online repo. When we pull commits from the online repo, the development works (the local commits, uncommitted works) of the application in our local computer are not deleted. It is going to be “merged” with the commits from the online repo. Merge means combine files between the two versions. The new files are going to include contains both versions.

The merging process might have a conflict. Conflict arises when two separate versions have made edits to the same line in a file, or when a file has been deleted in one version but edited in the other. Though, you can fix the conflict by adjusting the conflict files (there are signs on the files) and commit the changes.

Branching (Collaboration contd.)

Branching is another feature from Git to help collaboration between team members. This feature helps us to work in parallel with other team members without interfering with other works and also keeps the parent branch from features that are still in development. There are several “workflows” or methods in using the branching feature. I will explain two common workflows that are used.

Git Workflow I: Three-way merging

The main branch that is automatically created is the master branch. This branch is usually used to contain commits of the applications that can already be used by the users. From there, create a new branch using git branch <branch name> from a certain commit There, develop your application freely without any intervention After the development has been done, merge the development branch to the master so that the master containing the new feature To merge, first, go to the master branch using git checkout master Next, use git merge <development branch name>

Git Workflow II: Rebase

From the main branch, create a new branch using git branch <branch name> from a certain commit There, develop your application freely without any intervention After the development has been done, rebase the development branch to the master so that the master containing the new feature using git rebase master! This command will move all split commits in the current branch to the head of the target of the branch. Hence, the result of this command is a one-line commit-graph, no branch in the graph although we have two branches according to the Git (It plays with the pointers). I know the statement is confusing. Consider learning more about git rebase on other resources! Then, go to the master branch using git checkout master Lastly, use git merge <development branch name>

My Experience

In my project and usually many projects, they use three main branches: master, staging, dev branches.

Master: This contains the ready-to-be-used commits of the application. Staging: This branch functions as a development branch for developers. It may contain under-development features or developed features that haven’t been reviewed. After the commits pass the review (checking its functionality, bug-free, etc.), My team merges the staging into the master branch. Dev Branches: These are branches that are made from the staging branch. It is used to develop a certain feature of the app.

Other Useful Command

There is also this useful called git stash. This command stashes your works that haven’t been committed so you do something else, for example going to the other branch using git checkout. Then, after finishing doing other stuff, you can simply go back to the stashed works using git stash pop.

Summary

Git is a very useful thing for developing applications because it can tackle many problems. It is commonly used by many software engineers around the world. Therefore, you have to master this tool.

There several commands that you can remember:

git add <file> git commit -m “<message>” git add <remote name> <URL> git push <remote name> <branch name> git revert <commit ID> git reset <commit ID> git pull <remote name> <branch name> git checkout <branch name> git merge <branch name> git rebase <branch name> git stash git stash pop

This is just a simplified tutorial of the Git. Please learn more in other sources to help you understand Git better. Feel free to contact me for any questions and feedbacks!