Installing Git
homebrew http://brew.sh/
git config --global user.name 'chuhemiao'
git config --global user.email '[email protected]'
Creating a Repository
Create a directory and let Git manage it.
mkdir gitrepository
git init
Git creates the hidden .git directory.
Adding Files
- Create a file:
touch README.md
vim READEME.md
Welcome to git world!!!
git status / git status -s
git add READEME.md
git commit -m 'add a reademe file'
- Modify a file using the same workflow.
Use git diff filename to inspect changes. git diff HEAD -- readme.txt compares the working tree with the latest committed version.
Rolling Back
git reset HEAD filename
Use --hard carefully.
git reset --hard HEAD^ returns to the previous version.
git reset --hard HEAD~n returns n versions back and fails if that version does not exist.
git reset --hard commitid returns to a specified commit.
git revert commitid
After one commit and two subsequent modifications, inspect the commit history with:
git log --pretty=oneline
git reflog records operations and helps recover commit IDs so you can choose a version to return to.
Working Tree and Staging Area
- The working tree is the
gitrepositorydirectory we created. - The staging area is part of the repository represented by the
.gitdirectory generated bygit init.
The .git directory contains:
- The stage, or staging area.
- The first branch Git creates,
master. HEAD, a pointer tomaster.
git add filename adds a file's working-tree changes to the stage.
git commit commits the staged content to the current branch, here master.
Managing Changes
After a modification, use git add ,git commit to record it.
Discarding Changes
git checkout -- README.md discards working-tree changes to README.md.
If the changes have not been staged, the file returns to its repository state.
git reset HEAD README.md unstages changes and puts them back in the working tree.
If the file was staged and then changed again, checkout returns it to its staged state.
If it has already been committed with git commit, use version rollback.
In short, the file returns to its state at the latest git commit or git add.
Deleting Files
rm README.md
git rm README.md
If you only used git add and have not committed, these notes treat git rm -f README.md as unrecoverable. A file removed with rm README.md can be restored with git checkout -- README.md.
Restore staged content:
git reset HEAD README.md
git checkout -- README.md
The file is restored.
Adding a Remote
git remote add origin https://github.com/chuhemiao/baqiye.git
git push -u origin master
git pull fetches the latest code from the remote.
git clone clones code from the remote.