Git Commands: All You Need to Remember

Hahnsang Kim
4 min readJul 2, 2021

If you are still confused about when to use which git command, this will help you jump into collaboration as experienced. But, first, I want to show you what commands you need to use in each step.

Let’s walk through the Git commands with each step in the scenario shown in Figure 1. We have two collaborators: Kelly from Desktop and John from Laptop. Each first creates a clone on their local folder. As a starter, Kelly and John are supposed to create a branch as their workspace. Kelly starts working on a cooking feature while John works on the recipe documentation. An emergency happens to Kelly. Kelly needs to implement ramen immediately. So, Kelly puts the cooking feature on hold and switch to the ramen feature. Kelly completes ramen and merges it to the develop branch.

Meanwhile, John also completes the recipe documentation and is ready to commit it. Before doing this, Kelly makes sure any change in the develop branch has occurred since branched out last time. Once the develop branch is up-to-date on the cloud, the team merges it into the production code. Let’s get into the Git commands.

Figure 1. Git Command Exchange

There is an implication you know which branch you are on currently. You also make sure the branch you are about to start with is up to date since you have a local version of the repository.

| switch to the develop branch

git checkout develop

| Pull the latest changes in the develop branch

git pull

Step 1: When Kelly starts working on the cooking feature, Kelly creates a new cooking branch and starts working on her code in this branch.

| Create and switch to a new branch called cooking from the develop branch

git checkout -b cooking

The same is true for John regarding his recipe branch.

git checkout -b recipe

Kelly wants to switch her work into another feature called ramen. Kelly goes back to the develop branch, and from there, Kelly creates a new branch. She always makes sure her local repository is up to date.

| Switch to the develop branch

git checkout develop

| Update your local repository

git pull

| Create and switch to a new branch called ramen from the develop branch

git checkout -b ramen

| Work on this new feature and commit as you go

git commit -m 'added gredients'

Step 2: After finishing her work on the ramen feature, Kelly commits changes, switches to the develop branch, and merges it back to the develop branch.

| Commit changes before switching

git commit -m 'finalize ramen'

| Switch to the develop branch

git checkout develop

| Merge the ramen branch into the develop branch

git merge --no-ff ramen

Step 3: Kelly pushes the local develop branch to the Cloud’s develop branch.

| Push to the remote repository (Cloud)

git push origin develop

Step 4: Meanwhile, John finishes his work on the recipe feature and is ready to merge. Before switching to the develop branch, John makes sure to commit his changes.

| Commit changes before switching

git commit -m 'finalize recipe'

| Switch to the develop branch

git checkout develop

| Update your local repository

git pull

Step 5: John merges the recipe branch into the develop branch.

| Merge the recipe branch into the develop branch

git merge --no-ff recipe

For the most part, Git makes merging changes between branches simple. However, there are some cases where Git can face a merge conflict (corresponding to the Star marker). When two branches (e.g., the ramen branch and the recipe branch)modify the same file, it happens. For example, let’s say John deleted a line that Kelly modified. Git wouldn’t know whether to delete the line or modify it. So we need to tell Git which changes to take. To learn more about merge conflicts and methods to handle them, see About merge conflicts.

Step 6: John finishes merging and pushes the local develop branch to the Cloud’s develop branch.

| Push to the remote repository (Cloud)

git push origin develop

Step 7: the team reviews all the updates, merges them from the develop branch into the master branch, and then pushes it to the master branch for the production code.

| Merge the develop branch into the master branch

git merge --no-ff develop

| Push the changes up to the remote repository

git push origin master

I walked you through git commands using cases: pull, commit, merge, and push. Remember you always make sure which branch you are on before doing git, and the git commands explained in this article are all you end up using most. Happy coding!

--

--