One of the most common technologies that new professional developers struggle with is git.

Many junior developers are eager to start using all of the powerful commands of git and usually end up getting more confused.

Here are a few commands that I recommend to anyone learning git for the first time:

clone

This downloads the entire git repo from a remote server, usually. You will get all revision history and branches.

init

This initializes a new empty git repo in the current directory. As a junior developer you are unlikely to do this. Most likely you will be cloning an existing project.

Another caveat here is that when you initialize a git repo, you will need to set the remote destination to push your code changes. But let’s not worry about it yet.

checkout

Once you have cloned a repo on your machine, you can checkout a specific branch. By default when you clone, the master branch is checked out. Most likely you will be working in some other branch. So you will check it out by typing git checkout branch-name

pull

This command pulls changes from remote server into your local copy of the repo. If you are working on a branch that another developer created then that developer needs to push that branch to the remote server, then you can pull it. And finally you can check it out. I see many junior developers not pulling before checking out a new branch.

commit

This is simple make your changes and commit your code to the repo. When you issue this command, your changes are stored in your local copy of the repo. You will need to issue push command before your changes are pushed to the remote server and only then other developers can pull your changes.

push

This pushes your local repo with all local changes to the remote server.

Many times you will need to merge changes from remote into your local repo before you can push.

merge

This is where things get a bit confusing for new developers. There are likely 3 different cases when you are merging:

2 or more developers are working on same branch

This is the simplest case. You can simply issue git pull command and it will fetch and merge remote changes. Now this can result in conflicts, which will need to be resolved manually. I will talk about conflicts later.

New release went out

It is recommended to merge master (or default) branch into your branch often especially right after any major release. This avoids any conflicts later during the Continuous Integration pipeline.

You need a feature from another branch

This is probably the worst scenario to deal with because now your branch is dependent on another branch. I prefer to avoid this scenario unless the feature is in master. The reasons for avoiding this scenario are twofold, first code reviews get hard as it is hard to tell what was changed in your branch and what was pulled in from the other branch. And then many times my code got delayed because I was waiting on the other branch.

You may have to issue pull command before merging

git pull
git merge other-branch my-branch