GIT

GIT

Basics of git

  • a stream of snapshots

    Git thinks of its data more like a series of snapshots of a miniature filesystem.

  • nearly every operations is local

    We have the entire history of the project right there on our local disk, most operations seem almost instantaneous

  • three states

    • committed means that the data is safely stored in your local database.
    • modified means that you have changed the file but have not committed it to your database yet
    • staged means that you have marked a modified file in its current version to go into your next commit snapshot
  • three main sections of a Git project

    • working directory
    • staging area
    • .git directory(repository)

three main sections GIT

Getting Help

1
2
git help config
git add -h

Getting a GIT Repository

Two ways:

  1. You can take a local directory that is currently not under version control, and turn it into a Git repository

    1
    git init
  2. You can clone an existing Git repository from elsewhere

    1
    git clone https://github.com/libgit2/libgit2

    through https or SSH

Recording Changes to the Repository

1
2
git status
git add README.md
ignoring files

​ -> .gitignore file

diff

to see what you’ve changed but not yet staged

1
git diff

to see what you’ve staged that will go into your next commit

1
git diff --staged
commit your changes
1
2
3
4
$ git commit -m "Story 182: Fix benchmarks for speed"
[master 463dc4f] Story 182: Fix benchmarks for speed
2 files changed, 2 insertions(+)
create mode 100644 README

we could find that, the branch we commited to (master), what SHA-1 checksum the commit has (463dc4f), how many files were changed, and statistics about lines added and removed in this commit

skipping the staging area

let u skip the git add part

1
git commit -a -m "add new benchmarks"

don’t have to run git add on the file in this case before u commit. -a flag includes all changed files. This is convenient, but be helpful, sometimes this flag will cause u to include unwanted changes.

remove files
1
git rm readme.md

remove the file from your tracked files which means remove it from your staging area, and also removes the file from your working directory so you will not see it as an untracked file the next time around

1
git rm -f readme.md

If you modified the file and added it to the staging area already, must force the removal with the -f option. This is a safety feature to prevent accidental removal of data that hasn’t yet been recorded in a snapshot and that can’t be recovered from Git.

1
git rm --cached readme

Keep the file in your working tree but remove it from your staging area. In other words, you may want to keep the file on your hard drive but not have Git track it anymore.

1
git mv README.md README

rename a file in git, which equals to

1
2
3
mv README.md README
git rm README.md
git add README