Life as a developer involves using Git all the time. When I started my first software job in 1999 (OMG that sounds like a long time ago) Git did not exist. You just had copies of folders on your machine called mywebsite, mywebsite-copy, mywebsite-copy1…. you get the picture. And that worked to a point when all you were dealing with was a bit of HTML, and some images. In 2005 Git was introduced and gradually became irreversibly entwined in the life of a software developer. You can’t be a dev now without knowing Git, and I can’t imagine life building software without it. However, if you’re new to life as a dev and you don’t know the answer to the question “What is Git“, or “What is a git repository” it can be a bit uncomfortable to ask your new team mates that question!
Who is this post for?
This post is intended as a plain English introduction to Git for anyone starting out, for anyone who doesn’t know what Git is, and for anyone else who needs a refresher, or who wants clarification for what they think they know. It’s the post I wish I had found when I joined the first dev team I ever worked with who used Git.
What is git?
Git is a piece of software that’s used to track changes in files. In it’s simplest form that’s all it is. It has no front end, you interact with it via the command line using tools such as Git Bash, Command (Windows), or Terminal (Mac). There are other ways of interacting with it – using Visual Studio Code for example – but I think it’s worth understanding Git on the command line before you use any other tool, so this post will concentrate only on the command line.
Before you run any command written in this post I’ll presume you’ve opened your command line tool and that you’re in the root of your project. The ‘>’ symbol represents the cursor. Don’t type this, just type the words after it! I’ll also presume after every command that you’ll press return, so I won’t write it each time.
Do I already have git installed?
You can check this in your command window – open your command tool of choice and type:
Now press return. If you DO have git installed it will return a single line response like this:
>git version 2.29.2.windows.2
If you don’t have git installed you’ll see an error message, and you’ll need to install if from here – https://git-scm.com/book/en/v2/Getting-Started-Installing-Git
What is a git repository (or git repo)?
Repo is short for repository. The literal meaning of the word repository is “a place in which things may be stored“. In this context a repository contains all of your code including a list of all the historical changes, i.e. who changed what file, when, and why. It also contains at least one branch (see below) and will probably, eventually, contain multiple branches.
When you clone a repository you are creating a copy of the remote repository and putting it on your local machine.
What are branches in git?
When you create a repo a single branch is automatically created to hold your files. It’s often called main, sometimes it’s called master. You can call it whatever you want but in this post I’ll always presume your default branch is called main. You can decide how you want to use your branches (more on that in ‘A typical workflow‘ in the next post in the series) but again I’ll presume that your most current version of your code is kept in your default branch – main.
A branch contains your whole codebase. Lets say I’m building a website and I’m running it locally (on localhost). I could have 3 branches – main, feature-one and feature-two, where feature-one contains all the code that has got feature one in it, and where feature-two contains all of the code for feature two in it. And main may contain the current version of the production code. While I’ve got feature-one selected as my current branch, when I run localhost I’ll see the website including feature one. If I then switch to the feature-two branch and run the website on localhost, I’ll see a version of the website including only feature two. That way I can test each separately and when I’m ready I can merge both of those branches into my main branch and release them to the live website.
How does git work?
It turns your project folder into a repository, and in here it will keep ‘snapshots’ of your project each time you commit any changes to any files. All of this is done locally on your computer, so you can use it for version control without even having an internet connection. However, by connecting your local code repository with a remote one, you always have a centralised code store that anyone who has permission can access, and it also serves as a remote back up of your work.
If you want to work on a new feature you’ll create a new branch to do this work on, and when your work is done, you’ll then ‘merge’ any new changes into the main branch. To start with this branch is a direct copy of main. At any given time you will have a branch ‘selected’ on your local machine, and any code changes you make will only effect the files in this branch.
That’s a simplified overview but it gives you the general picture.
Not sure what branch you are currently using?
Want to change which branch you are on?
>git checkout branchname
Want to create a new branch from your current branch?
>git branch newbranchname
Want to see a list of all your local branches?
What is github?
Github is a hosting service for git repositories and you can find it here – https://github.com/. It also provides a user interface to interact with your git repos. It does other stuff, but that’s the basic description.
There are other websites that do the same kind of thing, for instance Gitlab.
What is git bash?
Git bash is a command line tool – https://www.atlassian.com/git/tutorials/git-bash. It’s the one I prefer to use although I originally used Command (CMD) on my PC. Git bash gives you a little more information, it shows you what folder you are in, it shows you what branch you’re on. It includes colour coding whereas CMD is white text on black only. When I create a new project I open my C:\ folder in Windows explorer, right click my Repos folder and click “open with Git Bash”. That opens Git bash already in that folder. Then I can create my new git repo from there – although I may not have to, for instance if you create a new React app it will already be initialised as a local git repo. A tip for Git bash is that to paste text you should right click where you want to paste the copied text, and then select paste from the context menu that appears.
How to create a remote repo
Presuming your starting point is as described above, i.e. that you have a local code repo created but no remote repo to push it to, then you’ll need to create a remote repo and then ‘connect’ the local to the remote.
I’d recommend going to github and creating a new Repo there. Once the repo is created you see a page with all of the commands listed as follows.
>git branch -M main >git remote add origin https://github.com/EmilyChristy/learn-git-app.git >git push -u origin main
The first line renames the master branch to main.
The second line connects the local repo to the remote repo. [At this point your local repo has code in it, but the remote repo is empty.]
The last line pushes the local code to the remote repo.
What does the -M mean in git branch -M main?
The -M is a shortcut for
--move --force. Move means to rename a branch, and force means force this change even if the branch already exists. Further info can be found here – https://git-scm.com/docs/git-branch#Documentation/git-branch.txt.
You now know what git is, you understand what a git repository is, what branches are in git, and how to create a local and remote repo. My next post will cover common tasks such as staging and committing changes, pushing to a remote repo, cloning a remote repo, and an example git workflow – https://www.brainstormcreative.co.uk/git/what-is-git-git-explained-in-plain-english-part-2/.