(Yet another) Basic git guide

Fernando De Freitas
3 min readMar 28, 2020

--

Photo by Yancy Min on Unsplash

Although Git might sound difficult at first it’s really easy to use once you understand some of its concepts.

An extended version of this is available on (Yet another) Terminal and Git Basics Guide

Adding files to the working tree (uncommitted files)

You can add any files with changes or new to the repo by using the . wildcard. It'll add all the files included in your current folder (the folder you’re standing on in your terminal) and its subfolders.

git add .

You can also add just the files you need by doing:

git add <file_name or file_path>

I’d recommend using VS Code’s Git tab to have a more visual idea of how your working tree looks.

If you need to have files only on your machine and don’t want them to be taken into account in the commits you need to add it to the .gitignore file.

Creating a commit

To create a commit you need 2 things:

  • A commit message (if you have your files and don’t specify a message it will fail).
  • Some staged files (files in your working tree that have been “staged” using the git add command).

Important note about staging files, you can stage only some changes to a file, VS Code makes it really easy, just look for red arrows and blue/green vertical lines in the editor’s gutter.

If you need to add a long commit message, like a list with bullet points, run:

git commit

It will open the default text editor in your computer (usually Vim but you can change this) so you can type in the message and save it (if you don’t save the commit message “file” it will abort)

If you just need to add a quick one line message run:

git commit -m "<type here your commit message>"

Pushing commits

To push a commit you just need to run:

git push

Remember to always pull before pushing, this will save you an unnecessary merge commit in case there are new commits in the remote that you haven’t pulled yet.

Git is configured by default for simple matching, which means it’ll assume you are pushing to the remote branch with the same name as your current one. if it doesn’t exist it’ll create a new one on the remote side.

Checking out (switching between) branches

To switch between branches you need to run:

git checkout <branch_name>

As long as there’s a branch with the same name in the remote, that will work.

Creating new branches

When you need to create a new branch you need to add -b to the command:

git checkout -b <new_branch_name> [<branch_to_base_from>]

When using the -b option it will base the new branch off the current one by default unless you specify otherwise.

Finding out what’s the current branch

To find out your current branch run:

git branch

It’ll show you all the local branches you have for that repo. The highlighted one will be your current one.

Merging branches

To merge a branch into your current one you just need to run:

git merge <branch_you_want_to_merge_into_your_current_one>

Sometimes there will be conflicts, and it can be overwhelming, take it step by step, most current code editors/IDEs have very useful tools to help you.

In case your merge goes sideways you can always abort the merge by running:

git merge --abort

And remember: DON’T PANIC!

Useful Links

CS Visualized: Useful Git Commands

--

--

Fernando De Freitas
Fernando De Freitas

Written by Fernando De Freitas

Principal Software Engineer and Engineering Manager. Specialty Coffee Enthusiast. I write about software development, management and other stuff occasionally.

No responses yet