git 是分布式代码管理工具,愈来愈多的企业使用它。因此掌握git的使用相当重要。它的远端管理是基于ssh,因此在使用远端git的时候须要进行ssh配置。
ssh是一种免密登陆,使用的rsa非对称加密来进行服务器认证;html
首先你须要拥有gitlab或是github的一套帐号,而后设置git的user name 和 user email:
# git 全局设置帐号 git config --global user.name "your name" git config --global user.email "your e-mail"
cd $USER cd .ssh #若是存在,须要备份能够备份下,若是不须要能够直接覆盖 ssh-keygen -t rsa -C "your e-mail" #生成 ssh
执行结果:git
最终获得 id_rsa 和 id_rsa.pubgithub
打开https://gitlab.company.com/ ,使用本身帐号登陆,而后添加ssh密钥;
主页头像 -> profile -> SSH Keys -> Add keyshell
将id_rsa.pub文件中的内容添加上去,而后点击Add key;服务器
ssh git@gitlab.company.com #会出现以下提示,说明连接成功 The authenticity of host 'gitlab.company.com (100.98.137.229)' can't be established. RSA key fingerprint is SHA256:WiQ1s8RKGjQpO0ICFY3NV2Hs0axwIbrv6j6q0XJsdsc. RSA key fingerprint is MD5:6c:8d:0f:09:31:c9:08:9b:67:48:09:7c:d9:46:65:3e. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added 'gitlab.company.com,100.98.137.229' (RSA) to the list of known hosts. PTY allocation request failed on channel 0 Welcome to GitLab, your name! Connection to gitlab.company.com closed.
建立一个新的仓库
git clone git@gitlab.company.com:yaoname/my-test-git-project.git cd my-test-git-project touch README.md git add README.md git commit -m "add README" git push -u origin master touch .gitignore vi .gitignore #编辑 git add .gitignore git commit -m "add .gitignore" git push -u origin master
本地仓库和远端仓库创建链接app
cd existing_folder git init git remote add origin git@gitlab.company.com:yourname/my-test-git-project.git git add . git commit git push -u origin master
git基本操做讲解
The most commonly used git commands are: add Add file contents to the index bisect Find by binary search the change that introduced a bug branch List, create, or delete branches checkout Checkout a branch or paths to the working tree clone Clone a repository into a new directory commit Record changes to the repository diff Show changes between commits, commit and working tree, etc fetch Download objects and refs from another repository grep Print lines matching a pattern init Create an empty Git repository or reinitialize an existing one log Show commit logs merge Join two or more development histories together mv Move or rename a file, a directory, or a symlink pull Fetch from and merge with another repository or a local branch push Update remote refs along with associated objects rebase Forward-port local commits to the updated upstream head reset Reset current HEAD to the specified state rm Remove files from the working tree and from the index show Show various types of objects status Show the working tree status tag Create, list, delete or verify a tag object signed with GPG
git init
git add . #或 git add * 提交全部文件 git add README.md #提交指定文件 git add folder/ #提交某个文件夹下面的全部文件
git commit -m '注解'
git pull origin master
git push origin master
git fetch
git branch -a #查看全部分支 git branch --list #查看本地分支 git branch feature/20181017.business.chao #建立分支 git checkout feature/20181017.business.chao #切换分支 #或直接建立而且切换 git checkout -b feature/20181017.business01.chao git push origin feature/20181017.business01.chao #推送到远端 git pull origin feature/20181017.business01.chao #从远端拉取 #删除 git branch -d/-D feature/20181017.business01.chao #删除本地 git push origin --delete feature/20181017.business01.chao #删除远端分
git status
#当前分支 git checkout feature/20181017.business01.chao touch 1.txt vi 1.txt # 编辑内容而后提交到远端 git checkout git checkout feature/20181017.business.chao git merge feature/20181017.business01.chao #合并本地分支
git grep --text "test" #使用git grep -h 查看更多用法
git diff master #比较两个分支的不一样
git log -n 10 #查看最近10条提交记录
touch 2.txt git add 2.txt git commit -m 'add 2.txt' git rm 2.txt #删除已经提交到本地仓库的文件
git checkout master git tag --list #查看全部标签 git tag v1.0 #添加tag git tag --list 'v1.*' # 列出version为1的全部子版本
git show #展现项目的各类类型 commit eaa7f945204bed8f2b01d284d99fcf0b3ac3029e Author: chao <chao@yourcompany.com> Date: Wed Oct 17 06:16:26 2018 +0000 add README diff --git a/README.md b/README.md new file mode 100644 index 0000000..7088fed --- /dev/null +++ b/README.md @@ -0,0 +1,5 @@ + +# this is repositry
git rebase branchName #消除本分支下面的commit git rebase --continue #在rebase的过程当中,也许会出现冲突(conflict). 在这种状况,Git会中止rebase并会让你去解决 冲突;在解决完冲突后,用"git-add"命令去更新这些内容的索引(index), 而后,你无需执行 git-commit,只要执行 git rebase --abort #在任什么时候候,你能够用--abort参数来终止rebase的行动,而且"mywork" 分支会回到rebase开始前的状态
#储藏使用名称和不使用名称保存未提交到仓库的(暂存和非暂存) git stash git stash save "stash-name-1" #变动统计 git stash show #每次储藏 git stash list #恢复到git版本管理中 git stash pop #弹出栈 #经过名字使用储藏 git stash apply #删除 git stash drop #清空 git stash clear #直接使用stash建立分支 git stash branch testbranch
git revert commit_id
git reset --hard HEAD^ #回退到上个版本 git reset --hard HEAD~3 #回退到前3次提交以前,以此类推,回退到n次提交以前 git reset --hard commit_id #退到/进到 指定commit的sha码 git push origin HEAD --force #强推到远端,使用git push 推送推送失败
git bisect start #开启查找 git bisect good v2.6.18 git bisect bad master #查找报错的版本
#查找到对应的文件 find . -name .DS_Store -print0 | xargs -0 git rm -f --ignore-unmatch #加入git忽略 echo .DS_Store >> ~/.gitignore #add git add --all #提交忽略 git commit -m '.DS_Store banished!'