Git 常用命令 1. 创建全新仓库 1 2 3 4 git init git init [project]
2. 克隆远程仓库
3. 查看文件状态 1 2 3 4 5 git status [filename] 查看所有文件状态 git status
4. 暂存 提交 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 git add [file1] [file2] ... git add [dir ] git add . git commit -m "commit_message" git commit [file1] [file2] ... -m [message] git commit -a git commit -v git commit --amend -m [message] git commit --amend [file1] [file2] ...
5. 查看 创建 删除分支 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 git branch git branch -r git branch -a git branch -vv git branch [name] git branch -d [name] git branch -D [name] git push origin --delete [name]
6. 切换分支 1 2 3 4 5 6 7 8 9 10 11 12 13 14 git checkout [fileName] git checkout . git checkout [name] git checkout -b [name] git switch [name]
7. 拉取 推送远程分支 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 git pull git pull origin [name] git push git push origin [branch_name] git push -f origin <远程分支名> git push -u origin [本地分支名]
8. 标签 tag 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 git tag git tag -n git tag -l "1.0.*" git show [标签名] git tag "[标签名]" git tag -a [标签名] -m "[说明文字]" git tag -a "[标签名]" [commitID] git push origin [标签名] git push origin --tags git tag -d "[标签名]" git push origin --delete tag [标签名]
9. 版本回退 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 git log git reflog git reset --hard HEAD^ git reset --hard <分支id > git reset --mixed <分支id > git reset --soft <分支id >
10. 工作区暂存 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 git stash list git stash save "暂存说明" git stash apply git stash apply stash@{$num } git stash pop git stash pop stash@{$num } git stash drop stash@{$num } git stash clear
文件的四种状态 - Untracked: 未跟踪, 此文件在文件夹中, 但并没有加入到git库, 不参与版本控制. 通过git add 状态变为Staged.
- Unmodify: 文件已经入库, 未修改, 即版本库中的文件快照内容与文件夹中完全一致. 这种类型的文件有两种去处, 如果它被修改, 而变为Modified. 如果使用git rm移出版本库, 则成为Untracked文件
- Modified: 文件已修改, 仅仅是修改, 并没有进行其他的操作. 这个文件也有两个去处, 通过git add可进入暂存staged状态, 使用git checkout 则丢弃修改过, 返回到unmodify状态, 这个git checkout即从库中取出文件, 覆盖当前修改
- Staged: 暂存状态. 执行git commit则将修改同步到库中, 这时库中的文件和本地文件又变为一致, 文件为Unmodify状态. 执行git reset HEAD filename取消暂存, 文件状态为Modified