Git极简实用教程

GIT常用操作

1. 下载安装git

https://www.git-scm.com/download

2. 生成密钥
ssh-keygen -t rsa -C "your_email@youremail.com"  

然后打开 .ssh/id_rsa.pub,(.ssh是隐藏文件夹,需要设置显示隐藏文件夹)复制其中的一串字符。并把其中的密钥串粘贴到阿里云密钥中:https://code.aliyun.com/profile/keys

3. 测试ssh key是否成功
ssh -T git@code.aliyun.com 
4. 配置Git的配置文件,username和email
git config --global user.name "your name"   //配置用户名 
git config --global user.email "your email"    //配置email 
5.在github上新建一个仓库
6. 进入要所要上传文件的目录输入命令
git init 
7.创建一个本地仓库,本地与远程仓库建立连接
git remote add origin git@code.aliyun.com:knowdo/knowdo.git
8. 添加一个文件xxx到本地仓库,使用命令
git add xxx  
或
git add .
9.查看文件有变更的文件
git status
10.把这个添加提交到本地的仓库,使用命令
git commit -m "提交的说明信息(比如第一次提交)”
11. 把本地仓库提交到远程的GitHub仓库,使用命令
git push -u origin master  
12.克隆远程项目
git clone git@code.aliyun.com:knowdo/knowdo.git
13.git 添加gitignore文件,忽略某些文件,避免其提交(.gitignore可以忽略你不想上传的文件,比如doc,target,classes等等,只需要在.git同目录下新增.gitignore文件,然后添加不需要上次的目录即可)
1. 创建:touch .gitignore
2. 编辑:
  1. 忽略文件/文件夹:
  ```code
  # 忽略目标文件夹
  文件夹名称/
  ```
  2. 不忽略文件/文件夹
  ```code
  # 不忽略指定文件
  !src/
  ```
3. 例子
```code
# 此为注释 – 将被 Git 忽略
*.a       # 忽略所有 .a 结尾的文件
!lib.a    # 但 lib.a 除外
/TODO     # 仅仅忽略项目根目录下的 TODO 文件,不包括 subdir/TODO
build/    # 忽略 build/ 目录下的所有文件
doc/*.txt # 会忽略 doc/notes.txt 但不包括 doc/server/arch.txt
```
4. 具体设置可详见git官网:https://git-scm.com/docs/gitignore
14.新建本地分支,并推送到远程
git checkout -b dev   //b 表示创建并切换
git push origin dev:dev
15.查看所有分支
git branch -al 
16.合并某分支到当前分支
git merge dev
17.删除分支
git push origin --delete dev //删除远程分支  或者
git push origin :dev  //推送本地空分支到远程dev分支

git基本命令

1.查看状态
git status #显示工作目录和暂存区的状态,不显示已经commit的信息
git log #显示提交日志
2.add添加文件#
git add .  #将所有修改添加到暂存区,不包括被删除的文件
git add abc.py  #添加某个文件
git add abc.py cde.py #添加多个文件时,中间用空格分开
git add ab* #将以ab开头的文件添加到暂存区
git add *.py #将以py结尾的文件添加到暂存区
git add -u <path> #只添加<path>中已跟踪的文件信息,省略<path>即当前目录
git add -A #提交所有变化,包括被删除的文件
3.撤销add操作#
git reset HEAD #撤销上一次的add操作 HEAD~2 代表倒数第二个
git reset HEAD abc.py #对某个文件进行撤销
4.commit提交到本地版本库#
git commit -m "注释信息"
git commit --amend #增补提交,使用当前节点相同节点进行一次新提交,就的提交被取消
5.撤销commit操作#
git reset --soft HEAD^ #不删除工作空间改动代码,撤销commit,不撤销git add
git reset --hard HEAD^ #删除工作空间改动代码,撤销commit和add
6.撤销本地的修改#
#本地做了一些操作,想撤销到上一次commit的版本
git checkout
7.暂存工作区文件#

本地进行了修改添加,这个时候需要切分支,或者更新本地代码,可以先将本地代码暂存,暂存后工作目录就是干净的,可以切分支或者更新代码,更新或者操作完别的分支后,再从暂存区取出之前的代码。

git stash   # 添加到暂存区
git stash save "注释"  #多了注释内容
git stash list # 查看存储的列表
git stash show #显示做了哪些改动,默认第一个,还可以git stash show stash@{2}
git stash apply stash@{1} #应用某个存储,但不会从存储列表删除
git stash drop stash@{1} #从栈中移除暂存的内容
git stash clear #删除所有缓存

注:没有在git版本控制中的文件是不能被stash存起来的

8.删除文件#
git rm a.py #将a.py从git仓库管理系统中删除
# 下次提交 远程仓库的a.py 也会被删除
git rm -r mydir #将mydir文件夹从仓库管理系统中删除

普通的本地删除文件,远程仓库还是会有的。

9.branch的基本操作#
git branch #查看本地分支
git branch -al#查看所有分支(包括远程)
git bracnh dev #创建dev分支
git checkout dev #切换到dev分支
git branch -m dev test #修改分支名字
git push origin --delete dev #删除分支
git merge dev #合并某个分支到当前分支
首次提交
git init
# 将本地仓库与码云远程仓库进行关联
git remote add origin git的url地址
git add .
git commit -m "描述"
# git push
# 强制提交
git push -u origin master -f
非首次提交
git add .
git commit -m "描述"
git push

首次报错

1. push报错
error: failed to push some refs to 'git@code.aliyun.com:hasimple/hasimple.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

解决:git pull --rebase origin master

2. push报错
remote: Resolving deltas: 100% (2328/2328), done.
remote: GitLab: You are not allowed to push code to protected branches on this project.

原因:没有传代码的权限,可前往git对开发者权限就行修改即可!!

3.拉取报错
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@       WARNING: POSSIBLE DNS SPOOFING DETECTED!          @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
The ECDSA host key for git.papamk.com has changed,
and the key for the corresponding IP address 120.25.235.138
is unknown. This could either mean that
DNS SPOOFING is happening or the IP address for the host
and its host key have changed at the same time.
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@    WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!     @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!
Someone could be eavesdropping on you right now (man-in-the-middle attack)!
It is also possible that a host key has just been changed.
The fingerprint for the ECDSA key sent by the remote host is
SHA256:Dar7f0uThzMQg2UqtGjAVzsDqtczwX9yZSWcXO5M+NM.
Please contact your system administrator.
Add correct host key in /c/Users/Administrator/.ssh/known_hosts to get rid of this message.
Offending ECDSA key in /c/Users/Administrator/.ssh/known_hosts:1
ECDSA host key for git.papamk.com has changed and you have requested strict checking.
Host key verification failed.
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.
解决:
 ssh-keygen -R git.domain.com(这里的域名是指你的代理仓域名)
# Host git.papamk.com found: line 1
/c/Users/Administrator/.ssh/known_hosts updated.
Original contents retained as /c/Users/Administrator/.ssh/known_hosts.old

如果本文对你有帮助,欢迎转载点赞!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值