The Bare Essential Guide to Git

One thing new programmers often see mentioned is "git", and a lot of jobs require you to know git or a similar version control tool. So what is it, and how do you learn it? 

This is NOT a git tutorial. This is the "bare essential" guide to git, just enough to get you started. I'll guide you on how to 
  • start a personal project repo on GitHub, 
  • link it with your local git repo, then have you 
  • make some changes, 
  • commit that change, 
  • push it to the remote repo (on GitHub), 
  • revert that change locally. 
This will NOT cover branches (and checkouts and pull requests and so on) and forks, or multiple contributors. You can read a more complete guide on that. This will, however, get you working with your own code repo.  

Please keep in mind that this is JUST enough to get you started using git and GitHub. It BARELY scratches the surface of the power git offers when you have multiple contributors and their code starts to conflict, and the power of pull request review to accept or reject, or to protect the repo to require a review before merging, and other features like that. This does NOT cover that. If you want to learn those features, please consult the "Pro Git" book linked at the end. It's free to read online. 

What is Git? 

Git is a "source/version control system" that tracks changes during software development. It can be used to track ANY file(s), not just source code. To put it plainly, it can tell if you've changed something recently and go back to different versions of that file, and it can do this for any file it tracks and can go back as far as it had tracked it. 

Download and install the latest version here: 

What is GitHub? 

GitHub, Inc. is a company that hosts free git repositories (usually shortened to "repos") at github.com, as well as offer enterprise users advanced functions. While often used together with git, as they are compatible, they are separate services. You CAN run git without ever using GitHub. However, git is really meant to be used with a remote repo. We will be using them together. 

Bonus Reason: a long-existing repo shows that you have a history of programming projects and sharing them, no matter how lame they look. 

Please go to https://github.com/ and register a free account for yourself now. 

As I don't know what will your username be, I'll assume it to be "myghuser". Remember to substitute your own username when you see that! 

Creating First Repo 

Click on the green "New" button to create a new repo. Make sure you are on the repositories app if you can't find it. 



Give it a name. Call it... "testrepo1". I know, not very creative. You can leave the rest of the options alone for now. Just hit "Create repository" at the bottom



Congratulations, you now have your own repository. I know, it's empty. We'll do something with it next. 

Linking it to Local

Right now, this repo is on GitHub only. You need to setup your local environment to match the "remote" repo. 

Open your Git-bash. It leaves you at a command prompt. This is a version of the "Bash Shell"

A super quick tutorial on Bash shell... 

 

For those of you who are familiar with the Windows Command Prompt (CMD) or Windows Powershell, this is not that different. The main difference is everything uses forward slash "/", not backslash "\". 

For a full list of bash shell commands, most of which you probably won't use, see here. But here are the ones you WILL use... a lot. 

mkdir -- make directory, ex: mkdir myprojects

cd -- change directory  ex: cd myprojects

pwd -- shows the current directory

ls -- lists all files in the current directory

To go up the directory tree, type cd ..  (that's c d and two periods)

You can enter program names inside the bash shell to run them. If you already installed VS Code editor, you can type CODE and it'll open that up. Same with Notepad. 

Now, back to git...

At Git-Bash, you will need to enter a few commands. 

First, go to where you want your code repo to be, using the "cd" command. Remember, use the forward slash, not a backslash. If you just want to use the default directory, which should be your user directory if you are on Windows, then do nothing. 

Then make a subdirectory for ALL your projects, such as "myprojects" in the example given above, using the mkdir command above.  

Now cd into that directory. 

user@SomePCName MINGW64 ~
$ mkdir myprojects

user@SomePCName MINGW64 ~
$ cd myprojects

user@SomePCName MINGW64 ~/myprojects
$

Now I will show you each line to type and what it does. 

git init   <-- initializes the git environment 

$ git init
Initialized empty Git repository in C:/Users/user/myprojects/.git/


git remote add origin https://github.com/myghuser/testrepo1.git <-- this makes the local git look for a "remote" repo at the address given. Remember, substitute your own repo name and username! 

This will not display anything for feedback. No error is good.  

git status <-- shows what files are being tracked. Right now, nothing. 

$ git remote add origin https://github.com/myghuser/testrepo1.git

user@SomePCName MINGW64 ~/myprojects (master)
$ git status
On branch master

No commits yet

nothing to commit (create/copy files and use "git add" to track)


Now your local repo is linked with the remote repo. Note that the prompt now shows "master" next to the directory name. That tells you this directory is linked to the master branch of the repo. 

Great! How do I use it? 

Let's create a text file with Notepad  (Mac users, use your local equivalent to create a text file in the current directory). 

(from now on, type in the commands at the bash prompt)

notepad sometext.txt

Notepad will ask if you want to create it, click on yes, enter some text like "Greatest text ever!", save it (File/Save), and exit out of the Notepad (close or File/Exit)cat some. 

ls

You will see sometext.txt. You can notepad sometext.txt again to see it's the file you just created. (Bonus Bash command: "cat sometext.txt")

$ notepad sometext.txt

user@SomePCName MINGW64 ~/myprojects (master)
$ cat sometext.txt
Greatest text ever!
user@SomePCName MINGW64 ~/myprojects (master)
$ ls
sometext.txt


git status

You will see that it has detected the new file and say it's untracked, and suggest you use "git add (filename)" to add it. So let's do that. 

$ git status
On branch master

No commits yet

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        sometext.txt

nothing added to commit but untracked files present (use "git add" to track)


git add sometext.txt

git status

$ git add sometext.txt

kscha@DESKTOP-FH7O04Q MINGW64 ~/myprojects (master)
$ git status
On branch master

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)
        new file:   sometext.txt

It has changed to "changes to be committed" and "new file: sometext.txt" meaning it is tracking changes to that file. 

Keep in mind that git add works for multiple files, in all subdirectories. And you don't even have to use individual filenames, but wildcards like *.py or *.js for Python or JavaScript files. So you can snapshot your entire project with one command. 

But we haven't "commit"ed the file, i.e. save it into the repo. Let us do that now. 

git commit

Wow, an editor popped up! What's going on? Don't worry, this is normal. This is what's known as a "commit message", where you describe the code you are committing and the changes you did. Go to the very end, and enter "Hello git!" (or whatever message you want). Save, and close the editor. You should see some additional text such as "1 file changed, 1 insertion(+)..."

NOTE: In the future, we can skip the editor step by using

git commit -m "my commit message"

Change it to whatever message you want to commit, like "Hello git!"

$ git commit -m "Hello git!"
[master (root-commit) 7c0f39a] Hello git!
 1 file changed, 1 insertion(+)
 create mode 100644 sometext.txt

git status

$ git status
On branch master
nothing to commit, working tree clean

Now git status shows "nothing to commit". 

Your local repo has now a snapshot of your file. 

Now let us sync it with your GitHub account. 

git push origin master  <-- push any changes locally to the remote called origin, and the branch called master. When it's done, give it a few seconds, then go refresh your repo on Github. Voila, it now has your file in it! 

So far, this is not very exciting. 

But let's say you had your greatest writing saved in that file, but your evil twin accessed your PC and changed it to read "Lamest Text Ever" and nothing else. (Simulate this by "notepad.exe sometext.txt" and change your text. Confirm the local copy has changed by using the cat command shown above.)

Fortunately, you have a previous version stored in git. 

git status

It will say "modified: sometext.txt". That means someone had modified the local after, but it wasn't committed. 

To revert to the previously committed version, do

git restore sometext.txt

Voila, you're back to normal. Verify it with notepad or cat

But wait, what if he wiped out your PC? 

No problem, provided you periodically pushed the committed versions to GitHub. To re-create your local repo, you can git pull the latest you had previously pushed. 

In Summary

As you reach certain minor milestones in your code, periodically commit your code into your local git, then push it to a remote git such as the one on GitHub, so you have multiple backups, should you need to revert in full or in part. 

Where do I go to learn more about Git? 


Remember, if you use the stuff in this guide, you're BARELY SCRATCHING THE SURFACE of what git can do for you. 

Comments

Popular posts from this blog

Yet another take on recursion

Problem Solving for Programmers: a Neglected Topic?

How to Solve a Problem, with Examples