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 ------------|
git commands
inspect
git log: show the list of commitsgit log --author=<user name>: show the list of commits authored by the specified user.
git show: show the changes in the current commitgit 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 addputs the file in track (in the working tree) and in the staging area. For a tracked and modified file,git addputs it in the staging area.git checkout <commit_hash>: checkout the commit. This mean, the HEAD will begit 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 commitgit reset --hard [<commit>]: will discard all changes not commited.
git rm <file>: remove file from both the file system and the working tree. equivalent torm <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
- https:
git remote -v: list all remote repos and their urlsgit pull: pull from the remote repogit push: push to the remote repo