如今不少公司都用git来管理代码,老一些的项目可能还在用svn,git比svn好的地方就在于其便利的分支管理功能,特别适用于多人协做开发,当年祖师爷linus开发git就是为了方便Linux操做系统的开发。git
git的基本用法很简单: 拉代码、提交变动、推代码!大部分公司都有本身内部的git服务器,通常都是使用gitlab,主要是安全和省钱,固然也有公司直接使用github的付费服务!无论咋样,你都须要拿到一个项目的git地址, 为了方便演示,我在github上面建立了一个演示的仓库,里面目前只有一个README.md文件:github
首先,你须要使用 git clone 拷贝一份项目代码到你本身的电脑,这个命令很简单就很少说了!shell
jwang@jwang:~$ git clone https://github.com/wangbjun/git_demo.git
Cloning into 'git_demo'...
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.
Checking connectivity... done.
复制代码
前面那步clone代码到本地以后那就能够写你本身的代码了,不过在你提交代码前我强烈建议你先更新一下代码!并且每次开始写代码以前最好都先pull一下,这样能够减小冲突,就算有冲突也能够提早发现解决!vim
有些人长时间不pull,到最后过了不少天提交的时候一大堆冲突,根本无法merge,很坑,因此我建议你们有空就pull,绝对是没毛病的!安全
改完以后固然要提交代码了,使用 git status 能够显示有哪些文件有修改!bash
jwang@jwang:~/git_demo$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: README.md
no changes added to commit (use "git add" and/or "git commit -a")
复制代码
若是你改动了多个文件可是你只想提交其中的某几个文件,你就须要使用 git add 命令添加改动的文件,在这个例子里面,就是 git add READEM.md
。服务器
若是你不想提交改动的文件,并且想撤销以前本身的更改,那你就可使用 git checkout 命令, 在这个例子里面,就是 git checkout READEM.md
。eclipse
这是紧接着第4步的,假设你已经使用 git add 命令添加了本身须要提交的文件,这时候就须要使用 git commit 来提交本身的修改,一般执行这个命令会弹出一个对话框让你添加提交信息,提交信息就是相对于一个备注吧! 编辑器
在Linux下面默认使用的是nano编辑器,不少人看到这个对话框会很懵,不知道咋用,这和vim的操做彻底不同,但也不难,直接输入你想写的内容,而后按 Ctrl+X 就会弹出一个选项,按 Y,最后回车就能够了ide
若是你实在不习惯这个编辑器,能够更改为vim,使用 git config --global core.editor vim
命令,若是你连vim都不会用。。。我建议你能够不用看下去了,下载一个图形化界面的工具吧,或者使用IDE也行,好比idea,eclipse都有自带git插件可使用。
有一个小操做,假如你修改了不少文件,并且都须要提交,你就没必要一个个 git add,跳过第4步,直接使用 git commit -a
便可。
最后一步,若是你只需本地使用git,这步就不须要了,可是大部分时候咱们须要把本身的修改提交到远程仓库,让别人也能拉取看到,这时候咱们就须要使用 git push
命令推代码。
jwang@jwang:~/git_demo$ git push
warning: push.default is unset; its implicit value has changed in
Git 2.0 from 'matching' to 'simple'. To squelch this message
and maintain the traditional behavior, use:
git config --global push.default matching
To squelch this message and adopt the new behavior now, use:
git config --global push.default simple
When push.default is set to 'matching', git will push local branches
to the remote branches that already exist with the same name.
Since Git 2.0, Git defaults to the more conservative 'simple'
behavior, which only pushes the current branch to the corresponding
remote branch that 'git pull' uses to update the current branch.
See 'git help config' and search for 'push.default' for further information.
(the 'simple' mode was introduced in Git 1.7.11. Use the similar mode
'current' instead of 'simple' if you sometimes use older versions of Git)
复制代码
请注意上面一些提示,其大概意思是自从 git 2.0版本开始,默认使用 "simple" 模式提交代码,simple模式是只会把代码提交到你 git pull 命令拉取代码的分支。其实意思就是你从哪一个分支拉取的代码就会默认push到哪一个分支,通常状况下咱们不须要更改这个。
其实最经常使用的也就是这几个命令,git clone 只须要最开始执行一次,平时用的最多的就是 git commit 和 git push,只要掌握这几个命令就能够了。
当你使用IDE或者一些图形化界面工具时更简单,好比我经常使用的PHPStorm (idea全家桶快捷键都同样), 快捷键 Ctrl+T 就是pull,Ctrl+K 能够列出全部修改文件,默认勾选全部修改过的文件,填一下提交信息,回车就是commit了。而后 Ctrl+Shift+K 就是push代码,若是不须要修改默认设置,直接回车就行,熟练操做的话很是方便,比使用命令行的效率高不少。
使用IDE还能够很是方便的查看历史记录、reset代码、合并分支、对比代码,可是命令行也是须要掌握的,毕竟有时候在服务器上面可木有图形化界面工具。。。
接下来,我会继续给你们讲讲git分支相关的操做!