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.
I change the working directory to the assignment folder:
cd /Users/user1/Desktop/assignment1
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.
First, you need to create a repo on GitHub, name it as
‘assignment 1’.
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.