部分git命令使用及注意事项
1、删除文件,直接在工作区右键删除文件/目录后,必须 git add *,然后git commit -m"---",才表示操作正确完成;git add -A 把所有修改或删除的问价添加至暂存索引。然后 提交:git commit -m "..."。
2、删除已经提交(tracked)的文件,git rm * ,之后还需git commit -m"-----",才表示操作正确完成,不需要git add。
3、我们可以在工作目录中创建一个名为 .gitignore 的文件(文件名称固定),touch .gitignore创建.gitignore文件,在此文件里面列出要忽略的文件模式。下面是一个示例:
# no .a files
.a
# but do track lib.a, even though you're ignoring .a files above
!lib.a
# only ignore the TODO file in the current directory, not subdir/TODO
/TODO
# ignore all files in the build/ directory
build/
# ignore doc/notes.txt, but not doc/server/arch.txt
doc/*.txt
# ignore all .pdf files in the doc/ directory
doc/**/*.pdf
4、git log 进人日志回滚,返回按q即可。
5、git remot / git remote -v / git remote show origin,查看本地仓库与远程仓库的对应关系
6、git remote add <shortname><url>添加一个新的远程Git仓库:git remote add origin https://...
7、git remote rm origin 删除与远程仓库关联的仓库(分支),亦即移除无效的远程仓库,并不会真正影响到远程仓库。注意这是删除远程仓库而非远程仓库的文件
8、git fetch origin master / git fetch 从远程仓库抓取(需要在本地合并)最新版本,然后用git merge origin/master进行合并。
9、git init -> git remote add origin url -> git pull origin master 直接拉取(自动在本地合并)最新版本
注意:如果当前本地仓库不是从远程仓库克隆,而是本地创建的仓库,并且仓库中存在文件,此时再从远程仓库拉 取文件的时候会报错(fatal: refusing to merge unrelated histories ),解决此问题可以在git pull命令后加入参 数--allow-unrelated-histories
10、git push origin master 推送本地版本至远程仓库。
11、git config 后的可选配置:
12、如果想省略git add 这一步,可以这样:git commit -a -m "..." 直接添加并提交。提交至本地仓库,然后进行push操作至远程,但仅限于对已有的文件进行修改(modified),新建的文件,必须先进行 git add <file>操作后git commit -m "..."。
13、git branch 查看本地分支。git branch -r 查看远程分支。git branch -a查看所有(本地和远程)分支
14、git branch b1创建b1分支,git checkout b1,切换至b1分支。
15、git checkout -b b2 :创建并切换至新建的分支。
16、把b1分支推送至远程仓库,git push origin b1,推送到远程origin仓库的b1分支。
17、删除分支
git branch -d <branchname> //删除本地分支
如果预被删分支做了改动未push或与远程分支不一致,删除时会报error: The branch 'b3' is not fully merged.
如果仍想删除的话,可以用git branch -D <branchname>.
git push origin -d <branchname>//删除远程分支
git push origin -d <branchname> <branchname>//同时删除几个远程分支
18、创建标签: git tag v01 , 显示 :git show。更新(推送)至远程仓库:git push origin <tagName>。
19、删除标签:git tag -d <tagName> 删除本地标签。删除远程标签:git push origin :refs/tags/<tagName>