Git的简明指南
我个人对Git或者Svn没什么偏好,这个如果个人开发用什么都一样,公司的话决定我用什么。可能是本人技术理解有问题,所以不参与两者的对比和争吵。
一般来讲,Git应该是一个必备技能,因为github的存在,每个写程序的人估计都有个github,虽然可能顶上一年只有几个提交。
再次强调本随笔无任何高深内容,纯是写写英文。如果想学细一点,可参考这篇博客 https://juejin.im/post/5ae072906fb9a07a9e4ce596,这东西熟能生巧,碰到问题再谷歌不迟。
主要涉及的点:
- 工作区:本地电脑存放项目文件的地方,比如learnGitProject文件夹;
- 暂存区(Index/Stage):在使用git管理项目文件的时候,其本地的项目文件会多出一个.git的文件夹,将这个.git文件夹称之为版本库。其中.git文件夹中包含了两个部分,一个是暂存区(Index或者Stage),顾名思义就是暂时存放文件的地方,通常使用add命令将工作区的文件添加到暂存区里;
- 本地仓库:.git文件夹里还包括git自动创建的master分支,并且将HEAD指针指向master分支。使用commit命令可以将暂存区中的文件添加到本地仓库中;
- 远程仓库:不是在本地仓库中,项目代码在远程git服务器上,比如项目放在github上,就是一个远程仓库,通常使用clone命令将远程仓库拷贝到本地仓库中,开发后推送到远程仓库中即可;
入门
1.创建一个仓库
create a new directory, open it and perform a git init to create a new git repository.
git init
2.检出一个项目
create a working copy of a local repository by running the command
git clone /pah/to/...
when using a remote server,your command will be
git clone username@host:/path/to/...
3.工作流
your local repository consists of three "trees" maintained by git. the first one is your Working Directory which holds the actual files. the second one is the Index
which acts as a staging area and finally the HEAD
which points to the last commit you've made.
4.添加和提交
You can propose changes (add it to the Index) using
git add <filename>
git add *
This is the first step in the basic git workflow. To actually commit these changes use
git commit -m "代码提交信息"
Now the file is committed to the HEAD, but not in your remote repository yet.
5.推送改动
Your changes are now in the HEAD of your local working copy. To send those changes to your remote repository, execute
git push origin master
Change master to whatever branch you want to push your changes to.
If you have not cloned an existing repository and want to connect your repository to a remote server, you need to add it with
git remote add origin <server>
Now you are able to push your changes to the selected remote server
6.分支
Branches are used to develop features isolated from each other. The master branch is the "default" branch when you create a repository. Use other branches for development and merge them back to the master branch upon completion.
create a new branch named "feature_x" and switch to it using
git checkout -b feature_x
switch back to master
git checkout master
and delete the branch again
git branch -d feature_x
a branch is not available to others unless you push the branch to your remote repository
git push origin <branch>
7.更新与合并
to update your local repository to the newest commit, execute
git pull
in your working directory to fetch and merge remote changes.
to merge another branch into your active branch (e.g. master), use
git merge <branch>
in both cases git tries to auto-merge changes. Unfortunately, this is not always possible and results in conflicts. You are responsible to merge those conflicts manually by editing the files shown by git. After changing, you need to mark them as merged with
git add <filename>
before merging changes, you can also preview them by using
git diff <source_branch> <target_branch>
8.标签
it's recommended to create tags for software releases. this is a known concept, which also exists in SVN. You can create a new tag named 1.0.0 by executing
git tag 1.0.0 1b2e1d63ff
the 1b2e1d63ff stands for the first 10 characters of the commit id you want to reference with your tag. You can get the commit id by looking at the log
9.日志
in its simplest form, you can study repository history using..
git log
You can add a lot of parameters to make the log look like what you want.
To see only the commits of a certain author:
git log --author=bob
To see a very compressed log where each commit is one line:
git log --pretty=oneline
Or maybe you want to see an ASCII art tree of all the branches, decorated with the names of tags and branches:
git log --graph -- oneline --decorate --all
See only which files have changed:
git log --name-status
These are just a few of the possible parameters you can use. For more,
git log --help
10.替换本地操作
In case you did something wrong, which for sure never happens ;), you can replace local changes using the command
git checkout -- <filename>
this replaces the changes in your working tree with the last content in HEAD. Changes already added to the index, as well as new files, will be kept.
If you instead want to drop all your local changes and commits, fetch the latest history from the server and point your local master branch at it like this
git fetch origin
git reset --hard origin/master