git本地倉庫搭建(windows,github)

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-youtube video

  • 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

  1. 在github中創(chuàng)建一個新的倉庫(repository)
image.png
  1. 在本地創(chuàng)建一個文件夾杨蛋,利用終端Git CMD進入文件夾目錄希俩, 執(zhí)行初始化命令棚贾。
    git init
    這一步的目的在于把這個文件夾變成Git可以管理的倉庫。
image.png

這一步成功后, Git自動在文件夾里創(chuàng)建一個.git的目錄弯淘,用于跟蹤管理版本庫绿店。

image.png
  1. 將本地倉庫和遠程倉庫進行關聯
git remote add origin git@github.com:username/repository path.git
image.png
  1. 關聯成功以后,還不能推送同步到遠程倉庫庐橙。本地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)。
image.png
  • 進入github邦泄,Account-》Settings-》SSH and GPG keys删窒,新建SSH key,將復制的key粘貼進去顺囊。
image.png
  • 添加完成后肌索,可以使用如下命令檢查是否成功。
$ 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 分支
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命令

Git完整命令手冊

  • Configure Tooling 配置工具
  1. set the name you want attached to your commit transaction.
$ git config --global user.name"[name]"
  1. set the email you want attached to your commit transaction
$ git config --global user.email"[email address]"
  1. enable helpful colorization of command line output
$ git config --global color.ui auto
  1. 內置圖形化git
$ gitk
  1. 歷史記錄每個提交信息只顯示一行
$ git config format.pretty oneline
  1. 交互式添加文件到暫存區(qū)
$ git add -i
  • Creating repositories 創(chuàng)建倉庫
  1. 創(chuàng)建一個新的本地倉庫
$ git init [project-name]
  1. 創(chuàng)建一個本地倉庫的克隆版本
$ git clone /path/to/repository
  1. 創(chuàng)建一個遠程服務器倉庫的克隆版本
$ git clone username@host:/path/to/repository
  • Making changes 更改替換
  1. Lists all new or modified files to be committed
$ git status
  1. Shows file differences not yet staged
$git diff
  1. 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

  1. Deletes the ?le from the working directory and stages the deletion
$ git rm [file] 
  1. Removes the ?le from version control but preserves the ?le locally
$ git rm --cached [file] 
  1. Changes the ?le name and prepares it for commit
$ git mv [file-original] [file-renamed] 

-Superess tracking: exclude temporary files and paths

  1. A text ?le named .gitignore suppresses accidental versioning of ?les and paths matching the speci?ed patterns
*.log build/ temp-*

  1. Lists all ignored ?les in this project
$ git ls-files --other --ignored --exclude-standard 

-Save fragments: Shelve and restore incomplete changes

  1. Temporarily stores all modi?ed tracked ?les
$ git stash 
  1. Lists all stashed changesets
$ git stash list 
  1. Restores the most recently stashed ?les
$ git stash pop 
  1. Discards the most recently stashed changeset
$ git stash drop 

-Review history: Browse and inspect the evolution of project ?les

  1. Lists version history for the current branch
$ git log 
  1. Lists version history for a ?le, including renames
$ git log --follow [file] 
  1. Shows content di?erences between two branches
$ git diff [first-branch]...[second-branch] 
  1. Outputs metadata and content changes of the speci?ed commit
$ git show [commit] 

-Redo commits: Erase mistakes and craft replacement history

  1. Undoes all commits after [commit], preserving changes locally
$ git reset [commit] 
  1. 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

  1. Downloads all history from the repository bookmark
$ git fetch [bookmark] 
  1. Combines bookmark’s branch into current local branch
$ git merge [bookmark]/[branch] 
  1. Uploads all local branch commits to GitHub
$ git push [alias] [branch] 
  1. Downloads bookmark history and incorporate changes
$ git pull
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
  • 序言:七十年代末缺谴,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子耳鸯,更是在濱河造成了極大的恐慌湿蛔,老刑警劉巖,帶你破解...
    沈念sama閱讀 222,590評論 6 517
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件县爬,死亡現場離奇詭異阳啥,居然都是意外死亡,警方通過查閱死者的電腦和手機财喳,發(fā)現死者居然都...
    沈念sama閱讀 95,157評論 3 399
  • 文/潘曉璐 我一進店門察迟,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人耳高,你說我怎么就攤上這事扎瓶。” “怎么了泌枪?”我有些...
    開封第一講書人閱讀 169,301評論 0 362
  • 文/不壞的土叔 我叫張陵概荷,是天一觀的道長。 經常有香客問我工闺,道長乍赫,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 60,078評論 1 300
  • 正文 為了忘掉前任陆蟆,我火速辦了婚禮雷厂,結果婚禮上,老公的妹妹穿的比我還像新娘叠殷。我一直安慰自己改鲫,他們只是感情好,可當我...
    茶點故事閱讀 69,082評論 6 398
  • 文/花漫 我一把揭開白布林束。 她就那樣靜靜地躺著像棘,像睡著了一般。 火紅的嫁衣襯著肌膚如雪壶冒。 梳的紋絲不亂的頭發(fā)上缕题,一...
    開封第一講書人閱讀 52,682評論 1 312
  • 那天,我揣著相機與錄音胖腾,去河邊找鬼烟零。 笑死瘪松,一個胖子當著我的面吹牛,可吹牛的內容都是我干的锨阿。 我是一名探鬼主播宵睦,決...
    沈念sama閱讀 41,155評論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼墅诡!你這毒婦竟也來了壳嚎?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 40,098評論 0 277
  • 序言:老撾萬榮一對情侶失蹤末早,失蹤者是張志新(化名)和其女友劉穎烟馅,沒想到半個月后,有當地人在樹林里發(fā)現了一具尸體然磷,經...
    沈念sama閱讀 46,638評論 1 319
  • 正文 獨居荒郊野嶺守林人離奇死亡焙糟,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 38,701評論 3 342
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現自己被綠了样屠。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,852評論 1 353
  • 序言:一個原本活蹦亂跳的男人離奇死亡缺脉,死狀恐怖痪欲,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情攻礼,我是刑警寧澤业踢,帶...
    沈念sama閱讀 36,520評論 5 351
  • 正文 年R本政府宣布,位于F島的核電站礁扮,受9級特大地震影響知举,放射性物質發(fā)生泄漏。R本人自食惡果不足惜太伊,卻給世界環(huán)境...
    茶點故事閱讀 42,181評論 3 335
  • 文/蒙蒙 一雇锡、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧僚焦,春花似錦锰提、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,674評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至名扛,卻和暖如春谅年,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背肮韧。 一陣腳步聲響...
    開封第一講書人閱讀 33,788評論 1 274
  • 我被黑心中介騙來泰國打工融蹂, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留旺订,地道東北人。 一個月前我還...
    沈念sama閱讀 49,279評論 3 379
  • 正文 我出身青樓殿较,卻偏偏與公主長得像耸峭,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子淋纲,可洞房花燭夜當晚...
    茶點故事閱讀 45,851評論 2 361

推薦閱讀更多精彩內容