How to be a Git master from scratch — Step by step instructions

Arman Kolahan
Towards Dev
Published in
7 min readFeb 20, 2022

--

How to be a Git master from scratch — Step by step instructions
How to be a Git master from scratch — Step by step instructions

Knowing how to work with Git is an essential part of programming. As a developer, git is the main tool to work in a team, which is useful for versioning the software, having the history of the code, implementing auto-deployment and other CI/CD tasks, and so on, even for individual developers. In this article, it is prepared in simple words many solutions in different common situations as instructions, which may happen in teamwork or individual development.

How to do the essentials

If it is the first time that you want to use git on your project, do not worry. You just need to be familiar with some common commands which are mostly used. In case, you have not installed git on your computer, please read this documentation to install it.

Open your terminal and go to the root folder of your project, for example:

cd my-first-project

Hint: on most of the IDEs there is a terminal that you can run your command on the root of your project.

Now, it is time to add git to your project. You just need to run the following command on your project once in order to add git to your project.

git init

Ta-da. Now you have everything you need to work with git.

The reason behind the git is to add some checkpoints on each feature we are adding to the project or each fix. These checkpoints help us to find why each part of the code is written in the project, we can revert the code to the previous checkpoints, we can find the issue in the code easier, and so on.

The checkpoints are named commit on git. Thus, we need to commit after each feature or each fix which are added to the project. Each commit has a message; the message should be informative, depending on the feature we are adding.

So, after adding each feature or fix just run the following commands on the root of the project to add a new commit:

git add .
git commit -m "<COMMIT_MESSAGE>"

For example for adding a navigation bar:

git add .
git commit -m "navigation bar"

Up to now, you just added git on your local computer. Still, you need to publish your code somewhere to be accessible by other developers, somewhere private or public.

As you may know, there are many git providers like Github and Gitlab. You just need to create an account there and create one repository for your project. After creating the repository, just run the following command once on the project root folder.

git remote add origin <REPOSITORY_ADDRESS>

For example:

git remote add origin git@github.com:User/UserRepo.git

Then, you just need to publish your code after each commit by pushing them:

git push origin master

So, you have all the essentials to work with git and publish your code in a shared environment to be accessible by other developers.

Recap

As a recap, just run once the following command on your project root:

git init
git remote add origin <REPOSITORY_ADDRESS>

and after adding each feature to the project, run the following commands to commit your change and push them.

git add .
git commit -m "<COMMIT_MESSAGE>"
git push origin master

In the following, it is outlined what is branching on git and why we need to learn it.

How to commit your change to a new branch

Pushing the changes to the master branch has drawbacks. You may push a new feature that breaks the code, and you need to revert the changes. In this case, you have some commits on the master branch which are not properly working. As a best practice, it is suggested to keep the master branch clean of unwanted codes and to avoid committing the changes on the master.

As a clean way, it is better to push each feature in a new branch (not on the master). A new branch is a copy of your master branch, you can work on that instead of touching the master.

For creating a new branch, just you need to run the following command on your project root folder (on the master branch):

git checkout -b <BRANCH_NAME>

Replace <BRANCH_NAME> with your new feature name such navigation_bar.

Next, after writing your code you can commit the same as before on your branch by the following commands

git add .
git commit -m "<COMMIT_MESSAGE>"
git push origin <BRANCH_NAME>

Then, after that, you can check if everything works properly you can merge your change to the master.

git checkout master
git pull origin master
git merge <BRANCH_NAME>
git push origin master

Or as a better solution:

Creating a pull request on Github or a merge request on Gitlab from your new branch to the master. In this case, there is a possibility to be reviewed your code by other developers. To read more about pull and merge requests click on the following links for Github and Gitlab.

You can navigate between the branches by the following command:

git checkout <BRANCH_NAME>

This is the straightforward case of working with branching. In some cases, because many developers are working on different features and trying to merge their changes on the master branch, there is a conflict possibility in some parts of the code. The following outlines how to resolve the conflicts.

How to resolve the conflicts on merging your branch

When many developers are working on different features and trying to merge their changes on the master branch, there is a conflict possibility in some parts of the code.

To resolve them, you just need to checkout to your branch:

git checkout <BRANCH_NAME>

Next, get the updated data from the origin by fetching:

git fetch

Then, rebase to have all the commits from the master on your branch.

git rebase origin/master

Now, you can find the files with the conflicts by

git status

You see some unusual >>> characters in your code that show different versions of the changes. Just keep the change that you want or merge the changes.

After resolving all the conflicts and testing that everything works well, run the commands to push your change to origin:

git add .
git rebase --continue
git push -f origin <BRANCH_NAME>

The conflicts are resolved and pushed to your branch. Now, you can merge your branch to the master with commands of the previous section.

The next section explains how to work as a pro on git branching with managing the commits.

How to manage commits as a pro

Now, it is time to know how to manage like a pro the commits on your branch. As a best practice, it is better to push each feature as a single commit in a new branch. It means that you need to squash your commits to merge all commits to one commit on your branch, or you can rearrange your commits, or delete them.

In order to squash your commits on your branch, in case you have more than one commit and you want to make them in one, just run the following command:

git rebase -i HEAD~<NUMBER_OF_COMMITS>

for example, if you committed two commits on your branch and you want to convert to one commit:

git rebase -i HEAD~2

You can check the history of your commits by git log command to find NUMBER_OF_COMMITS.

Then, you see the list of commits on your screen, something like the one below

pick 5943187 test
pick 43379f4 test2
pick 33432n3 test3

Change the pick to s for all except the first. For example:

pick 5943187 test
s 43379f4 test2
s 33432n3 test3

Then, save and exit. Based on the text editor the commands for exit and save are different ( Ctrl+o and Ctrl+x or :wq).

Now, you should set the commit message by removing the previous ones and then saving and exiting the text editor.

Hint: Do NOT run rebase on the master branch, just on your branch.

Hint: You can delete commits and rearrange them on git rebase -i. The instructions are there.

As an alternative simple solution, when you have one commit and you want to add something new to the previous commit just run the following code:

git add .
git commit --amend --no-edit
git push -f origin <BRANCH_NAME>

It means that for the first time you are committing on your branch you just run (for the first commit)

git add .
git commit -m "<COMMIT_MESSAGE>"
git push origin <BRANCH_NAME>

Then, for the next commits to add to the previous one:

git add .
git commit --amend --no-edit
git push -f origin <BRANCH_NAME>

Hint: Do NOT run git commit --amend and git push -f on the master branch, just on your branch.

Hint: be careful to NOT do git commit --amend for the first commit on your branch.

In the following, some useful extra instructions are expressed.

Extra instructions

In this section, some useful extra instructions are expressed including how to save your change without committing, use a commit from another branch, delete all you committed or uncommitted changes, and reset and keep the change.

How to save your change without committing

You can stash your uncommitted changes:

git add .
git stash

Then, later use your stashed changes:

git stash pop

How to use a commit from another branch

Just find the commit id by git log then on your branch use the following commit

git cherry-pick <COMMIT_ID>

How to delete your changes before committing

git reset --hard

How to delete your changes after committing

git reset --hard origin/master

How to reset and keep your changes

git reset --soft origin/master

Conclusion

In this article, the most important instructions to do the most of the git are presented. For sure, it is not all the things that you can do to manage the version control system. This article is written to be some kind of a handbook for developers to find the instructions that could be useful for them. In general, it is expressed how to do the essentials as a beginner and also how to do the advanced instructions for git management.

Feel free to write your comments about the article and what things are missed here. If it was useful just press the clap button multiple times :-)

--

--

Expert Frontend Developer, Proficient in TypeScript, Kotlin, PHP, and Java, and Additional Experience in Android and Backend.