git介绍
git init后创建本地仓库,有三个区。
工作区:文件目录
缓存区:git add后把文件放在该区
分支区:git commit后把文件放到分区
第一次git commit后才创建了master主分区。创建了master分区后才能创建其他分区。
git push origin 本地分支:远程分支
git pull origin 远程分支:本地分支
git status 现在处于哪个分区
git branch 本地有哪些分区
git branch -r 远程有哪些分区
git branch -a 共有哪些分区
git branch dev 创建本地dev分区
git checkout dev 切换到dev分区
git checkout -b dev 创建并切换到dev分区
gitlab或github创建时不要创建readme.md,因为会导致不能直接上传文件。
git通过ssh方式提交到github
向同一个github下的不同仓库上传只需要一个公私钥对即可
1. 下载git,C:\Program Files\Git\bin加入环境变量,并配置全局参数
进入cmd 配置全局参数,连接多个github仓库使用同一套
git config --global user.name “lxc"
git config --global user.email "1320xxxxxxx@163.com"
git config --global default simple
# 参看git配置参数
git config -l
2. 创建github仓库
Initialize this repository with a README:添加一个README
add .gitignore : 建立文件过滤规则,里面是不需要更改版本更改的文件,即上传后,以后都不动它了,类似
# Built application files *.apk *.ap_ # Files for the ART/Dalvik VM *.dex # Java class files *.class # Generated files bin/ gen/ out/ # Gradle files .gradle/ build/ # Local configuration file (sdk path, etc) local.properties # Proguard folder generated by Eclipse proguard/ # Log Files *.log # Android Studio Navigation editor temp files .navigation/ # Android Studio captures folder captures/ # Intellij *.iml .idea/workspace.xml # Keystore files *.jks
add a license为开源许可证,即授权条款的法律文件。最基本,开源软件强迫任何使用和修改该软件的人承认发起人的著作权和所有参与人的贡献。任何人拥有可以自由复制、修改、使用这些源代码的权利,不得设置针对任何人或团体领域的限制;不得限制开源软件的商业使用等。
3. 选择git提交到github的方式,选择ssh(公私钥方式)
两种方式:
a. https 提交,需要输入账户密码;
b. ssh 提交,RSA 验证,不需要输入账户密码。创建一个RSA公钥 / 私钥对,然后在 github 中添加公钥
4. 配置公私钥
向同一个github下的不同仓库上传只需要一个公私钥对即可
a. 在~/.ssh/下生成公私钥对
#右键单击git Bash
cd ~/.ssh # ~/即用户的根目录 C:\Users\liangxiaochong,把所有的公私钥都存在这里
# 实际操作
ssh-keygen -f a
然后一路enter即可生成 a私钥和a.pub公钥
# 生成 ssh 公钥认证所需的公钥和私钥文件
ssh-keygen -t 类型(默认rsa) -f 文件名(默认id_rsa,id_rsa_pub公钥) -C 公钥中的备注
b. 将该公私钥添加到~/.ssh下的config文件中(没有的话新建)
#sancun
Host sancun # 给hostName 起个别名
HostName github.com #是目标主机的主机名
PreferredAuthentications publickey #强制使用公钥
IdentityFile ~/.ssh/sancun #私钥地址
c. 将公钥里的内容添加到github中
setting->SSH and GPG keys
d. 验证是否成功配置SSH key
# 右键单击git Bash
ssh -T git@sancun
Hi Liangsancun! You've successfully authenticated, but GitHub does not provide shell access.
5. 首次上传。先进入项目目录,进入命令行cmd
git init #在该目录下创建一个本地仓库
git add . 或 git add a.txt/*.txt #将该目录下所有的文件添加到本地仓库
git commit -m 'comments' #提交该操作,并为该操作添加注释
git remote add origin ssh地址或https地址 #将本地库连接到github上的仓库
其中git@github.com:Liangsancun/lang.git改为git@sancun(Host的名字/HostName的别名):Liangsancun/lang.git,这样就可以使用指定的ssh公私钥对了
git push -u origin master #将本地(master)推送到主机(origin,即上面的github仓库地址,-u指定其为默认主机,后面上传就可以不加任何参数,直接使用git push了)
6. 再次上传
git add . 或git add a.txt/*.txt #将该目录下所有的文件添加到本地仓库
git commit -m 'say something' #提交本次操作,并为本次操作添加注释
git push # 将本地仓库的文件同步到github指定仓库 git push origin,当第一次-u指定origin后,就直接用git push了。
7. 出现的问题
使用git add . 时,出现 : git warning: LF will be replaced by CRLF in xx.py。
windows 中换行符为CRLF(回车换行),Linux中换行符为LF(换行)
在该文件中LF会被改成CRLF
solution
git config --global core.autocrlf false //禁用自动转换
git init
git add .
…
-
当前电脑中文件版本还没有github上的新 ! [rejected] master -> master (fetch first) error: failed to push some refs to 'git@github.com:Liangsancun/kaggle-.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. #原因: 在push时出现:(第一次push【在github上先修改了readme.md】,之后push【电脑中文件的版本比github上还旧,因为其他人更新的原因】 #解决办法: 第一次push:git push -f -u origin master 之后push:git push -f
-
#问题: 打开cmd 出现:警告: git command could not be found. Please create an alias or add it to your PATH. 也无法使用git命令 #解决办法: 使用git bash, 单击右键即可
#问题 之前一直正常,突然间在git push时出现: ssh: connect to host github.com port 22: Connection timed out fatal: Could not read from remote repository. Please make sure you have the correct access rights and the repository exists. # 原因:之前保存的github的ip地址不能用了 #解决办法 22端口号是ssh的端口号 在hosts文件里添加了新查找的github.com的IP地址,【git push的时候,自动把ip地址放到.ssh里的know_hosts文件里】
-
gitlab使用
大致相同
第一次上传
如果只是上传文档的话,不涉及代码可以和github一样
创建master分区,并将该分区同步到远程仓库dev分支(此时dev分支可没有创建)
git push -u origin master:dev
创建dev分区并将该分区推到远程仓库
//rm -rf .git删除仓库
git init #在该目录下创建一个本地仓库
git add . 或 git add a.txt/*.txt #将该目录下所有的文件添加到缓存区
git commit -m 'comments' #提交到master分区,并为该操作添加注释
//此时master分区创建
git checkout -b dev #创建并切换到dev分区
//在目录中随便添加一个文件,否则git认为缓冲区东西还是一样的,已经提交过了,不用再提交了
git add .
git commit -m "xx"//提交到dev分区
git remote add origin ssh地址或https地址 #将本地库连接到github上的仓库
其中git@github.com:Liangsancun/lang.git改为git@sancun(Host的名字/HostName的别名):Liangsancun/lang.git,这样就可以使用指定的ssh公私钥对了
git push -u origin dev #将本地(dev)推送到主机(origin,即上面的github仓库地址,-u指定其为默认主机,后面上传就可以不加任何参数,直接使用git push了),会新建dev分支
再次上传
git add .
git commit -m ""
git push//master->master 公司文档地址
git push origin master:dev//本地master->远程的dev 公司代码地址