前言
记录时间:2023.4.9
已坚持学习第42天
git教程总结
git的常用命令
1.环境配置
#设置用户信息
git config --global user.name “itcast”
git config --global user.email “itcast@itcast.cn”
#查看配置信息
git config --list
git config user.name
#通过上面的命令设置的信息会保存在~/.gitconfig文件中
2.初始化本地仓库 init
# 初始化仓库带工作区
git init
# 初始化仓库不带工作区
git init --bare
3.克隆 clone
# 从远程仓库克隆
git clone 远程Git仓库地址
例如: git clone https://gitee.com/git/cs.git
4.查看状态 status
# 查看状态
git status
#查看状态 使输出信息更加简洁
git status –s
5.添加到缓存区 add
# 将未跟踪的文件加入暂存区
git add <文件名>
# 将暂存区的文件取消暂存 (取消 add )
git reset <文件名>
6.提交到本地仓库 commit
# git commit 将暂存区的文件修改提交到本地仓库
git commit -m "日志信息" <文件名>
7.删除 rm
# 从本地工作区 删除文件
git rm <文件名>
# 如果本工作区库误删, 想要回退
git checkout head <文件名>
8.查看远程仓库
# 查看远程 列出指定的每一个远程服务器的简写
git remote
# 查看远程 , 列出 简称和地址
git remote -v
# 查看远程仓库详细地址
git remote show <仓库简称>
9.添加/移除远测仓库
# 添加远程仓库
git remote add <shortname> <url>
# 移除远程仓库和本地仓库的关系(只是从本地移除远程仓库的关联关系,并不会真正影响到远程仓库)
git remote rm <shortname>
10.
# 从远程仓库克隆
git clone <url>
# 从远程仓库拉取 (拉取到.git 目录,不会合并到工作区,工作区发生变化)
git fetch <shortname> <分支名称>
# 手动合并 把某个版本的某个分支合并到当前工作区
git merge <shortname>/<分支名称>
# 从远程仓库拉取 (拉取到.git 目录,合并到工作区,工作区不发生变化) = fetch+merge
git pull <shortname> <分支名称>
git pull <shortname> <分支名称> --allow-unrelated-histories # 强制拉取合并
注意:如果当前本地仓库不是从远程仓库克隆,而是本地创建的仓库,并且仓库中存在文件,此时再从远程仓库拉取文件的时候会报错(fatal: refusing to merge unrelated histories ),解决此问题可以在git pull命令后加入参数--allow-unrelated-histories (如上 命令)
# 将本地仓库推送至远程仓库的某个分支
git push [remote-name] [branch-name]
11.分支
# 默认 分支名称为 master
# 列出所有本地分支
git branch
# 列出所有远程分支
git branch -r
# 列出所有本地分支和远程分支
git branch -a
# 创建分支
git branch <分支名>
# 切换分支
git checkout <分支名>
# 删除分支(如果分支已经修改过,则不允许删除)
git branch -d <分支名>
# 强制删除分支
git branch -D <分支名>
# 提交分支至远程仓库
git push <仓库简称> <分支名称>
# 合并分支 将其他分支合并至当前工作区
git merge <分支名称>
# 删除远程仓库分支
git push origin –d branchName
12.tag
# 列出所有tag
git tag
# 查看tag详细信息
git show [tagName]
# 新建一个tag
git tag [tagName]
# 提交指定tag
$ git push [仓库简称] [tagName]
# 新建一个分支,指向某个tag
$ git checkout -b [branch] [tag]
# 删除本地tag
$ git tag -d [tag]
# 删除远程tag (注意 空格)
$ git push origin :refs/tags/[tag]
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END
暂无评论内容