Tracking verions of code by Git Version Control - [Proj:My Computer Workbench]

Introduction

For this part of My Computer Workbench, I would like to introduce you to a version control system that helps manage different versions of code.

Version control

Version control, also known as source control or revision control, is a system that records changes to a file or set of files, enabling users to recall specific versions later.

My software engineer friends cheer me on to use it instead of saving many versions of files, such as coding files from R.

Git version control meme

Git version control

I use Git as a version control tool. Git will create a directory called .git inside the folder of interest. This hidden folder will track the changes of all files inside the folder and sub-folder. Git will not interrupt your normal work; it just tracks quietly, and you just need to type a few commands to let Git record your coding version. Follow my steps to use Git.

  1. Install Git according to the recommendation.
  2. Go to the folder that you want to monitor the files via the command line.
    cd <folder-of-interest>
    
  3. Initialize Git
    git init
    

    Now that Git has started, you can see the .git folder inside the folder of interest by simply listing all files using the command ls -al.

  4. Add all files inside this folder to the staging area.
    git add .  #git add <path-to-directory-or-file> 
    

    You can add it many times.

  5. Commit the change to git.
    git commit -m "<you-message>"
    

    For your routine work, just run git add and git commit (4 to 5).

Note for Git

  1. Check status of Git
    Now the changes were sent to git. You can check the status of each step using the following command.
    git status
    
  2. Git ignore
    If you don’t want to track some files or folders, you can use git ignore by defining those in .gitignore.
    # Create `.gitignore` file using nano
    nano .gitignore
    

    Add the file name and folder that you want to ignore to the .gitignore

    # Here I use `cat .gitignore` to show what I wrote
    host-site-library/    # ignore all files under host-site-library folder
    .Rproj.user
    .Rhistory
    .RData
    .Ruserdata    # ignore all files ending with `.Rproj.user`, `.Rhistory`, `.RData`, and `.Ruserdata`. 
    
  3. Git doesn’t track my folders, even though I do all things correctly.
Git can't track the folders

I found the solution on Stack Overflow. The problem occurs because I have .git inside these two folders (I initized inside those folders). After removing the .git files inside those folders and re-running all the needed commands, it worked very well.

GitHub Repository

I’m firstly confused between Git and GitHub. I would like to clarify this first. Git is the version control tool, while GitHub is a repository where we can store files using the benefits of Git commands and its version control. GitHub provides free but limited space for the repository. Therefore, I need to use .gitignore to help me manage what will not be sent to GitHub.