Getting Started with Git

I’ve been using Git for a while but almost exclusively through an IDE so I thought it was about time I learnt how to use Git from the command line. I’ll be creating a simple project and uploading it to GitHub.

Install Git

Installing Git under Linux is super easy, in fact on my fresh install of Linux Mint it was installed by default.

sudo apt-get install git

Create a Git Project

In the root of your project directory run

git init

Git will tell you it has initialized an empty Git repository. Note that you can also get git to create the project directory for you but I find I usually start the project and then add it to source control. You’ll now find a “.git” folder in the root of your project.

Recommended – Create a README.md File

A readme file is not required but it can be helpful and if you are going to upload your project to GitHub I’d highly recommend it. The readme file is there to give you a quick overview of the project and tell you how to get started with it. The “md” extension indicates this file should be in markdown format which gives you some basic formatting control. GitHub uses the “README.md” to present a sort of home page for your project.

Recommended – Pick Files to Ignore

It’s quite common for projects to include files that shouldn’t be uploaded to source control. These can be excluded by adding a file called “.gitignore”. Each line in the ignore file specifies a rule for what should be ignored.

Tell Git Who You Are

You need to let Git know who you are so it can tag your commits (and you can be blamed later).

git config --global user.name “name”
git config --global user.email email@example.com

Add and Commit Files to Git

Git has a two stage commit process. The first stage is to add the file to a staging area and then you commit it with a message.

git add .
git status
git commit -m <message>

The “git status” isn’t strictly necessary but it’s good practice to review what you are about to commit. At this point the files are being versioned locally on your machine. This is acceptable if this is the only place you’ll ever work on the project and it’s being backed up. You’ll probably want to push the files to GitHub though or some other remote repository.

Pushing the Project to GitHub

I usually log into GitHub and create the repository via the web frontend at this stage. Now run the commands: Note you might need to set up a private key with before you can commit anything see here.

git remote add origin git@github.com:username/repository_name.git
git push origin main