git常用命令

学习参考网址:https://www.liaoxuefeng.com/wiki/896043488029600

1.本地库部分

初始化

git init
###初始化完成后,会在工作空间下生成一个.git文件

将修改的文件提交到暂存区

git add 文件名

将暂存区的内容提交

git commit -m "备注内容"

设置全局用户姓名

git config --global user.name "姓名"

设置全局用户邮箱

git config --global user.email "邮箱"

查看git状态

git status
###状态一:改变还没提交到暂存区
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   readme.txt

no changes added to commit (use "git add" and/or "git commit -a")

###状态二:改变提交到暂存区,还没提交到库
On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        modified:   readme.txt

查看和最新版有何不同

git diff

查看日志

git log

git log --pretty=oneline

2.版本回退

版本回退到上一个版本

 git reset --hard HEAD^

回退三个版本

 git reset --hard HEAD^^^ 
 ###or
 git reset --hard~3

回退到某个版本

git reset --hard 版本号

查看版本变换

git reflog

撤销修改

git checkout -- 文件名
###or 
git restore 文件名
###未提交到暂存区时,撤销修改回合版本库一样;提交到暂存区后,撤销回提交到暂存区的状态。

3.远程库

从远程库克隆

git clone ssh路径

显示所有远程仓库

git remote -v

添加远程版本库(SSH路径)

git remote add 仓库别名(自己随便起) 仓库路径
#示例:origin为远程库别名
git remote add origin git@github.com:Huletian/learn.git

推送内容到远程库(第一次推送加上-u ,后面就不用加 -u了)

若GitHub不信任公钥:参考:https://zhuanlan.zhihu.com/p/454666519

git push -u 远程仓库别名 分支名

#示例
git push -u origin master 

删除远程库分支

git remote rm 远程仓库别名

修改仓库别名

git remote rename old_name new_name

4.创建分支与合并

创建分支

#创建分支并切换过去
git checkout -b 分支名字
#or
git switch -c 分支名



#创建分支
git branch 分支名


#切换分支
git checkout 分支名
#第二种切换分支方法
git switch 分支名

查看当前分支

git branch

合并分支(切换到主分支上,在进行合并)

git merge 分支名

删除分支

git branch -d 分支名

分支合并时解决冲突

#切换到主分支,执行合并
git merge 分支名

#结果提示
Auto-merging 文件名
CONFLICT (content): Merge conflict in 文件名
Automatic merge failed; fix conflicts and then commit the result.

#此时我们可以手动解决冲突,然后在add,commit


#使用git status可以查看冲突的文件


#冲突解决后,查看合并路径日志
git log --graph --pretty=oneline --abbrev-commit
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值