Git and Github Usage

Git Cheat Sheet
Git Doc
Git 教程

Git and Github

  1. Git is the open source distributed version control system that facilitates GitHub activities on your laptop or desktop.
  • It can be used to install update run uninstall package without sudo rights.
  • It can be used to create different environments for software version management. This is useful in switching different project.
  1. Github is a platform for hosting and collaborating on Git repositories.

Installation

Anaconda in Linux

See https://git-scm.com/

An example for the git installation in Linux

1
2
3
4
apt-get install libcurl4-gnutls-dev libexpat1-dev gettext \
libz-dev libssl-dev
apt-get install git
git --version

An example for the git installation using conda

1
conda install -c anaconda git

How to use conda

CONFIGURE TOOLING

Sets the name you want atached to your commit transactions

1
git config --global user.name "[name]"

Sets the email you want atached to your commit transactions

1
git config --global user.email "[email address]"

Enables helpful colorization of command line output

1
git config --global color.ui auto

CREATE REPOSITORIES

Downloads a project and its entire version history from github

1
git clone [url]

Creates a new floder named [project-name]
and init it as a local repository

1
git init [project-name]

or init the current folder

1
git init

MAKE CHANGES

stage files (in the current folder) into the repository

1
2
3
git add [file]
git add -A
git add .

Unstages the file, but preserve its contents
Like un-add one of the file

1
git reset [file]

Deletes the file from the working directory and stages the deletion
–cached is “Removes the file from version control but preserves the file locally”

1
2
git rm [file]
git rm --cached [file]

Changes the file name and prepares it for commit

1
git mv [file-original] [file-renamed]

record the staged files permanently in version history
DO COMMITS

1
git commit -m "[descriptive message]"

REDO COMMITS
Erase mistakes and craf replacement history
Undoes all commits afer [commit], preserving changes locally

1
git reset [commit]

Discards all history and changes back to the specified commit

1
git reset --hard [commit]

REVIEW HISTORY

Browse and inspect the evolution of project files

List all of the files in the current repository
A text file named .gitignore suppresses accidental versioning of
files and paths matching the specified paterns

1
2
git ls-files
git ls-files --other --ignored --exclude-standard

Lists all new or modified files to be commited

1
git status

Shows file differences not yet staged
It works only when you have add this file
but now you change it without adding again
exit the environment.

1
git diff

If it is staged (git add), you can not see
the difference anymore. But you can use this
command to see the file differences between staging and the last file version

1
git diff --staged

Shows content differences between two branches

1
git diff [first-branch]...[second-branch]

Lists version history for the current branch
List the log in breaf
Lists version history for a file, including renames

1
2
3
git log
git log --oneline
git log --follow [file]

Outputs metadata and content changes of the specified commit

1
git show [commit]

GROUP CHANGES

Name a series of commits and combine completed efforts
The master branch is created when you record (commit) the first change

Lists all local branches in the current repository

1
git branch

Jump to another branch, or creates a new branch and jump (if not exist)

1
2
git checkout [branch-name]
git branch [branch-name]

Combines the specified branch’s history into the current branch

1
2
git merge [branch]
git rebase [branch]

Deletes the specified branch

1
git branch -d [branch-name]

SAVE FRAGMENTS

Shelve and restore incomplete changes

Temporarily stores all modified tracked files

1
git stash

Restores the most recently stashed files

1
git stash pop

Lists all stashed changesets

1
git stash list

Discards the most recently stashed changeset

1
git stash drop

SYNCHRONIZE CHANGES

Register a repository bookmark and exchange version history

Downloads all history from the repository bookmark

1
git fetch [bookmark]

Combines bookmark’s branch into current local branch

1
git merge [bookmark]/[branch]

Uploads all local branch commits to GitHub

1
2
3
git push [alias] [branch]
git push origin dev
git push -u origin [branch]

Uploads a specific branch (dev) to GitHub, if there is no dev branch in GitHub, a new branch will be created

1
git push origin dev

Downloads bookmark history and incorporates changes

1
git pull

Glossary

git: an open source, distributed version-control system
GitHub: a platform for hosting and collaborating on Git repositories
commit: a Git object, a snapshot of your entire repository compressed into a SHA
branch: a lightweight movable pointer to a commit
clone: a local version of a repository, including all commits and branches
remote: a common repository on GitHub that all team member use to exchange their changes
fork: a copy of a repository on GitHub owned by a different user
pull request: a place to compare and discuss the differences introduced on a branch with reviews, comments, integrated tests, and more
HEAD: representing your current working directory, the HEAD pointer can be moved to different branches, tags, or commits when using git checkout