Git Basis

Huayu Zhang
2 min readJun 23, 2020

git concepts

  • repository: the “container” that tracks the changes (all the commits) to your project files.
  • commit: a snapshot of your project files (working tree) at a time point.
  • working tree (working directory): consists of files that you are currently working on.
  • index (staging area): compares the files in the working tree to the files in the repo (the current commit).
Untracked | Unmodified |    Modified |   Staged 
| — — git add — — — — — — — — — — — -----— ->|
| — git add — — -> |
| — edit — — -> |
| <-- git commit — — — — — — — -|
| <-- remove — — — — — — |
Untracked | Unmodified | Modified | Staged | --- git add ------------------------------------------->|
| -- git add -----> |
| -- edit -----> |
| <--- git commit -----------------|
| <--- remove ------------|

git commands

inspect

  • git log : show the list of commits
  • git log --author=<user name> : show the list of commits authored by the specified user.
  • git show : show the changes in the current commit
  • git show --stat : show the files changed in the current commit
  • git diff: show what you’ve changed but not staged.
  • git diff --staged: show what you’ve staged.
  • git status: it tells you what is your current commit. What files are staged? What files are modified but not staged? What files are untracked?
  • git status -s: show the short version of status

modify

  • git add: when a file is added, it is not tracked by git. For a untracked file, git add puts the file in track (in the working tree) and in the staging area. For a tracked and modified file, git add puts it in the staging area.
  • git checkout <commit_hash>: checkout the commit. This mean, the HEAD will be <commit_hash>
  • git checkout -- <file>: discard the change of . To discard changes of all files. Use `git reset --hard`.
  • git commit: create a new snapshot for staged changes.
  • git commit -a: stage all modified files and commit them
  • git reset [<commit>]: reset to the commit
  • git reset --hard [<commit>]: will discard all changes not commited.
  • git rm <file>: remove file from both the file system and the working tree. equivalent to rm <file> && git add .
  • git rm --cached <file>: remove it from the repo but keep it in the file system (leave it untracked).

remote

  • git clone: clone from a remote repo. two ways
  • https: git clone https://github.com/<user>/<repository name>
  • ssh: git clone ssh://git@github.com/<user>/<repository name>.git
  • git remote -v: list all remote repos and their urls
  • git pull: pull from the remote repo
  • git push: push to the remote repo

Original Post: https://gug11.github.io/2019/12/21/gitbasic.html

Reference

  1. https://www.atlassian.com/git/tutorials/setting-up-a-repository
  2. Git workflow
  3. Git Documentation

--

--