An Intro to Git and GitHub

Posted on: March 27, 2022
4 min read

What are Git and GitHub?

Git is a version control system which lets you track changes you make to your files over time. With Git, you can revert to various states of your files (like a time traveling machine). You can also make a copy of your file, make changes to that copy, and then merge these changes to the original copy.

GitHub is a “hub” (a place or platform) where Git users build software together. GitHub is also an hosting provider and version control platform you can use to collaborate on open source projects and share files. When you’re using GitHub, you’re working with Git beneath the hood.

How to use Git

Step 1 - Install Git

In order to use Git, you have to install it on your computer. To do this, you can download the latest version on the official website. You can download for your operating system from the options given. You can also install Git using the command line. To verify the installation, you can run this command on the command line: git --version.

Step 2 - Configure Git

Now that you have Git on your system, you’ll want to do a few things to customize your Git environment. You should have to do these things only once on any given computer.

# view all of your settings
git config --list --show-origin
# your identity
git config --global user.name "John Doe"
git config --global user.email [email protected]
# default branch
git config --global init.defaultbranch main
# your editor
git config --global core.editor vim
# alias
git config --global alias.st status
git config --global alias.co checkout
git config --global alias.ci commit
git config --global alias.br branch
git config --global alias.gl "alias.lg=log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"

Step 3 - Use Git

Basic Git Concepts:

  • Default branch name: main
  • Default remote name: origin
  • Current branch reference: HEAD
  • Parent of HEAD: HEAD^ or HEAD~1
  • Grandparent of HEAD: HEAD^^ or HEAD~2

starting a project

# init a repository
git init
git init <project-name>  # create a new directory

# downloads a project from the remote repository
git clone <project url>
# proxy
git clone https://ghproxy.com/<url>

common usage

git status  # display the status of your project
git add <file>  # add a file to the staging area

git checkout -- <file>  # discard changes in working directory
git reset <file>  # remove file from staging but keep working area unchanged

git diff <file>  # show changes between working and staging area
git diff --staged <file>  # show diff between stageing and repository

git rm <file>  # delete file from working and staging area
git rm --cached <file>  # delete file from staging area
git mv <file_old> <file_new>  # rename file

commit

git commit -m "Notes about the commit"
git commit -am "Notes"
git commit --amend -m "Notes"

reversing changes

git checkout HEAD~3  # checks out the third-to-last commit.

git reset <commit> # undo the latest commit but leave working unchanged
git reset --hard <commit> # discard all changes (Dangerous)
git reset --hard HEAD~2  # or HEAD^^

branch

git branch  # list all branches
git branch -a  # list branches include remote

git branch <new-branch-name>  # create new branch
git checkout -b <new-branch-name>  # create and switch to new branch

git checkout <branch-name>  # switch branches
git checkout -  # switch to last branch

git branch -m <new-name>  # rename a branch
git branch -d <branch-name>  # delete a branch

git merge <branch>  # merge branch to current branch

stash

git stash  # put changes of working directory into stash
git stash list  # show stashes
git stash pop  # apply stored stash into working directory
git stash drop  # delete a specific stash

Managing remote repositories

remote info

git remote
git remote -v
git remote add <name> <url>
git remote remove <name>
git remote rename <old-name> <new-name>

remote synchronization

git fetch <remote> # download all commits and branches from remote
git fetch <remote> <branch>

git pull <remote> <branch> # pull changes from a remote repository

git push <remote> <branch> # push changes to a remote repository
git push <remote> --force