This is an introduction for Stata users to maintain your repos using command line (Lauchpad → Others → Terminal in Mac) on a Mac device. All codes below are used with command line.

For those of you with Windows system, the steps should be similar. Here is a good tutorial.


1. Set Up


  • Create your GitHub account.

  • Download Git and Git Credential Manager with command line, you might need to install Homebrew first.

    brew install git
    brew tap microsoft/git
    brew install --cask git-credential-manager-core
  • Authenticate access to your GitHub account.

    Generate a new SSH key and add to your account. After doing this, you need to use SSH url when you clone a repo.

  • Create a test repo on GitHub without readme file.

  • Change command line current directory to your repo location, and run the code shown on GitHub.

    # check current directory
    pwd
    # change directory
    cd 'your_path_here'
    
    # Create a new repo under your destination folder, below are the codes from GitHub
    echo "# test" >> README.md
    git init
    git add README.md
    git commit -m "first commit"
    git branch -M main
    git remote add origin git@github.com:Xiao-Ying-Liu/test.git
    git push -u origin main
  • Refresh your GitHub page and there should be a README file.

You are good to use Git for version control and collaboration now!


2. Basic Commands


2.1 Create a new repo

2.1.1 Initialize Git

Suppose you have a project folder on your computer and you want to maintain it using Git/GitHub. After setting you working directory in command line, you need to initialize Git, basically telling Git that ‘Hey, this is the folder I want to do version control. Please create a repo for me.’

Type in: git init

Example: I have an assignment with folders code, output, data. Now I want to put them on GitHub so that my teammates could clone the repo and proceed to do the remaining part.

  1. I change the working directory to the assignment folder: cd /Users/user1/Desktop/assignment1

  2. Initialize the repo: git init

2.1.2 Add files and commit

So far, your repo is created, but it is still empty. Now, you need to add your files to the repo and submit them.

# Add all files
git add -A

# Commit the change (adding files)
git commit -m 'first commit'

2.1.3 Add remote access

Push your local repo to GitHub so that others can clone it, or you could view the changes.

  1. First, you need to create a repo on GitHub, name it as ‘assignment 1’.

  2. In command line, link your local repo to the remote repo

git branch -M main
git remote add origin git@github.com:Xiao-Ying-Liu/test.git

2.1.4 Push your local repo to GitHub

Now you are ready to push your files to GitHub: git push -u origin main


2.2 Maintain an existed repo

2.2.1 Invite collaborator(s)

Go to GitHub repo settings, on the left pane, choose Collaborator to invite your colleagues.

2.2.2 Git pull

Before introducing the commands to maintain a repo, here is an important command that I would like to introduce first: git pull. git pull is basically pulling your GitHub repo to your local one and keep your local up-to-date.

After your initial push, some changes may be made without your noticing (e.g., you edited your repo on GitHub, or your colleague(s) made some changes), so it is important that you pull every time before you push.

2.2.3 Maintain the repo

After you made some changes on your local repo (e.g., updated do files, added some papers, etc.):

# Add new changes
git add -A

# Submit the changes and enter a message saying what changes were made
git commit -m 'Updated cleaning.do'

# Check if your local is up-to-date
git pull

# Push the changes to GitHub
git push

There are also some advanced options for git add. You can also learn about the difference among git add -A, git add ., and git add -u here.


2.3 Check status

git status could be used to check current file status (which file is modified, deleted, untracked).


2.4 Check commit log

git log displays committed snapshots.


3. Advanced Commands


3.1 Branch

Git branch allows you to try out a new idea outside the main branch. For example, suppose you’ve reached the empirical part of your project. You wonder if a subset of the data gives you similar results as the full sample does. While this could be done directly in the main branch, you are not sure if you want to keep the results. Or, suppose you and your colleague work on a project together. One night when you are lying on the bed, you realize something is wrong in the cleaning process, you get up immediately and want to correct it and check the results. But without discussion, you are reluctant to change the code directly. In both cases, you could create a new branch and then decide (or let your colleague decide) whether to keep, remove, or merge the branch later.

3.1.1 Create a branch

First, you need to create a branch.

# create a new branch named 'subset_reg'
git branch subset_reg

Notice right now you only create a new branch name. To locate all future adds and commits to this branch, you need to select the branch with git checkout subset_reg. Then do the regular git add, git commit, git pull, git push.

3.1.2 Switch back to main branch

If you need to switch to the main branch, you need to check out to the main branch first.

git checkout main

3.1.3 Merge a branch

git merge is used to combine two branches.