Xiuyu Cao
  • CV
  • Research
  • Blog

Contents

  • 1 Introduction
  • 2 Basic workflow
    • 2.1 Configure Git
    • 2.2 Create an repository on GitHub
    • 2.3 Clone the repo to local
    • 2.4 Work in your working directory
    • 2.5 Git
    • 2.6 Push commits to remote
  • 3 Branches
  • 4 Other useful commands

Git and GitHub

Workflow
Coding
Workflow for using Git and GitHub for version control.
Author

Xiuyu Cao

Published

June 3, 2024

Modified

January 4, 2026

1 Introduction

When working on coding projects, it is good practice to keep track of different versions of your work. This is actually very important for monitoring progress, identifying bugs, and check previous versions when needed. Git and GitHub are widely used tools for version control.

Git is a local version control system that tracks changes in your files and allows you to commit, revert, and manage different versions of a project; while GitHub is an online platform that hosts Git repositories and provides a remote location for backing up and sharing your code. In practice, you use Git on your local computer to record changes, and use GitHub to synchronize those changes and collaborate with others.

2 Basic workflow

2.1 Configure Git

Configure Git by running the following commands in the terminal

git config --global user.name "John Smith"
git config --global user.email "johnsmith@gmail.com"
git config --global credential.helper "store --file ~/.git-credentials"

Git will save the credentials in the location you assign (i.e., "~/.git-credentials").

You can check the configured information by running one of the following commands:

git config --global --list
git config --global user.name
git config --global user.email

2.2 Create an repository on GitHub

Create a repository on GitHub to host your project and enable version control. This repository will serve as a central location for storing the code, tracking changes, and maybe collaborating with others. I would set most of my ongoing projects to private repositories before done.

2.3 Clone the repo to local

git clone https://github.com/xiuyucao/xiuyucao.github.io.git

Some useful Unix-style terminal commands commonly used on macOS:

  • pwd –> print working directory
  • cd location/directory –> change working directory
  • ls –> list the files of the working directory
    • ls -a –> show all (include hidden files)

2.4 Work in your working directory

  • cd into your working directory
  • working directory setup
    • set .gitignore to ignore files you do not want to do version control
    • I recommend organizing in the structure of scripts/ and data/

2.5 Git

  • git status -s –> check the status
  • git diff [file] –> check what is changed
  • git add [file] –> add the file to the staging area
    • git add . –> add all the files
    • git reset <file> –> remove the added file from the stage
  • git commit -m "[commit message]" –> commit the change to Git
  • git log –> show all the history Git commits
    • git log --oneline

2.6 Push commits to remote

Use git push to push the commits to the remote repository on GitHub.

3 Branches

To better organize the project, it is good practice to use branches. The main (some places called master) branch is the stable version of the code. We do not want to try new feature on this branch. That’s why we need another branch. The basic idea is we create a branch which is an isolated developing environment and try new features we like. If satisfying, we merge the branch. This is also very helpful when 2 developers working on the project at the same time.

  • Check branches: git branch
    • git branch -a - show all branches including remote ones
  • Create a branch: git branch branch1
  • Switch to a branch: git checkout branch1
    • or can create and switch at the same time: git checkout -b branch1
  • Merging branches
    • First switch to the branch we want to merge other branhes into
    • git merge branch1
  • Delete a branch: first switch to another branch, then:
    • git branch -d branch1 - for merged branches
    • git branch -D branch1 - regardless of the merge status

4 Other useful commands

  • git reset --hard HEAD –> resets the current branch to the latest commit (HEAD)
 
Xiuyu Cao