A beginner’s guide to Git — part 2
Tags: Coding, Git, Beginners Guide

Hello and welcome back! In ‘A beginner’s guide to Git — part 1’, you learned all about Git. If you missed it, I’d recommend reading it first. Now I’m going to assume you’re ready to jump right in and learn how to use it!

First things first, what is the command line?

The command line is a way that you can communicate directly with your computer by typing in ‘commands’. Basically, you can tell it what to do… just without a mouse! It’s really important to have a basic understanding of how the command line works to use Git to its full potential. Find a handy guide here to learn more, but as a quick example:

cd <folder> ⇒ change directory (move to a different folder within your computer)

ls ⇒ list (this shows a list of all folders and files within the current directory)

mkdir <folder name> ⇒ make directory (make a new folder within the current directory)

There are also GUI (Graphical User Interface) tools you can use to work with Git, which some people prefer, but I think it’s important to understand what’s going on underneath first.

Ok, with that, let’s dive in!

Most useful commands

Git init — To create a new repository

Create a new directory, open it and use ‘git init’. This will create a new git repository in that directory. It won’t be connected to a remote repository, but it prepares the directory by creating some important files (you don’t need to worry about these, though you can always check out this link for more info if you’re interested).

Use ‘git init’ to create a new repository

Git clone — To copy an existing repository

Use this command to make a copy of a remote repository (a version of a project that is hosted on the Internet) on your computer:

git clone route-to-repo [optional: name-you-want-the-local-repo-to-have]

Use ‘git clone’ to copy a remote repo

Git pull — To update the local repository

Use this command to update your local repository to the latest version. For example, if someone’s made changes in the repo and you want to have them on your computer:

git pull

Use ‘git pull’ to update the local repo

Git checkout — To change branches, etc

This is a bit of a special command that can be used to do various things, but here are the most important uses you need to know! Use this command to move to a different branch. You will only be able to see the files in that new branch:

git checkout name-of-branch

Use ‘git checkout’ to move to a different branch

The result of git checkout 2!

Use this command with -b to create a new branch:

git checkout -b name-of-new-branch

Use with ‘-b’ to create a new branch

The result of using ‘git checkout -b’

You can also use this command to go to previous commits and see the state of your files at that point in the history of the branch:

git checkout [commit-id]

Use ‘git checkout <commit-id>’ to look at your file history

The result of checking out a commit

Commit IDs can look super long, but just copy and paste it and everything will be ok! For example: ’ab8748d1e04fea2d14aaef3e2d8e6c2b92817118’. Also, fun fact: Git is smart enough to figure out what commit you’re referring to even if you provide only the first few characters (as long as it is at least four characters long and unambiguous).

This will move the HEAD back to that commit (instead of moving the HEAD to a different branch), which is why the diagram says ‘HEAD detached’. I know it sounds scary, but it isn’t really! In part 1 we talked about alternate timelines, well the HEAD is a reference to where/when you are when you go back in time or to a different timeline. You need this reference so you know exactly where you are and how to get back home!

An alternative to ‘git checkout’ is the relatively new command ‘git switch’. It is only used for changing and creating branches, whereas checkout can be used for that, as well as discarding changes, restoring files and much more.

Git add — To add changes to the staging area

Use this command to add changes that you want to save. Choose specific files or add all files at once:

git add name-of-file ⇒ specific files

or

git add -A ⇒ all files

Use ‘git add’ to add changes to the staging area

Remember that this is like people moving to the platform because they want to catch the next train (or the next commit). They can always move back into the waiting room (or back to your working directory) with git reset if you change your mind.

Git commit — To commit changes to the git directory

Use this command to commit changes that you want to save. It’s important to include a message with your commit (using -m), so people know what you’ve changed:

git commit -m “Made changes to the file!”

Use ‘git commit’ to commit changes to the git directory

‘Committing’ means that you have decided the changes you’ve added are ready to share but remember, they’re still only on your computer at this point. The people have gotten on the train, but it hasn’t left the station yet!

Pro Tip 1 — Try to manage your commits well; in each commit, include only relevant file changes. This will help whoever is reviewing your code, as they’ll be able to see exactly which files you changed for a given task.

Pro tip 2 — Write clear, well-written commit messages. This is the easiest way for your colleagues to see what you’ve changed. Sure, they could go through every change you made and work it out, but using a message is a more efficient way of communicating what you’ve done. Your company will probably have guidelines on this.

Pro Tip 3 — Try not to make lots of commits changing the same file, making and undoing changes. This is a bit more advanced, so don’t worry too much, but it makes it much easier for someone to review your code! If you’re starting to feel more confident, maybe it’s time to look into more advanced Git commands like reset — soft to undo commits and group all changes together… but this is much more complicated stuff, so don’t worry if you’re just starting out.

Git push — To push your local version to the remote repository

Use this command to push your local branch to the remote version of that same branch. This will overwrite changes in the remote branch, so make sure that there are no changes before you do this! A good way to avoid this is to always do a ‘git pull’ before you do a ‘git push’.

git push

Use ‘git push’ to push your local branch to the remote repo

If there isn’t a remote version of the branch yet, it may show you an error message and ask you to ‘set the upstream branch’. To fix this, you can either copy and paste what it suggests you do, or use this shortcut:

git push -u origin name-of-local-branch

Use ‘git push -u origin’ to set the upstream branch

Once you’ve ‘pushed’, anyone who has access to the repository will be able to see your changes.

Pro Tip 4 — Never push (or merge) to Master or Develop! I’d say that this is only if you’re working on a shared project — if it’s a private project, of course you can do what you want…

Git status — To check the status of your local branch

Want to find out the status of a local branch v a remote one)?

Use this command to see the status of your branch. It will tell you if you have changes that haven’t been added, committed or pushed:

git status

Use ‘git status’ to see the status of your branch

There are various options for the current status:

There are changes in the working directory. These are changes that have been made but not yet staged.

You have changes in the working directory

2. There are changes in the staging area. These are changes that have been staged but not yet committed.

You have changes in the staging area

3. There are changes in the git directory. These are changes that have been committed, but you haven’t push the branch yet so the changes are still only in the local repo.

You have changes in the git directory

4. There are no changes! Your local branch is identical to the remote branch.

There are no changes in the local branch.

You could have changes in your working directory, changes in the staging area, committed changes ready to be pushed, or you don’t have any changes.

Git fetch — To check what’s going on in other remote branches

If you’re feeling a bit nosy, use this command to get the latest history from the server:

git fetch

Use ‘git fetch’ to update your repo

Git log — To see all commits on the current branch

Use this command to see a list of all commits made on the branch you’re currently in:

git log

Git merge — To merge a branch into your current branch

Use this command to merge a branch into the branch you’re currently in.

git merge name-of-branch-to-merge

For example, if you’re in branch 1 and you want whatever is in branch 2, you can merge branch 2 into branch 1.

Use ‘git merge’ to merge branch 2 into branch 1

Now branch 1 has all of the changes from branch 2

** Warning! Use this with caution! As I mentioned in part 1, it is common practice to use your online platform to manage merging branches (normally called pull/merge requests) instead of doing it manually with ‘git merge’. GitHub has a useful guide to setting up your first repository and how to do a pull request: https://guides.github.com/activities/hello-world/.

Conflicts

Watch out for conflicts when merging. This can come up whether you’re doing it manually or using Pull Requests, and it’s important to know what it means and how to deal with it. This can be a bit daunting at first, and depending on your situation, you might want to ask a colleague to go through your first conflict-resolution with you! I’d recommend learning about how to deal with conflicts, for example with a guide like this: https://www.git-tower.com/learn/git/ebook/en/command-line/advanced-topics/merge-conflicts/

Summary

Today we’ve looked at the following:

The importance of being able to use the command line.

Git init — To create a new repository

repository Git clone — To copy an existing repository

repository Git pull — To update the local repository

the local repository Git checkout — To change branches , etc

, etc Git add — To add changes to the staging area

to the staging area Git commit — To commit changes to the git directory

to the git directory Git push — To push your local version to the remote repository

to the remote repository Git status — To check the status of your local branch

of your local branch Git fetch — To check what’s going on in other remote branches

in other remote branches Git log — To see all commits on the current branch

on the current branch Git merge — To merge a branch into your current branch

Final notes

This is by no means all of the git commands that you’ll need to know eventually, but they should get you started!

I’m going to leave you with this guide that I found in my research:

https://rogerdudler.github.io/git-guide/. It contains all the important git commands that you should know about, and it’s presented in a really user-friendly way.

Thanks for reading this blog! I really hope you’ve found it useful :) Please let me know if you notice anything I need to fix or improve, I’d love to hear from you.