Codecademy學習記錄
Basic Git Workflow(Git工作流初探)
- git三大部分
- 編輯一份文件,名為scene-1.txt
- 查看當前工作目錄
- 將當前工作目錄變為Git項目
- check the status of the changes
untracked files說明Git已經檢測到文件,但是Git尚未開始追蹤變化(Git sees the file but has not started tracking changes yet)
- 將文件加入到git的staging area
- 查看添加之后的狀態
- 比較改動后的文件差異
在scene-1.txt文件上略作改動,并用git diff scene-1.txt看Staging Area上該文件和Working Directory上的差異
注意,此時對文件的改動是在Working Directory上的,如果沒有使用git add filename的話,改動的文件不會上傳到staging area
- 使用git commit將文件提交到Repository
-
使用git log查看提交記錄
橘黃色的40字節的編碼(40-character code)是SHA碼,即本次提交的指紋碼
除此之外,還包括提交者、提交日期以及提交注釋
**How to Backtrack(如何撤銷更改) **
-
編輯一下scene-5.txt文件,git commit之后使用git show HEAD命令查看當前頭節點
scene-5.txt上新增加的內容被標記成綠色并展示出來
-
在工作目錄上的scene-5.txt文件上做若干改動
比如將flames改成balloons
-
使用git checkout HEAD scene-5.txt使文件恢復成服務器上的版本
此時,你并沒有把在本地工作目錄上改動的文件提交到Staging Area或者Repository,如果你想要丟棄改動
使用git checkout HEAD filename,其效果等同于Android Studio中revert
- 使用git add filename1 filename2增加多個文件到Staging Area
在scene-3.txt以及scene-7.txt兩個文件進行改動,并且提交到Staging Area
- 使用git reset HEAD filename將指定的文件從Staging Area中移除
此時,scene-3.txt以及scene-7.txt文件已經提交到了Staging Area中,故意在scene-2.txt文件上做一些改動,然后將scene-2.txt文件add到Staging Area中,此時如果想要還原scene-2.txt的提交,使用git reset HEAD scene-2.txt
- 重新設置提交節點
現在想要還原提交至指紋碼首6位為8f18e6的節點,使用git reset 8f18e6重設節點,重設節點之后,scene-3.txt以及scene-7.txt上的改名變成了unstaged changes
重設節點示意圖
Git Branching(Git分支)
- 使用git branch查看當前所在分支
- 使用git branch fencing創建一個名為“fencing”的新分支
- 使用git checkout fencing切換到fencing分支
- 合并fencing分支到master分支
先通過git checkout master切換到master分支,然后通過git merge fencing,將fencing分支以fast forward方式合并到master分支
- 當merge conflict發生時的處理
在master分支上對一份文件進行了改動,而后又在其他branch上對文件進行了改動,此時,如果要將其他branch合并到master上,就會發生合并沖突。
比如說在master的resume.txt文件上改動了第30行并且最終commit到Repository上,而后又切換到了fencing分支,并且在resume.txt文件的同一行進行了改動,然后commit到Repository上,此時再切換回master分支,使用git merge fencing命令,會出現如下結果:
此時刪除掉<<<、===這樣的字符,重新編輯resume.txt文檔,然后再次提交文件
- 使用git branch -d fencing刪除fencing分支
Git Teamwork(Git協同工作)
- 使用git clone remote_location clone_name
使用git clone science-quizzes my-quizzes將remote上的science-quizzes文件夾克隆到本地,并且命名為my-quizzes
- 使用git remote -v查看已經存在的遠程分支
- 使用git fetch查看遠程分支上的改動
- 使用git merge origin/master將遠程分支上的更新同步到本地
- 多人協作基本流程
1.從遠程服務器同步更新
2.創建新的分支,以編輯新的內容
3.commit改動的文件到該新分支上
4.從遠程服務器同步更新(以防在自己編輯新內容的過程中,遠程分支上的內容又更新了)
5.將自己的分支push到遠程分支以供review
Git命令匯總
git init:creates a new Git repository
git status:inspects the contents of the working directory and staging area
git add:adds files from the working directory to the staging area
git diff:shows the difference between the working directory and the staging area
git commit:permanently stores file changes from the staging area in the repository
git log:shows a list of all previous commits
git show HEAD:shows the last commit
git checkout HEAD filename:Discards changes in the working directory
git reset HEAD filename:Unstages file changes in the staging area
git reset SHA:Can be used to reset to a previous commit in your commit history.
git branch:Lists all a Git project's branches.
git branch new_branch_name:Creates a new branch.
git checkout branch_name:Used to switch from one branch to another.
git merge branch_name:Used to join file changes from one branch to another.
git branch -d branch_name:Deletes the branch specified.
git clone:Creates a local copy of a remote.
git remote -v:Lists a Git project's remotes.
git fetch:Fetches work from the remote into the local copy.
git merge origin/master:Merges origin/master into your local branch.
git push origin <branch_name>:Pushes a local branch to the origin remote.
其他資料
Why did my Git repo enter a detached HEAD state?
git 查看遠程分支、本地分支、刪除本地分支
Git之detached HEAD
git checkout之一 HEAD基本和detached 狀態