关于git:git基本操作命令

6次阅读

共计 2341 个字符,预计需要花费 6 分钟才能阅读完成。

$ git init 创立 git 仓库

$ git add <filename> 将工作区文件增加进暂存区

$ git add -f <filename> 强制增加

$ git chech-ignore -v <filename> 查看 .gitignore 规定与该文件的抵触

$ git commit -m <message> 暂存区文件提交至仓库,-m为此次提交的备注

$ git status 查看仓库以后状态

$ git diff <filename> 查看文件批改内容

$ git log 查看提交历史

$ git reflog 查看命令历史

$ git reset --hard <commit-id> 回退历史版本,HEAD指向以后版本,HEAD^示意上一个版本,上 n 个版本能够写成HEAD~n

$ git checkout -- <filename> 撤销工作区的批改,留神--。如文件为被提交至暂存区,文件将回退到与版本库统一,否者回退到与暂存区统一

$ git reset HEAD <filename> 撤销暂存区的批改

$ git rm <filename> 从版本库中删除该文件

$ git branch 查看分支

$ git branch <name> 创立分支

$ git checkout <name>$ git switch <name> 切换分支

$ git checkout -b <name>$ git switch -c <name> 创立并切换到分支

$ git branch -d <name> 删除分支

$ git branch -D <name> 强行删除分支

$ git merge <name> 将分支合并到以后分支上

$ git merge --no-ff -m "merge with no-ff" <name>同样是合并分支,其中 --no-ff 参数示意禁用 Fast forward 模式,而此次合并会创立一次新的 commit,加上 -m 参数能够增加 commit 表述

$ git stash 贮存现有文件状态,保留工作现场

$ git stash list 查看 stash 保留记录

$ git stash apply$ git stash apply stash@{<n>} 复原最近保留状态或指定的 stash, 但不会删除 stash 内容。<n> 能够用 git stash list 查看

$ git stash drop 删除 stash 内容

$ git stash pop 复原并删除 stash 内容

$ git cheey-pick <commit> 将某次提交到以后分支,防止重复劳动

$ git rebase 将本地末 push 的分叉提交历史整顿成直线


与近程仓库相干指令

$ git remote add <repo-name> <server-path> 将本地仓库与近程库关联。git 的近程仓库名称 <repo-name> 默认为 origin,可自定义其余名称。

例如:$ git remote add origin git@github.com:michaelliao/learngit.git

$ git clone <server-path> 将近程库克隆到本地,个别只会克隆 master 分支

$ git checkout -b <branch-name> <repo-name>/<branch-name> 在本地建设与近程分支绝对应的分支,本地与近程分支名统一方便管理

$ git push -u <repo-name> master 将 master 分支的所有内容推送到近程库,master能够替换成其余分支,需与近程库对应。

尔后,能够应用命令 $ git push origin master 推送最新批改

$ git remote -v 查看近程库详细信息

$ git remote rm <repo-name> 删除近程库

$ git pull 抓取近程的最新提交

$ git branch --set-upstream <branch-name> <repo-name>/<branch-name> 建设本地分支与近程分支的关联,在提醒no tracking information,则阐明本地分支和近程分支的链接关系没有创立。


操作标签命令

$ git tag 查看标签

$ git tag <tagname> 在以后分支 HEAD 打上标签

$ git tag -a <tagname> -m <message> <commit-id> 在某次提醒上打上带阐明的标签,-m指阐明文字,-a指定标签名,均可选用

$ git tag -d <tagname> 删除某个标签

$ git push <repo-name> :refs/tags/<tagname> 删除一个近程标签

$ git push <repo-name> <tagname> 将某个标签推送到近程

$ git push <repo-name> --tags 将全副标签推送到近程

留神:标签总是和某个 commit 挂钩。如果这个 commit 既呈现在 master 分支,又呈现在 dev 分支,那么在这两个分支上都能够看到这个标签。


改写配置文件

$ git config --global alias.<other-name> <order> 简写命令,例如:

$ git config –global alias.unstage ‘reset HEAD’

当敲入命令:

$ git unstage test.txt

理论 Git 执行的是:

$ git reset HEAD test.txt

亦或者这样, 用 co 示意 checkoutci 示意 commitbr 示意branch

$ git config –global alias.co checkout
$ git config –global alias.ci commit
$ git config –global alias.br branch

加上 --global 是针对以后用户起作用的,如果不加,那只针对以后的仓库起作用。

每个仓库的配置文件都放在 .git/config 文件中。


该文章为集体学习 git 后的一些学习总结,如果错漏,欢送补充。更具体的内容能够到 git 教程,雪峰老师写的文档写的很棒,欢送大家围观

正文完
 0