How to use Git and GitHub in the Terminal (Mac)
A step-by-step guide for the absolute beginners
If you could relate to the meme above, the post is for you. Git helps to track changes in files and codes. In this post, we will walk through the whole process and you could try it in your next project.
Key Points:
Files in Git are in one of three states: modified, staged, or committed.
Changed files must be staged to be included in the commit.
Version could only be saved once the file is committed.
What is Git?
Git is a version control system. It helps you keep track of code or file changes. Git is mainly used to collaborate on code.
Git is usually run from the command line, but graphical user interfaces(GUI) also exist.
What is GitHub?
Git and GitHub are different things. GitHub is a website for hosting Git projects.
Part 1 : How to get started with Git
A Git repository, or repo, is a folder that Git tracks changes in, each stored in their own folder.
step 1: Check if Git is installed
(base)-iMac ~ % git --version #check Git version
git version 2.39.3 (Apple Git-145)
Step 2 : create a new folder and new file
(base)-iMac ~ % cd # change directory, back to root directory
(base)-iMac ~ % ls # list files
Applications
Desktop
(base)-iMac ~ % cd Desktop # change directory to 'Desktop'
-iMac Desktop % mkdir Story # create new directory : Story
-iMac Desktop % cd Story # Change Directory to Story
-iMac Story % touch Chapter1.txt # Create new file : chapter1.txt
-iMac Story % ls # list files under directory 'Story'
Chapter1.txt
-iMac Story % open Chapter1.txt # open file ’ chapter1.txt’
# open file ’ chapter1.txt’ in vscode
Story % open -a /Applications/Visual\ Studio\ Code.app Chapter1.txt
Step 3: create respository
Before using Git, we need to create a repository in the target directory by using command git init.
.git file is always hidden by default, in order to view hidden files in the terminal we use the command ls -a (note: there’s a space in between).
-iMac Story % git init # Create git
Initialized empty Git repository in /Users/iris/Desktop/Story/.git/
-iMac Story % ls -a # show all files including invisibles
. .. .git Chapter1.txt
-iMac Story % git status # check git status
On branch main
No commits yet
Untracked files:
(use "git add <file>..." to include in what will be committed)
Chapter1.txt
nothing added to commit but untracked files present (use "git add" to track)
Step 4: add files to be committed
-iMac Story % git add Chapter1.txt # add file to be committed
-iMac Story % git add . # add all files to be committed
-iMac Story % git status
On branch main
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: Chapter1.txt
Step 5: commit files
-iMac Story % git commit -m "Complete Chapter 1" # commit file (final)
[main (root-commit) a967740] Complete Chapter 1
1 file changed, 1 insertion(+)
create mode 100644 Chapter1.txt
-iMac Story % git log #git log
commit a967740f337dc891fa455b81ecd6acdf98c306a3 (HEAD -> main)
Author: abc <abc@gmail.com>
Date: Wed Dec 20 14:52:16 2023 +0800
Complete Chapter 1
How to manage different versions
# check the changes made in the file
-iMac Story % git diff Chapter3.txt
# retrieve the last committed version
-iMac Story % git checkout Chapter3.txt
How to remove files from staging area
-iMac Story % git rm --cached -r .
Part 2: Using GitHub from the Command Line
Step 1 : GitHub login and create new repository in GitHub
How to delete repository from GitHub
1. Click the name of the repository
2. Click setting
3. Scroll down to the bottom of the page to danger zone
4. Click delete this repository and follow the instruction
Step 2: import existing repository into GitHub
-iMac Story % git remote add origin https://github.com/semui01/storyagain.git
-iMac Story % git push -u origin main
How to update GitHub committed file
(base) -iMac Story % git add .
(base) -iMac Story % git commit -m"change doc name"
(base) -iMac Story % git push
(base) -iMac Story % git log
How to incorporate gitignore in a project
When sharing your files with others, please take note that not all files can not be shared, such as confidential info. Gitignore file is here to help. You could copy all the file names in gitignore. (note: the file names in gitignore should be exactly the same as the file names in the project including formatting)
# copy node gitignore file from GitHub gitignore page https://github.com/github/gitignore to gitignore file
(base) -iMac Story % touch .gitignore
(base) -iMac Story % git init # initiate git
Initialized empty Git repository in /Users/iris/Downloads/Test-2/.git/
(base) -iMac Test-2 % git add .
(base) -iMac Test-2 % git status
(base) -iMac Test-2 % git commit -m "Inital Commit"
How to remove .DS_Store files from a Git repository
% find . -name .DS_Store -print0 | xargs -0 git rm -f --ignore-unmatch
Update (13 April 2024):
In case you have deleted the repository from GitHub and want to upload it again, this is what you should do in the terminal to avoid error massage “error: remote origin already exists.” 1
(base) -iMac Test-2 % git remote rm origin
Thanks for reading and hope it’s helpful.
references:
https://www.freecodecamp.org/news/what-is-git-learn-git-version-control/
https://towardsdatascience.com/version-control-with-git-get-started-in-less-than-15-minutes-696b4ce7ce92
https://www.w3schools.com/git/
Mac command line cheatsheet
https://github.com/0nn0/terminal-mac-cheatsheet/tree/master?tab=readme-ov-file#english-version
Windows Command Prompt Cheatsheet https://www.cs.columbia.edu/~sedwards/classes/2015/1102-fall/Command%20Prompt%20Cheatsheet.pdf
git cheatsheet https://github.com/0nn0/git-basics-cheatsheet
Using Git and GitHub for Version Control
https://research.wou.edu/c.php?g=904510&p=6551528
https://learn.microsoft.com/en-us/devops/develop/git/what-is-git
https://stackoverflow.com/questions/1221840/remote-origin-already-exists-on-git-push-to-a-new-repository