Git Basic

初始配置

大家一般都是用github,其威名不必多说,这里配置针对GitHub,其他比如gitlab和码云等都类似。

  1. 我是直接用的cmder自带的git,首次使用先生成新的ssh key:
    ssh-keygen -t rsa -C "xxxxxx@yy.com"
    Win10下其存放路径为:c:/Users/xxxx_000/.ssh/。
  2. 添加ssh key到GItHub.
    登录GitHub→Settings→SSH kyes→Add SSH key;
    复制公钥内容id_rsa.pub,粘贴到key输入框.
  3. 配置账户.
    $ git config --global user.name “your_username” #设置用户名
    $ git config --global user.email “your_registered_github_Email” #设置邮箱地址(建议用注册github的邮箱)
  4. 测试ssh keys是否设置成功.
    ssh -T git@github.com
    Hi xxx! You've successfully authenticated, but GitHub does not provide shell access.
    出现该提示说明设置成功。

安装gogs

实验室的代码不方便传到github上,就自己在服务器搭了一个git server,用的gogs-go git service,基本步骤如下:

  1. 安装基础环境:go mysql git,注意mysql安装5.7版本;
  2. mysql中新建gogs库,设置编码utf8,引擎InnoDB:
    CREATE DATABASE gogs CHARACTER SET utf8 COLLATE utf8_general_ci;
  3. CentOS中新建git用户,下载gogs二进制安装包(注意位数),将解压后的文件mv到/usr/local目录,并在/usr/local/gogs目录下新建log目录用来存储日志;
  4. cd到/usr/local/gogs下,运行./gogs web即可启动git服务。但是这样关闭命令窗口后服务也会退出,运行nohup ./gogs web &指令可以保证服务不退出。
  5. 访问ip:3000即可进入gogs服务页面,可进行相应的个性化配置。
    配置文件在/usr/local/gogs/custom/conf/app.ini,进一步的配置可以更改此文件。

注意有时候输入一些git指令操作会报错提示已有index.lock文件,有其他进程将资源锁住之类云云。到任务管理器中将进程杀死再继续操作就可以了,暂时不清楚是操作不规范的问题还是存在的bug,以前用github没有碰见过,待查究。

git基本概念

这里写图片描述
Git只能跟踪文本文件。
stage在git-gui中被翻译为缓存,较为形象。
传输协议:git:// 协议,也可用 http(s)?/ 或 user@server:/path.git 表示SSH 传输协议。
一般的,我们约定俗成将master分支视为主分支,在该分支发布稳定版本代码,其实在设计和操作上master分支和其他分支并无本质区别。

常用指令

$ which git                                         #linux查看安装目录
$ where git											#Windows查看安装目录
$ git config --list									#配置列表
$ git init											#初始化本地git仓库
$ git clone -b branch_name git@github.com:HackerDotCN/learngit.git (new_name) #特定分支中间加上分支名,默认是master
$ git status									#查看状态
$ git remote add origin https://github.com/HackerDotCN/JavaBasicDemo.git #添加远程仓库,注意要先在github上创建好该仓库
$ git remote add origin  git@github.com:HackerDotCN/JavaBasicDemo.git
$ git remote add upstream ssh://git@gitlab.qiyi.domain:10022/qichuan/qichuan.git # 添加upstream
$ git fetch upstream  # 拉取upstream更新
$ git merge upstream/walle # 将upstream更新与本地合并
$ git remote -v								   #查看远程仓库
$ git remote set-url origin gitlab@gitlab.chumob.com:php/hasoffer.git #更改远程仓库地址
$ git add <filename>    						#添加到暂存库
$ git add -A   			#stages All
$ git add .  			  #stages new and modified, without deleted
$ git add -u 			  #stages modified and deleted, without new
$ git commit -m "Your description"   #提交到本地仓库
$ git push origin master   #将本地库推送到远程
$ git push origin lab_desktop
$ git push origin HEAD:refs/for/master
$ git push origin master:master #创建远程master分支
$ git push origin --delete <branchName> #删除远程分支


$ git branch –a #查看所有分支
$ git branch -d branch_name   #删除某分支
$ git checkout -b branch_name #新建并切换到某分支
$ git diff <filename>   #工作目录中文件和暂存区域快照之间的差异(修改之后还没有暂存起来的变化内容)
$ git checkout -- filename  #撤销工作区中的文件变化:
$ git reset HEAD <filename>  #撤销暂存区中的修改

#git上删除文件和添加文件一样,都是要add/commit/push后才生效的
$ rm <filename>  			 #只删除工作区中文件,跟Linux下删除文件无异
$ git rm <filename>			 #删除仓库中的文件
$ git rm --cache a  		 #只删除仓库中的a,保留工作区中的
$ git rm -r myFolder   	     #删除文件夹myFolder,并把它从git的仓库管理系统中移除

$ git reset HEAD a    #恢复删除文件a
$ git checkout a
$ git reset --hard HEAD^/commit_id  #将文件恢复到前一次或者某个id标志的状态
# 注意使用 --hard 参数会抛弃当前工作区的修改
# 使用 --soft 参数的话会回退到之前的版本,但是保留当前工作区的修改,可以重新提交
$ git push origin <分支名> --force
# 本地reset后使远端仓库也回退到相应的版本,需要参数 --force

$ git log  #查看提交日志,有commit id等信息


$ git reflog  #查看所有历史
# 假如不小心将本地和远端的某次提交都reset了,赶紧看一下这个命令,找到自己的某次提交,再reset过去就行了,哎,吓一跳,还是要玩转git才行,下面是我git reset后丢失又找回的命令历史
$ git reflog
94f0c67 (HEAD -> miaoyun-springboot, origin/miaoyun-springboot) HEAD@{0}: commit: add qts journal & remove deprecated opsreport schdule
57d8677 HEAD@{1}: reset: moving to HEAD^
dbad775 HEAD@{2}: reset: moving to dbad775
57d8677 HEAD@{3}: reset: moving to HEAD^
dbad775 HEAD@{4}: commit: add qts journal function & remove deprecated OpsReport schedule task

常见问题

很多人在windows下开发,部署运行在Linux下,提交代码时可能会遇到如下问题:

warning: LF will be replaced by CRLF

原因分析:
CRLF – Carriage-Return Line-Feed 回车换行,就是回车(CR, ASCII 13, \r) 换行(LF, ASCII 10, \n)。
这两个ACSII字符不会在屏幕有任何输出,但在Windows中用来标识一行的结束。而在Linux/UNIX系统中只有换行符。也就是说在Windows中的换行符为:CRLF, 而在linux下的换行符为:LF。

  • 如果是在Windows系统上开发,配置core.autocrlf为true,这样++提交时git会自动把行结束符CRLF转换成LF,而在签出代码时把LF转换成CRLF++,指令:
    $ git config --global core.autocrlf true
  • Linux或Mac系统使用LF作为行结束符,因此你不想 Git 在签出文件时进行自动的转换;当一个以CRLF为行结束符的文件不小心被引入时你肯定想进行修正,把core.autocrlf设置成input来告诉 Git 在++提交时把CRLF转换成LF,签出时不转换++。
    $ git config --global core.autocrlf input
    这样会在Windows系统上的签出文件中保留CRLF,会在Mac和Linux系统上,包括仓库中保留LF。
  • 如果你是Windows程序员,开发运行都在Windows上,可以设置false取消此功能,把回车符记录在库中:
    $ git config --global core.autocrlf false
SafeCRLF

拒绝提交包含混合换行符的文件
git config --global core.safecrlf true
允许提交包含混合换行符的文件
git config --global core.safecrlf false
提交包含混合换行符的文件时给出警告
git config --global core.safecrlf warn

Filename too long

在Git bash中,运行下列命令: git config --global core.longpaths true

推送代码时每次都要输入用户名密码

git remote -v看是不是选择了https方式,如果是的话改成git@github.com:user_name/repo_name.git即可。

push失败提示信息
! [rejected]        lab_desktop -> lab_desktop (fetch first)
error: failed to push some refs to 'git@github.com:HackerDotCN/OJ.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

英文提示很清楚了,远程分支和本地本地文件多(比如初始化时的readme文件或者别人往这个分支上推送代码了),需要先合并一下

git pull --rebase origin lab_desktop

refusing to merge unrelated histories

已经在git server新建一个repository,写了README.md文件,将本地项目合并时提示上述错误,需要添加参数--allow-unrelated-histories
http://blog.csdn.net/lindexi_gd/article/details/52554159

参考链接
http://blog.csdn.net/u012869696/article/details/52097227
http://blog.csdn.net/jtracydy/article/details/53174175

附录:
我的.gitignore文件

#忽略所有.svn目录
.svn/
#忽略eclipse
.classpath
.project
.settings/
target/
#忽略idea
.idea/
*.iml
./../*iml
out/
#忽略info
issue.info
#忽略upload.py
upload.py
output/
deploy/version.txt
webroot/
sql/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值