Last updated on January 10, 2023 am
git常用命令
最最最常用
git init
git clone
git status
git add
git commit
git push
git pull
1.新建仓库
1 2 3 4 5 6
| git init //初始化仓库
git init 仓库名称 //新建并初始化仓库
git clone [url] //下载已有项目(包含历史版本,如果只克隆包含最近的一次commit的一个分支的话, 可以加 --depth=1,下载体积会相对减小)
|
2.配置
1 2 3 4 5 6 7
| git config --list
git config -e [--global]
git config [--global] user.name "[name]"
git config [--global] user.email "[email address]"
|
3.增加/删除文件
1 2 3
| git add [file1] [file2] ... git add [dir] git add ./
|
以下使用较少一些
1 2 3
| git rm [file1] [file2] ... git rm --cached [file] git mv [file-original] [file-renamed]
|
4.代码提交
1 2 3 4 5 6 7 8 9 10 11 12
| git commit -m [message]
git commit [file1] [file2] ... -m [message]
git commit -a
git commit --amend -m [message]
git commit --amend [file1] [file2] ...
|
5.分支
以下的[commit]代表git的版本号,为hash值
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
| git branch
git branch -r
git branch -a
git branch [branch-name]
git checkout -b [branch]
git branch [branch] [commit]
git branch --track [branch] [remote-branch]
git checkout [branch-name] /切换到指定分支,并更新工作区
git checkout -
git branch --set-upstream [branch] [remote-branch]
git merge [branch]
git cherry-pick [commit]
git branch -d [branch-name]
git push origin --delete [branch-name] git branch -dr [remote/branch]
|
6.标签
1 2 3 4 5 6 7 8 9 10 11
| git tag //列出所有tag
git tag [tag] //新建一个tag在当前commit
git tag [tag] [commit] //新建一个tag在指定commit
git tag -d [tag] //删除本地tag
git push origin :refs/tags/[tagName] //删除远程tag
git show [tag] //查看tag信息
|
7.查看信息
1 2 3 4 5 6 7 8 9 10 11 12 13
| git status //显示有变更的文件
git log //显示当前分支的版本历史
git log --stat //显示commit历史,以及每次commit发生变更的文件
git log -S [keyword] //搜索提交历史,根据关键词
git log --oneline --decorate --graph --all //查看分支图 // --oneline 日志单行显示 // --graph 分支图显示 // --decorate 可显示分支名称 // --all 显示所有分支
|
8.远程同步
1 2 3 4 5 6 7 8 9 10 11
| git fetch [remote]
git remote -v
git remote show [remote]
git remote add [shortname] [url]
git pull [remote] [branch]
git push [remote] [branch]
|
关于git fetch 和 git pull
笼统的来说git pull
约等于git fetch
+git merge
。
区别在于git fetch
之后需要git log -p FETCH_HEAD
来查看差异,然后通过git merge FETCH_HEAD
合并,而git pull
会将代码直接合并,造成冲突后需要手动解决。
了解更多关于git fetch git fetch详解。
9.撤销
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| git checkout [file]
git checkout [commit] [file]
git checkout .
git reset [file]
git reset --hard
git reset [commit]
git reset --hard [commit]
git reset --keep [commit]
|
查看更多
git 常用命令
git分支管理策略
git工作流程