Quick Start Git

  1. Navigate to the directory - navigate to the project folder on the shell command line. From here, you can use Git and shell/bash commands. The line prompt for the user to enter commands is $, other lines are generated by the Shell.
username@PHS000000 MINGW64 ~
$ cd Documents/project

username@PHS000000 MINGW64 ~Documents/project
$

You can also navigate the project folder using Windows Explorer, right click, and select “Git Bash Here”, this will open a command-line interface for that folder.

 

  1. Initialise Git - in the command line enter git init. Git is now initialised inside that project folder/directory and can track any files or sub-folders.
username@PHS000000 MINGW64 ~/Documents/project
$ git init
Initialized empty Git repository in C:/Users/username/Documents/project/.git/

username@PHS000000 MINGW64 ~/Documents/project (main)
$ 

You’ll notice that you’re on the “main” branch. For set-up purposes, we’ll continue to work on the main branch but note that it’s best practice to do any work in a separate branch which is later merged into the main when ready.

The main title is commonly used as the top-level, main, branch. This used to be called master and in many projects still will be. However, as a more inclusive, and obvious, naming strategy, main is now the default and expected name.

  1. Check for changes - Git will recongise any files that have been added or changed. Enter git status to see an overview.
username@PHS000000 MINGW64 ~/Documents/project (main)
$ git status
On branch main

No commits yet

Untracked files:
  (use "git add <file>..." to include in what will be committed)
    
      .gitignore
      project.Rproj
      
nothing added to commit but untracked files present (use "git add" to track)

username@PHS000000 MINGW64 ~/Documents/project (main)
$

In this example, an R Project has been started with a .gitignore file. A .gitignore file is used to tell Git which files and folders to ignore, this is particularly important when working with GitHub and ensures that no sensitive information or data files are uploaded to GitHub. An example .gitignore file can be found here. Be aware that other file formats may also need to be considered to be added in this file.

  1. Track files - in this example Git has recognised two untracked files. In order to track them we must first stage them using git add <file name> (if the file name contains spaces you will need to put single quotation marks around it, e.g. 'file name') You can stage each file individually or use git add . to stage all files which have been detected by Git. It is usually safer to stage each file separately.
username@PHS000000 MINGW64 ~/Documents/project (main)
$ git add .gitignore

username@PHS000000 MINGW64 ~/Documents/project (main)
$ git add project.Rproj

username@PHS000000 MINGW64 ~/Documents/project (main)
$ git status
On branch main

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)
      
      new file:   .gitignore
      new file:   project.Rproj
 
   
username@PHS000000 MINGW64 ~/Documents/project (main)
$
  1. Commit changes - now, a set of staged changes can be committed using git commit -m <commit message>. When entering you commit message, ensure that it’s consise, meaningful and written in imperative mode.
username@PHS000000 MINGW64 ~/Documents/project (main)
$ git commit -m "Create R project"
[main (root-commit) 1ab2cde] Create R project
  2 files changed, 17 insertions(+)
  create mode 100644 .gitignore
  create mode 100644 project.Rproj
  
username@PHS000000 MINGW64 ~/Documents/project (main)
$

Git has now stored a snapshot of the project folder and its content at that point in time. Going forward, it will now be possible to check back through the old version of the folder via the commits and, if necessary, revert to a previous version. To see a history of commits on a branch use git log.

  1. Create a branch - when first created, a branch is an exact copy of the original folder and contents (the main branch). As you work on the project, the working branch will change but leave the main branch untouched. This means that you always retain a main copy of the project and you only merge changes when you’re satisfied that they’re ready. To create a branch use git branch <name of branch>. To switch to working on the new working branch, use git checkout <name of branch>. These two steps can be done via one command, git checkout -b <name of branch>.
username@PHS000000 MINGW64 ~/Documents/project (main)
$ git checkout -b feature
Switched to branch 'feature'

username@PHS000000 MINGW64 ~/Documents/project (feature)
$
  1. Check for changes to branch - like in step 3, you’ll do work in your project and have changes to commit, use git status as before to see an overview. Then, keep saving and committing your work using git add <file> and git commit -m <commit message>.
username@PHS000000 MINGW64 ~/Documents/project (feature)
$ git status
On branch feature
Changes not staged for commit:
  (use "git add <file>..." to include in what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)
  
      modified:   script.R
      
no changes added to commit (use "git add" and/or "git commit -a")

username@PHS000000 MINGW64 ~/Documents/project (feature)
$ git add script.R

username@PHS000000 MINGW64 ~/Documents/project (feature)
$ git commit -m "Set up new script"
[feature a1bc23d] Set up new script
  1 file changed, 1 insertion(+)
  1. Check for branch differences - when you want to merge the changes into the main branch, you’ll want to compare them first. You can see the changes made by using git diff <main branch> <working branch>. On the command line, insertions have plus (+) signs at the start of the line while any deletions will have a negative (-) sign.
username@PHS000000 MINGW64 ~/Documents/project (feature)
$ git checkout main
Switched to branch 'main'

username@PHS000000 MINGW64 ~/Documents/project (main)
$ git diff main feature
diff --git a/script.R b/script.R
new file modile 100644
index 0000000..fe32d10
--- /dev/null
+++ b/script.R
@@ -0,0 +1,1 @@
+ #This is a demo R script with no content
\ No newline at end of file

username@PHS000000 MINGW64 ~/Documents/project (main)
$
  1. Merge changes into main branch - when you’re ready to make the merge into the main branch, ensure you’re on the main branch and use git merge <working branch>.
username@PHS000000 MINGW64 ~/Documents/project (main)
$ git merge feature
Updating 111abc0..f999ed0
Fast-forward
  script.R | 1 +
  1 file changed, 1 insertion (+)
  create mode 100644 script.R
  
username@PHS000000 MINGW64 ~/Documents/project (main)
$
  1. Delete working branch - when you’re finished with your working branch delete it using git branch -d <branch name>, this will help to avoid merge conflicts. There is no need to have long-living branches; when you want to make further changes to your project, simply create a new working branch.