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上的倉庫可以作為備份,還可以通過該倉庫和他人協(xié)作
How
- 在github中創(chuàng)建一個新的倉庫(repository)
- 在本地創(chuàng)建一個文件夾杨蛋,利用終端Git CMD進入文件夾目錄希俩, 執(zhí)行初始化命令棚贾。
git init
這一步的目的在于把這個文件夾變成Git可以管理的倉庫。
這一步成功后, Git自動在文件夾里創(chuàng)建一個.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
workflow
你可以提出更改(把它們添加到暫存區(qū)Index)
$ git add [file name]
你可以提交改動到head, 但這一步改動還沒有到你的遠端倉庫
Records ?le snapshots permanently in version history
$ git commit -m"[descriptive message]”
你可以推送你的改動特碳,從而把改動提交到遠端倉庫诚亚。master可以換成你想要推送的任何分支
$ git push origin master
假如你操作失誤,你可以替換掉本地改動
$ git checkout --[filename]
此命令會使用 HEAD 中的最新內容替換掉你的工作目錄中的文件测萎。已添加到暫存區(qū)的改動以及新文件都不會受到影響亡电。
- Branch 分支
創(chuàng)建一個名字叫做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]
-
標簽
創(chuàng)建一個叫做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
- 交互式添加文件到暫存區(qū)
$ git add -i
- Creating repositories 創(chuàng)建倉庫
- 創(chuàng)建一個新的本地倉庫
$ git init [project-name]
- 創(chuàng)建一個本地倉庫的克隆版本
$ git clone /path/to/repository
- 創(chuàng)建一個遠程服務器倉庫的克隆版本
$ 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 ?le di?erences between staging and the last ?le 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 speci?ed branch and updates the working directory
$ git checkout [branch-name]
4. Combines the speci?ed branch’s history into the current branch
$ git merge [branch]
5. Deletes the speci?ed branch
$ git branch -d [branch-name]
-**Refactor filenames**: relocate and remove files
- Deletes the ?le from the working directory and stages the deletion
$ git rm [file]
- Removes the ?le from version control but preserves the ?le locally
$ git rm --cached [file]
- Changes the ?le name and prepares it for commit
$ git mv [file-original] [file-renamed]
-Superess tracking: exclude temporary files and paths
- A text ?le named .gitignore suppresses accidental versioning of ?les and paths matching the speci?ed patterns
*.log build/ temp-*
- Lists all ignored ?les in this project
$ git ls-files --other --ignored --exclude-standard
-Save fragments: Shelve and restore incomplete changes
- Temporarily stores all modi?ed tracked ?les
$ git stash
- Lists all stashed changesets
$ git stash list
- Restores the most recently stashed ?les
$ git stash pop
- Discards the most recently stashed changeset
$ git stash drop
-Review history: Browse and inspect the evolution of project ?les
- Lists version history for the current branch
$ git log
- Lists version history for a ?le, including renames
$ git log --follow [file]
- Shows content di?erences between two branches
$ git diff [first-branch]...[second-branch]
- Outputs metadata and content changes of the speci?ed 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 speci?ed 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