What
*中文翻译感觉比较拗口,直接搬了英文解释
Git: a version control system (VCS) for tracking changes in computer files and coordinating work on those files among multiple people. It is primarily used for source code management in software development, but it can be used to keep track of changes in any set of files.
Git GUI: aimed at people who don't like the coding on black screens, it provides a graphical user interface to run the git commands.
Git bash: bash is a Unix shell and command language, and is the default shell on Linux and OS X. In laymen terms, the git which runs on the terminal of any Linux device is known as git bash.
Git CMD(command line prompt) is the command-line interpreter on Windows operating systems. Sort of an equivalent to the terminal in Linux.
GitHub: a web-based Git or version control repository and internet hosting service. It offers all the distributed version control and source code management (SCM) functionality of Git as well as adding its own features.
*总结一下,就是一个通过Git进行版本控制的软件源代码托管服务。号称全球最大男性交友平台(误)
Why
- 在本地和远程各有一个git仓库用来储存自己的代码,两个仓库可以进行同步
- GitHub上的仓库可以作为备份,还可以通过该仓库和他人协作
How
- 在github中创建一个新的仓库(repository)
- 在本地创建一个文件夹,利用终端Git CMD进入文件夹目录, 执行初始化命令。
git init
这一步的目的在于把这个文件夹变成Git可以管理的仓库。
这一步成功后, Git自动在文件夹里创建一个.git的目录,用于跟踪管理版本库。
- 将本地仓库和远程仓库进行关联
git remote add origin git@github.com:username/repository path.git
- 关联成功以后,还不能推送同步到远程仓库。本地Git和GitHub仓库之间传输是通过SSH加密的,但现在所用的SSH key是public key,不在自己的GitHub账户列表里。所以需要配置SSH key验证信息。
- 首先使用Git Bash生成SSH Key
$ ssh-keygen -t rsa -C "yourgithubemail@example.com"
- 使用默认路径,命令会在C:\Users\Administrator中生成.ssh文件夹。使用记事本打开其中的id_rsa.pub,复制内容(ssh key)。
- 进入github,Account-》Settings-》SSH and GPG keys,新建SSH key,将复制的key粘贴进去。
- 添加完成后,可以使用如下命令检查是否成功。
$ ssh -T git@github.com
Hi tianqixin! You've successfully authenticated, but GitHub does not provide shell access.
Git的简要流程
以下摘自Git教程: http://www.runoob.com/git/git-tutorial.html
-
Workflow
你可以提出更改(把它们添加到暂存区Index)
$ git add [file name]
你可以提交改动到head, 但这一步改动还没有到你的远端仓库
Records file snapshots permanently in version history
$ git commit -m"[descriptive message]”
你可以推送你的改动,从而把改动提交到远端仓库。master可以换成你想要推送的任何分支
$ git push origin master
假如你操作失误,你可以替换掉本地改动
$ git checkout --[filename]
此命令会使用 HEAD 中的最新内容替换掉你的工作目录中的文件。已添加到暂存区的改动以及新文件都不会受到影响。
- Branch 分支
创建一个名字叫做feature_x的分支,并切换过去
$ git checkout -b feature_x
切换回主分支
$ git checkout master
删掉新建的分支
$ git branch -d feature_x
将分支推送到远程仓库,才能被他人所看到
$ git push origin[branch]
-更新与合并
要更新你的本地仓库至最新改动
$ git pull
在你的工作目录中获取(fetch)并合并(merge)远端的改动
- 要合并其他分支到你当前的分支(例如master)
$ git merge [branch]
- 可能会出现冲突,这时候就需要手动修改这些文件来手动合并这些冲突。改完之后,可以将它们标记为合并成功。
$ git add [file name]
- 在合并改动前,你可以使用如下命令预览差异
$ git diff [source_branch][target_branch]
-
标签
创建一个叫做1.0.0的标签
$ git tag 1.0.0 1b2e1d63ff
后面那一串字符是你想要标记的提交ID的前10位字符(也可以使用少几位,只要能确定唯一性),可以使用以下命令获取
$ git log
常用Git命令
- Configure Tooling 配置工具
- set the name you want attached to your commit transaction.
$ git config --global user.name"[name]"
- set the email you want attached to your commit transaction
$ git config --global user.email"[email address]"
- enable helpful colorization of command line output
$ git config --global color.ui auto
- 内置图形化git
$ gitk
- 历史记录每个提交信息只显示一行
$ git config format.pretty oneline
- 交互式添加文件到暂存区
$ git add -i
- Creating repositories 创建仓库
- 创建一个新的本地仓库
$ git init [project-name]
- 创建一个本地仓库的克隆版本
$ git clone /path/to/repository
- 创建一个远程服务器仓库的克隆版本
$ git clone username@host:/path/to/repository
- Making changes 更改替换
- Lists all new or modified files to be committed
$ git status
- Shows file differences not yet staged
$git diff
- Shows file differences between staging and the last file version
$ git diff --staged
-Group changes 群更改
1. Lists all local branches in the current repository
$ git branch
2. Creates a new branch
$ git branch [branch-name]
3. Switches to the specified branch and updates the working directory
$ git checkout [branch-name]
4. Combines the specified branch’s history into the current branch
$ git merge [branch]
5. Deletes the specified branch
$ git branch -d [branch-name]
-**Refactor filenames**: relocate and remove files
- Deletes the file from the working directory and stages the deletion
$ git rm [file]
- Removes the file from version control but preserves the file locally
$ git rm --cached [file]
- Changes the file name and prepares it for commit
$ git mv [file-original] [file-renamed]
-Superess tracking: exclude temporary files and paths
- A text file named .gitignore suppresses accidental versioning of files and paths matching the specified patterns
*.log build/ temp-*
- Lists all ignored files in this project
$ git ls-files --other --ignored --exclude-standard
-Save fragments: Shelve and restore incomplete changes
- Temporarily stores all modified tracked files
$ git stash
- Lists all stashed changesets
$ git stash list
- Restores the most recently stashed files
$ git stash pop
- Discards the most recently stashed changeset
$ git stash drop
-Review history: Browse and inspect the evolution of project files
- Lists version history for the current branch
$ git log
- Lists version history for a file, including renames
$ git log --follow [file]
- Shows content differences between two branches
$ git diff [first-branch]...[second-branch]
- Outputs metadata and content changes of the specified commit
$ git show [commit]
-Redo commits: Erase mistakes and craft replacement history
- Undoes all commits after [commit], preserving changes locally
$ git reset [commit]
- Discards all history and changes back to the specified commit
$ git reset --hard [commit]
-Synchronize changes: Register a repository bookmark and exchange version history
- Downloads all history from the repository bookmark
$ git fetch [bookmark]
- Combines bookmark’s branch into current local branch
$ git merge [bookmark]/[branch]
- Uploads all local branch commits to GitHub
$ git push [alias] [branch]
- Downloads bookmark history and incorporate changes
$ git pull