© 2015-2019 by Zack Smith. All rights reserved.
GIT is a popular and useful tool but few people have comprehensive knowledge of it. Many aspects of its design seem arcane but because of technical debt and inertia, it's unlikely to be revised to be more intuitive.
|To add a file||git add fileName|
|To commit some changes locally||git commit|
|To push changes to the master branch||git push origin master|
|To push changes to a different remote branch||git push origin branchName|
|To merge changes on master from some other person||git pull origin master|
|To get the current status of local files||git status|
|To get the change log for the branch||git log|
|To get the change log for one file||git log -- fileName|
|To save local changes||git stash|
|To recover and merge local changes||git stash pop|
|To change branches||git checkout branchName|
|To create a new branch||git checkout -b newBranchName|
|To add a tag||git tag tagName|
|To push tags to the remote server||git push --tags|
|To check out using a tag||git checkout tags/tagName|
Files can be in 4 different places.
- Working directory.
- On a remote server.
The first three locations are on your machine. Git does not have to be used with a remote server.
git add to move a file from working directory to staging.
git commit to move a file from staging into history.
git push to move a file from history to the remote server.
Files can be in 5 different states.
- Unmodified tracked.
- Modified tracked.
- Staged tracked. (Committed)
The typical daily work process is:
git pullto fetch others people's changes.
- Edit files.
git addto move your changes from working directory to staging.
git committo move your changes from staging into history.
git pullto fetch and merge others' changes into your copy.
git pushto send the merged changes from history to the remote server.
Automatic merges sometimes introduce errors.
Just don't do it. It causes too many problems. If your work process requires rebasing on a frequent basis, you are doing something very wrong.
Stashing is useful for doing manual merges.
ResettingThere are three levels of resets:
git reset HEAD file undoes staged changes to a file.
git reset --hard HEAD~1; git push -f may undo the last push to the server.
Useful shell functions