Creating repository
git init
: create a repositorygit add File_Name
: add “File_Name” to repositorygit add .
: add all filesgit commit -m "message"
: commit changes and tell others what changes have been madegit commit -m "Title" -m "Description .."
: commit with a short title then long description
Way-back Machine
git status
: tell you which files have been changedgit diff
: check what content exactly has been changed in each file
Time Travelling
commitID: git uses commit ID, a hex number calculated by SHA1 to record your commit history
HEAD: HEAD is the current version, HEAD^ is the previous, HEAD^^ is the one before the previous, HEAD~100 is the last 100.
git log
: check the commit historygit reflog
: check the command historygit reset --hard CommitID
: Going back to the “Commit ID” version(e.g.
git reset --hard HEAD^
: going back to the previous version)When you go back, the “future version” will no longer appear in “
git log
“. However, you can use “git reflog
“ to trace “commit ID” from the future
Undo Changes
- messed up with working directory: use
git checkout -- File_Name
to discard changes in working directory and make “File_Name” to go back to the latest “committed” or “added” version - messed up with working directory but don’t want to delete all the changes:
git stash
saves your local modifications away and reverts the working directory to match theHEAD
commit. - messed up with working directory and added it to stage: use
git reset HEAD File_Name
to discard changes in stage but keep “File_Name” in working directory changed, therefore going back to situation 1 - committed the mess to master branch: use the Time Traveling technique in the previous section
Deleting Files
If you want to delete files that are already committed to the master branch:
- delete the file in working directory:
rm File_Name
- delete the file from git / restore the file
- delete the file from git:
git rm File_Name
&git commit
- restore the file:
git checkout -- File_Name
- delete the file from git:
Remote Repository
Change Git Remote URL
1 | git remote set-url <remote_name> <remote_url> # remote name is usually "origin" |
Change Remote Branch a Local Branch is Tracking
1 | git branch <local_branch_name> --set-upstream-to <remote_name>/<remote_branch_name> |
Push to Different Remote Branch
1 | git push <remote_name> <branch_name> |
Managing Branch
Creating and Deleting Branch
- 查看分支:
git branch
- 创建分支:
git branch <name>
- 切换分支:
git checkout <name>
- 合并某分支到当前分支:
git merge <name>
- 删除分支:
git branch -d <name>
Configuration
show configuration:
git config -l --local
git config -l --global
modify configuration:
For this current repo:
1
2git config user.name "Your Name Here"
git config user.email your@email.comFor global settings:
1
2git config --global user.name "Your Name Here"
git config --global user.email your@email.com
Quick look up a specific attribute:
git config user.name
If you are using HTTPS remote, it will ask for your username and password each time you push/pull from the remote. You can then cache your credentials with the following command for 7200 seconds (2 hour). If ignore the part in the quote, the credentials will be saved forever.
1
git config --global credential.helper 'cache --timeout 7200'
Others
Keep a local copy of files, don’t update with server change:
git update-index --skip-worktree <path-name>
from StackoverflowWhen use GitHub, use https to track remote branch. SSH works weird when you have multiple accounts on your local machine.
Revert an accidental pull request: the following are 2 equivalent ways
1
2git reset --merge
git merge --abort