目录
创建步骤
1.右键点击文件夹 --> git bash here
2.git init 初始化一个空的git仓库
3. 查看文件状态 git status
untracked files 表示文件没有被追踪
git add 文件名 表示将文件从工作区添加到暂存区
git commit 将暂存区的文件添加到版本库
第一次会报 please tell me who you are,则使用下面命令注册地址
1.1 git config --global user.name cc
1.2 git config --global user.email ....
git 基本命令
0) git init
1.初始化git仓库
2.出现.git文件
1) git add
1. git add filename 将文件添加暂存区
2. git add . 将本地所有修改的文件添加到暂存区
2) git commit
1. git commit -m 'add new files' ''添加一个描述
将暂存区的所有文件添加到版本库中
2. git commit -am 'description' 跳过 git add添加到暂存区命令 直接一次性将修改中的文件添加到版本库中
3) git log
--查看提交信息
git log --oneline
4) git status
--查看项目文件状态
5) git config
1. git config --global user.name Hayden
2. git config --global user.email chenorange12@gmail.com
3. git config --list 查看配置信息
4.地址 /.gitconfig
git 结构
1) working directory 工作区
2) staging index 暂存区
3) git directory(Repository) 版本库
git 状态
1) untracked 未被追踪
2) Modified 表示工作区修改了某个文件但是还是没有添加到暂存区
3) Stagd 表示把工作区修改的文件添加到暂存区但是没有提交到版本库
4) Committd 表示数据被安全的存储在本地库中
git 撤销操作
1. git commit --amend 撤销上次提交 并将暂存区的文件重新提交
2. git checkout -- filename
2.1拉去暂存区的文件并将其替换工作区文件 (恢复修改前的状态)
3. git reset HEAD -- filename
1. 拉去最近一次提交的版本库中的这个文件到暂存区,该操作不影响工作区
4.git reset --hard HEAD 将撤销本次合并回退到合并前
git rest --mixed HEAD
git rest --soft HEAD
git 文件删除
1.在编译环境中删除了文件,则在 git命令行会提示没有删除暂存区,则 git add . 命令将删除命令提交到暂存区
2.git rm 文件名
1)将工作区和暂存区一起删除
2)git rm --cached filename
1.不小心将不需要追踪的文件添加到暂存区,想要删除暂存的文件但又不想删除工作区的文件很有用
3)git rm -f filename
1.当工作区或者暂存文件修改了
4) glob 模式
建立文件 touch .gitigore --> vim .gitigore -->添加忽略文件
1)星号(*)代表匹配多个任意字符
2)[abc] 匹配任何一个列在方括号中的字符
3)问号(?) 只匹配一个任意字符
3. git mv filename
1. mv oldname newname
2. git rm oldname
3. git add newname
git 分支
1. git分支的创建、修改、切换、删除
git branch 查看分支
git branch name 本地创建分支
git branch -r 查看远程分支
git branch -a 查看所有分支
1) * 代表当前分支
2)git checkout name 切换分支
3) git branch -d name 删除分支(不能删除本分支,只能在其他分支删除)
4)git branch -m name 改变分支名
5)git checkout -b name 创建分支并切换到本分支
6) 将分支推送到github git push origin gh-dev
7) 删除github 远程分支 git push origin :gh-dev
2. git分支的合并
1.分支的指针
1) HEAD指针指向当前工作的分支
2.git diff
1)git diff 比较工作区与暂存区文件的差异
2)git diff --staged 比较暂存区与版本库的文件差异
3)git diff 版本号 版本号 比较分支内两个版本的差异
4) git diff 分支 分支 比较两个分支的最新提交版本的差异
3. git merge name 合并分支
1)两个分支有相同的标签则会报
Automatic merge failed;fix conflitcts and then commit the result
只需要手动将相同的删除一份就好
2) fast-forward 快进式合并
3.存储变更
git stash
1) git stash list 查看存储区文件
2)git stash apply stash@{0}(存储文件名称)
3) git stash drop 名称
GitHub 上创建 ssh 拉去文件
1)首先在本地 git 中输入 ssh-keygen
2)将 公钥复制到 gitHub中
settings -->SSH
4.远程仓库
1.gitHub上的仓库
1)将项目推送到 github上
git push github创建的https地址 分支名(master)
2)将gitHub中的项目下载到本地
git pull github项目地址 分支名(master)
1)修改 github 地址名称
git remote add github git@github.com:用户名/项目名.git
2. 在服务器搭建 github
1)用 ssH登录 服务器 ssh root@ip
创建 存放git文件夹 mkdir git 进入文件夹后 初始化仓库 git init --bare
2)将本地文件 push到服务器
git push ssh://root@ip/git文件目录 master
3)将远程服务器的项目拉取到
3. git ssh免密登录
1)创建密钥文件
ssh-keygen -t rsa -C "你的github账号邮箱"
**:如果原本有秘钥又想生成其它的,此时不要连续敲空格,如下图:
这里我们用id_rsa_github
来区别原有密钥对,避免被覆盖。
完成之后,我们可以看到~/.ssh
目录下多了两个文件。
2)将公钥添加到Github
- 将
id_rsa.pub
文件内容全部复制 - 登陆到GitHub上,右上角小头像->Setting->SSH and GPG keys中,点击new SSH key)
3)添加密钥到ssh-agent
这里如果你用的github官方的bash,用:
ssh-agent -s
如果是其他的,比如msysgit,用:
eval $(ssh-agent -s)
略过这一步的话,下一步会提示这样的错误:Could not open a connection to your authentication agent.
4) 测试
ssh -T git@github.com
拓展
关于配置多个ssh
1.3 添加私钥
ssh-add ~/.ssh/id_rsa ssh-add ~/.ssh/id_rsa_github
如果提示文件或目录不存在,就使用绝对地址。
1.4 创建config文件
在~/.ssh
目录下创建名为config
的文件。
添加一下内容:
# gitlab Host git.iboxpay.com HostName git.iboxpay.com PreferredAuthentications publickey IdentityFile ~/.ssh/id_rsa_gitlab # github Host github.com HostName github.com PreferredAuthentications publickey IdentityFile ~/.ssh/id_rsa_github
其中,Host
和HostName
填写git服务器的域名。
IdentityFile
指定私钥的路径。
如果在Linux系统下提示错误:Bad owner or permissions on /home/gary/.ssh/config
说明config权限过大,chmod
命令调整:
$ chmod 644 ~/.ssh/config
然后在github和gitlab上添加公钥即可,这里不再多说。
5. git help 文档
1)git help add
git 打包
git bundle create repo.bundle HEAD master