Git Basic

来源:互联网 发布:淘宝名店 编辑:程序博客网 时间:2024/04/29 16:24

初始配置

  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.
    出现该提示说明设置成功。

git基本概念

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

常用指令

$ 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 -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^ or commit_id  #将文件恢复到前一次或者某个id标志的状态$ git log  #查看提交日志,有commit id等信息$ git reset -hard xxxxx  #恢复到某次历史提交(根据gitlog取得的commit id)$ git reflog  #查看命令历史

常见问题

很多人在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 dohint: not have locally. This is usually caused by another repository pushinghint: to the same ref. You may want to first integrate the remote changes

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

参考链接
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./../*imlout/#忽略infoissue.info#忽略upload.pyupload.pyoutput/deploy/version.txtwebroot/sql/