设为首页 收藏本站
查看: 1288|回复: 0

[经验分享] 使用git和github搭建代码开发环境

[复制链接]

尚未签到

发表于 2018-9-18 08:05:18 | 显示全部楼层 |阅读模式
  步骤如下:
  1. 按照https://help.github.com/articles/generating-ssh-keys 所说建立本地Linux与github.com的链接并测试通过;
  2. 在github.com上新建一个自己的项目repo,这里命名为hello作为测试
  3. 在本地的某个目录下/path/to/hello执行git pull git@github.com:you_github_user_name/hello.git
  这样执行完之后就可以在本地/path/to/hello下添加项目文件了,比如这里我创建一个hello.go的源文件,并保存。然后执行git add hello.go和git commit -a -m "my test file"
  4. 这时执行git push git@github.com:you_githubuser_name/hello.git,等命令执行成功以后你会在github.com上看到hello.go文件被提交成功!
  另外,如果你希望在git push 的时候不要每一次都指定
  push git@github.com:you_github_username/hello.git,你可以通过给该项目起个别名的方式进行简化,例如git remote add origin git@github.com:you_github_username/hello.git,这样在下次对该项目进行提交操作时就可以通过git commit -a -m "***" && git push直接完成提交。这时的提交将被直接提交到master下。
  更多的git与github操作可以参看下面的文章:https://github.com/chenzhiwei/linux/tree/master/git
git的相关配置
使用git
  搭建gitserver: https://github.com/chenzhiwei/linux/blob/master/git/build-git-server.mkd
全局设置
$ git config --global user.name "Chen Zhiwei"  
$ git config --global user.email zhiweik@gmail.com
  配置完成之后会在$HOME目录下生成一个.gitconfig配置文件,具体参考示例。
  --local 是将这些内容写入 project 下的 .git/config 文件中,每个project都可以有不同的配置。
自动补全
  如果你系统上安装git之后没有自动补全功能,可以按如下操作来添加补全功能,记得需要重新登录一下。
$ wget -O /etc/profile.d/git-completion.sh https://raw.github.com/git/git/master/contrib/completion/git-completion.bash开始使用

  •   创建新的git仓库
$ mkdir git_repo  
$ cd git_repo
  
$ git init
  
$ echo "test" > README.mkd
  
$ git add README.mkd
  
$ git commit -m "add README.mkd file"
  
$ git remote add origin git@github.com:username/test.git
  
$ git push -u origin master

  •   使用已存在的git仓库
$ cd git_repo  
$ git remote add origin git@github.com:username/test.git
  
$ git push -u origin master
  注意,如果提示fatal: remote origin already exists.,那么说明该本地仓库已经有远端地址了。你可以先使用git remote rm origin删除origin,或者使用git remote add other_name git@github.com:username/test.git来添加(提交时记得使用git push -u other_name master)。
为本地仓库创建两个镜像
  假设现有仓库地址为: git@github.com:chenzhiwei/linux.git
$ git clone git@github.com:chenzhiwei/linux.git  
$ cd linux
  
$ git remote add backup git@gitcafe.com:chenzhiwei/linux.git
  
$ git push -u backup master
  之后每次修改时,在最后push的时候需要执行两条命令:
$ git push origin  
$ git push backup
  如果要只执行一次push就提交到两个远端仓库,那么需要修改仓库根目录下的.git/config文件,添加以下内容:
[remote "both"]  
    url = git@github.com:chenzhiwei/linux.git
  
    url = git@gitcafe.com:chenzhiwei/linux.git
  然后在提交更新时使用git push both就可以将更新提交到两个远端仓库了。
  或者直接把 backup 分支的地址写到 origin 分支这里:
[remote "origin"]  
    fetch = +refs/heads/*:refs/remotes/origin/*
  
    url = git@github.com:chenzhiwei/linux.git
  
    url = git@gitcafe.com:chenzhiwei/linux.git
  注意:在 Git 2.0 将会更改默认的push动作为『只 push 当前 branch 到远端仓库』。如果想继续使用git push both命令需要手动设置一下git push的默认动作git config --global push.default matching。
  push.default有几个简单动作,这里介绍matching和simple,二者意思分别是 push 本地所有的分支到远端仓库和 push 本地当前分支到上游分支。这个解释貌似还不够精确,可以man git-config来查看详细说明。
  一个完整的.git/config文件示例如下:
[core]  
    repositoryformatversion = 0
  
    filemode = true
  
    bare = false
  
    logallrefupdates = true
  
[remote "origin"]
  
    fetch = +refs/heads/*:refs/remotes/origin/*
  
    url = git@github.com:chenzhiwei/linux.git
  
[branch "master"]
  
    remote = origin
  
    merge = refs/heads/master
  
[remote "backup"]
  
    url = git@gitcafe.com:chenzhiwei/linux.git
  
    fetch = +refs/heads/*:refs/remotes/backup/*
  
[remote "both"]
  
    url = git@github.com:chenzhiwei/linux.git
  
    url = git@gitcafe.com:chenzhiwei/linux.git
在现有仓库上创建孤儿分支
  孤儿分支意思为该分支中没有任何内容,与之前创建的其他分支没有任何关联。
$ git clone git@github.com:chenzhiwei/test.git  
$ cd test
  
$ git checkout --orphan new_branch
  
Switched to a new branch 'new_branch'
  
$ git rm -rf . # 删除旧工作目录树中所有文件
  
$ rm .gitignore # 如果有该文件的话就删除
  
$ echo "orphan branch" > README.mkd
  
$ git add .
  
$ git commit -m "add README.mkd file"
  
$ git push origin new_branch
提交单个分支到远端git仓库
  git push命令默认是将所有分支(branch)都提交到git仓库,有时你只想提交某个分支到远端仓库,那么就就需要使用git push origin HEAD。当然也可以使用git config --global push.default tracking命令来改变git push的默认操作,意思是执行git push时默认只提交当前分支到远端git仓库。
git常用指令
  以下几个是git常用的指令,可以简单了解一下。
git config
  在使用git前最好先配置一下你的个人信息及使用偏好。以下命令的意思就不用解释了吧,执行完以下命令就会在你的家目录(~)下生成一个文件~/.gitconfig。
$ git config --global user.name "Chen Zhiwei"  
$ git config --global user.email zhiweik@gmail.com
  
$ git config --global core.editor vim
  
$ git config --global merge.tool vimdiff
  
$ git config --global color.status auto
  
$ git config --global color.branch auto
  
$ git config --global color.interactive auto
  
$ git config --global color.diff auto
  
$ git config --global push.default simple
  
$ git config --global alias.co checkout
  
$ git config --global alias.ci commit
  
$ git config --global alias.st status
  
$ git config --global alias.last 'log -1 HEAD'
  
$ git config --global alias.unstage 'reset HEAD --'
git add
  添加文件内容到索引中去(暂存文件),几个简单示例:
$ git add .  
$ git add --all
  
$ git add *.txt
  
$ git add directory/*.sh
  突然你又不想git add了,那么执行以下命令:
$ git reset .  
$ git reset *.txt
  
$ git reset directory/*.sh
git rm
  删除索引和当时工作目录中的文件。
$ git rm filename  
$ git rm -f *.txt
  
$ git rm -r .
git commit
  将当前改动记录到仓库中,即提交改动到本地仓库中。
$ git commit -m "add a file and remove a file"  突然你又不想git commit了,那么执行以下命令:
$ git reset HEAD^  你commit之后发现少添加了一个文件:
$ git commit -m'msg'  
$ git add forget_file
  
$ git commit --amend
  你的 commit 已经 push 到远程分支(master)了,现在你想反悔了:
$ git clone git@github.com:chenzhiwei/test.git  
$ cd test
  
$ git reset HEAD^
  
$ git push -f master
git status
  查看当前工作目录的状态,即修改、添加及删除了哪些文件。
$ git statusgit checkout
  检出一个分支和目录到当前工作目录中,可以简单理解为切换分支的命令。
  以下命令分别为切换到分支 branch1 和创建一个新的分支 new_branch 。
$ git checkout branch1  
$ git checkout -b new_branch
  取消本地改动:
$ git checkout -- file_namegit branch

  •   列出、创建和删除分支。
  以下指令分别为列出本地分支、所有分支、远端分支、创建、删除、强制删除分支。
$ git branch --list  
$ git branch --all
  
$ git branch --remotes
  
$ git branch new_branch
  
$ git branch --delete branch_name
  
$ git branch -D branch_name
  删除remote tracking branch,就是git branch -r命令列出的分支。
$ git branch -r  
$ git branch -d -r origin/develop

  •   合并分支
  如果出现冲突,那么手动解决冲突就可以了。
$ git checkout branch_name  
$ git checkout master
  
$ git merge branch_name

  •   删除远程分支
  合并分支之后如果不再需要以前的分支了,那么可以在本地及远程删除它。
$ git branch -d branch_name  
$ git branch -D branch_name
  
$ git push origin :branch_name
  这条命令耐人寻味啊,其中origin是你的远程仓库名字(git remote -v可以查看到)。
git diff
  查看改动内容。
$ git diff filename  
$ git diff .
  
$ git diff revision1 revision2
  
$ git diff branch1 branch2
  DIFF暂存(添加到索引中)的文件:
$ git add .  
$ git diff --cached
  View the redundant Tab or Space in your codes:
$ git diff --check  
$ git diff --check --cached
  只查看更改了哪些文件:
$ git diff --name-onlygit init
git log
git merge
git mv
git pull
git push
git rebase
  你检索出代码之后(假设是master分支),创建了一个分支,然后修改了这个分支。另一个人也检索出代码(假设是master分支),然后修改了这个分支并提交到远端了。这时你需要重新从远端检索一下(master)分支的代码,然后切换到你创建的那个分支,再执行rebase命令,因为你这个分支的 base 分支已经发生变化了,你要合并到 base 分支的话需要重新 rebase 一下。
$ git clone -b master git@github.com:chenzhiwei/test.git  
$ git checkout -b new_branch
  
$ git checkout master
  
$ git pull
  
$ git checkout new_branch
  
$ git rebase
  
$ git rebase --continue
  你执行git rebase之后可能会有文件冲突,这时你需要手动解决冲突,解决之后继续rebase。
git revert
git reset
  一不小心执行了git add .,然后就可以用git reset命令来破了。
  删除最近一次 commit ,但保留这次 commit 中修改的内容,即用git status来查看会显示有文件等待提交。
$ git reset --soft HEAD^  删除最近一次 commit ,并且不保留这次 commit 中的修改内容,即用git status来查看会显示没有改动。
git stash
  当你修改了本地仓库的文件,但又想放弃修改时,可以用git stash来还原到仓库原来的状态。
git submodule
  git 是分布式的,每次只能将整个 repo 全部下载下来进行更改提交,这样明显不太合理,因为很多时候你只想改动一点代码,只需要将这部分代码所在的目录 clone 下来就行了。为了解决这个问题,git 引入了 submodule ,即一个 git repo 里可以包含多个 child git repo 。
  一个比较大的项目,里面肯定会包含很多模块及子项目,可以将这些模块和子项目分别做成 git repo ,然后用大项目的 git repo 将其包含进来就可以了,举例如下:
  一个项目(foo)包含了一个子项目(bar),主项目的 repo 是 https://github.com/chenzhiwei/linux,子项目的 repo 地址是https://github.com/chenzhiwei/vim,需要做以下操作来完成 git submodule 的创建。
$ git clone https://github.com/chenzhiwei/linux  
$ cd linux
  
$ git submodule add https://github.com/chenzhiwei/vim vim
  
$ git status
  
$ git commit -m"add a submodule vim"
  git submodule add命令会生成一个.gitmodules文件,并且当前目录下也会出现vim这个 repo 。
  其他人 clone 时默认是不会将vim的代码clone下来的,并且vim这个 submodule 体现在本地文件系统上是个目录,而在 git repo 里其实是一个空的文本文件。如果想将 submodule 即子项目的代码也全部 clone 下来,你需要执行以下操作:
$ git clone https://github.com/chenzhiwei/linux  
$ cd linux
  
$ git submodule init
  
$ git submodule update
  删除 submodule
$ git rm -r submodulegit tag
  个人感觉功能比branch强大点,因为tag里可以加注释。
$ git tag -a tagname -m"tag message"  
$ git tag tagname -f -m"new tag message"
  
$ git push origin tagname
  
$ git push orgin :tagname
  
$ git push --delete orgin tagname
  
$ git push orgin :refs/tags/tagname
git blame
  查看某个文件的某行代码是谁和在哪个commit里修改的。
$ git blame filename  会列出filename文件中所有行都是在什么时候被添加的、被谁添加的以及被谁在哪个commit里添加的。
其他
给自己项目在github上创建一个mirror
# In this example, we use an external account named extuser and  
# a GitHub account named ghuser to transfer repo.git
  

  
git clone --bare https://githost.org/extuser/repo.git
  
# Make a bare clone of the external repository to a local directory
  

  
cd repo.git
  
git push --mirror https://github.com/ghuser/repo.git
  
# Push mirror to new GitHub repository
  

  
cd ..
  
rm -rf repo.git
  
# Remove temporary local repository
  我想睡觉,因此先不写了。



运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-589265-1-1.html 上篇帖子: 使用Git合成之文件冲突 下篇帖子: git 暂存区
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表