Uncentive Research Notes & Markets

Getting started with Git

Git is a version control system that allows developers to track changes to their code-base and collaborate with others on software projects. Using git can greatly increase your productivity as a developer. It allows you to track and manage your code, collaborate with others, and stay organised.

One of the main benefits of using git is that it allows you to easily track changes to your code. You can see exactly what changes were made, who made them, and when they were made. Additionally, git allows you to create different branches of your code-base, allowing you to work on multiple features simultaneously without interfering with the main branch. This can save a lot of time and hassle, especially on large projects with multiple developers.

If you’re not already using git, it’s definitely worth learning and incorporating into your workflow. This post will give you a high level overview of the most common commands, which you can use as a cheat sheet once you start using it. I’ll only include one Git pun in this post, and with that warning in mind, lets git going…

Installing Git

On the Git website you can find installation instructions for your platform of choice here.

Common Commands

git config --global user.name "Firstname Surname" sets your name.

git config --global user.email "email@address.domain" sets your email address.

git config -l to show all the config parameters, including the ones above.

git init to initialises a new new git directory. If you’ve got all your code in a folder called MyCode, you’d get started by navigating into the MyCode folder in your terminal of choice and running git init to apply Git’s functionality to the files therein.

git status to show the status of changes tracked & untracked.

git add [filename] add a file to staging. A file that’s “staged” means that when you next commit your code, the files which are staged are the ones which will be updated in the Git history. Changes in files not “staged” won’t be tracked, unless you stage them.

git restore [filename] restores a file to its most recent committed state. Let’s say I’ve been working on a file and I’ve completely screwed it up. I hit backspace too enthusiastically perhaps and there’s a chunk of it missing, and no amount of CTRL+Z’ing is helping. git restore [filename] will revert the given file back to its most recent committed state.

git commit to commit staged changes. Typically when you do this, you’ll be shown a text file in which you can document the code changes you’re committing to the git repositoryGetting Started with Git. It’s really important to take time to document your changes clearly, as this can save you a lot of time down the road. How many times have you revisited code you wrote weeks or months ago and you can’t remember why it was written a certain way? Commit comments can mitigate this.

git commit -m "text" commit changes along along with the message. This is like a shortcut to the git commit step above, but where you just submit the headline summary of the change. Only recommended for small, or obvious changes, like adding something to your .gitignore file.

git commit -a stage any changes from tracked files and commit in one step. This is another shortcut, where you basically are doing git add and git commit -m in one step.

git log to show the list of commits and the info provided for each commit.

git show [commit ID] show the log for a specific commit. Each git commit has an ID and are used for identification purposes.

git log --stats show some stats regarding recent commits.

git rm [filename] stage a change to remove/delete a file.

git mv [filename] [new_filename]stage a rename or move of a file.

git diff --staged show all staged files compared to the named commit.

git add -p interactively review patches and add to the current commit.

git show -2 will show the most recent 2 commits. The number can be changed.

Ignoring Files

Oftentimes you’ll have a bunch of files in your directories that you don’t need - or don’t want - to be tracked in Git.

You can tell Git to ignore files in a directory by adding their filenames to a file called .gitignore. You usually have to create this yourself, though.

Create an empty text file in your project directory and name it .gitignore.

Once you have created your .gitignore file you can tell Git to keep track of it by doing git add .gitignore and commit it by doing git commit -m "text".

By adding the types of files you want to ignore (e.g. *.csv to ignore all csv files in the directory) or more specific files using their filenames (e.g. fileToIgnore.txt to ignore this specific file) Git will ignore them.

For example, if you were to make a change to the fileToIgnore.txt file, Git won’t show it as a file with “untracked changes” if you do git status.

Reverting Changes

git checkout allows us to revert a file to a previous state, prior to it being staged or committed.

git reset HEAD [filename] un-stages a file which has already been staged.

git commit --amend will redo the most recent commit. This can be used in the event that something was incorrect, or missing from the most recent commit. This overwrites the most recent commit log, and thus removes some audit trail, and therefore should not be used for public/shared repos.

git revert HEAD creates an inverse commit to the most recent one, to reverse changes. This will only revert to the most recent commit, so to revert to an earlier commit, you can do git revert [commit ID]. Git will attempt to match on the commit ID characters provided, so you do not have to input the entire hash commit ID - typically the first five characters are sufficient.

Branching & Merging

A branch is a variant of a git repository. It is useful when trying out a new feature or a materially different behaviour, without having to work with the master branch.

You can see a list of branches by doing git branch.

You can create a new branch by doing git branch [branch name].

You can switch to this branch by doing git checkout [branch name].

You can create a new branch and switch to it in one step by doing git checkout -b [branch name]. You can delete a branch by doing git branch -d [branch name]. You can forcibly do this by using a -D flag.

Merging a branch is when you incorporate a branch into the master branch. You can merge a branch into the master branch by doing git merge [branch name]. This can sometimes cause merge issues however. If there are merge conflicts then you can abort the merge by doing git marge --abort.

You can see a summarised view of the commit history for a repo by doing git log --graph --oneline.

Simple Use Case

I have some code I’ve been working on in a file called coolscript.py in my ~/Uncentive/CoolStuff directory. Where you might keep your own code is of course totally up to you.

(base) Uncentive[~]$ cd Coding/CoolStuff

I want to keep track of the changes I make to the script, so I am going to open my terminal and do cd ~/Uncentive/CoolStuff to navigate to the script’s location, and do git init to initiate a Git repo in that directory.

(base) Uncentive[~/Coding/CoolStuff]$ git init
hint: Using 'master' as the name for the initial branch. This default branch name
hint: is subject to change. To configure the initial branch name to use in all
hint: of your new repositories, which will suppress this warning, call:
hint: 
hint: 	git config --global init.defaultBranch <name>
hint: 
hint: Names commonly chosen instead of 'master' are 'main', 'trunk' and
hint: 'development'. The just-created branch can be renamed via this command:
hint: 
hint: 	git branch -m <name>
Initialized empty Git repository in /home/Uncentive/Coding/CoolStuff/.git/

I know for sure that I want to keep track of coolscript.py so I do git add coolscript.py and commit that file to Git by doing git commit -m "Adding coolscript.py", and hit enter.

(base) Uncentive[~/Coding/CoolStuff]$ git add coolscript.py 
(base) Uncentive[~/Coding/CoolStuff]$ git commit -m "Adding coolscript.py"
[master (root-commit) d237909] Adding coolscript.py
 1 file changed, 1 insertion(+)
 create mode 100644 coolscript.py

I decide actually that I hate how something in coolscript.py works, so in my code editor I make my changes and test them successfully. coolscript.py generates a csv file as its output, and I can now see cooloutput.csv in the directory, because I’ve run it a bunch of times while making my changes. Back in my terminal window, I do git status

(base) Uncentive[~/Coding/CoolStuff]$ git status
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
	modified:   coolscript.py

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

no changes added to commit (use "git add" and/or "git commit -a")

Given that I made changes to coolscript.py, Git is showing me this file with the preface “modified: “. I want to stage this change to be committed later on, so I again do git add coolscript.py and commit my changes.

(base) Uncentive[~/Coding/CoolStuff]$ git add coolscript.py 
(base) Uncentive[~/Coding/CoolStuff]$ git commit -m "Changing the script"
[master 0fd9a0a] Changing the script
 1 file changed, 1 insertion(+), 1 deletion(-)

Finally, I change my mind and decide actually I want to go back to the original version of coolscript.py before I made my change. So I’m going to check out a previous version of the code. To do so, I need the ID of the commit from the history, so I do git log and see my two commits…

(base) Uncentive[~/Coding/CoolStuff]$ git log
commit 0fd9a0aab1a955051880afc25c92c77785d88dc5 (HEAD -> master)
Author: Uncentive <Uncentive@protonmail.com>
Date:   Wed Dec 28 23:38:09 2022 +0100

    Changing the script

commit d237909462072c75697185ff2e49680b6f5511f7
Author: Uncentive <Uncentive@protonmail.com>
Date:   Wed Dec 28 23:36:43 2022 +0100

    Adding coolscript.py

The commit IDs are the long strings of letters and numbers which is referred to as a hash. I want the original one (commit 2 at 23:36:43 on Dec 28) so I copy the hash and do git checkout d237909462072c75697185ff2e49680b6f5511f7 which reverts the code in the directory to its previous state.

(base) Uncentive[~/Coding/CoolStuff]$ git checkout d237909462072c75697185ff2e49680b6f5511f7
Note: switching to 'd237909462072c75697185ff2e49680b6f5511f7'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by switching back to a branch.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -c with the switch command. Example:

  git switch -c <new-branch-name>

Or undo this operation with:

  git switch -

Turn off this advice by setting config variable advice.detachedHead to false

HEAD is now at d237909 Adding coolscript.py

Further Reading

Git is such a ubiquitous tool that searching for terms like “Git intro” or “Git Cheat Sheet” will likely provide you with ample resources.

Personally I found git - the simple guide to be very beginner-friendly. For those of you who want a bit more interactivity and detail, I also recommend the Introduction to Git and GitHub Coursera course.