Git学习记录:Notes for Codecademy

Codecademy学习记录

Basic Git Workflow(Git工作流初探)

  • git三大部分
Git学习记录:Notes for Codecademy_第1张图片
  • 编辑一份文件,名为scene-1.txt
Git学习记录:Notes for Codecademy_第2张图片
  • 查看当前工作目录
  • 将当前工作目录变为Git项目
  • check the status of the changes
    untracked files说明Git已经检测到文件,但是Git尚未开始追踪变化(Git sees the file but has not started tracking changes yet)
Git学习记录:Notes for Codecademy_第3张图片
  • 将文件加入到git的staging area
Git学习记录:Notes for Codecademy_第4张图片
  • 查看添加之后的状态
Git学习记录:Notes for Codecademy_第5张图片
  • 比较改动后的文件差异
    在scene-1.txt文件上略作改动,并用git diff scene-1.txt看Staging Area上该文件和Working Directory上的差异
    注意,此时对文件的改动是在Working Directory上的,如果没有使用git add filename的话,改动的文件不会上传到staging area
Git学习记录:Notes for Codecademy_第6张图片
  • 使用git commit将文件提交到Repository
Git学习记录:Notes for Codecademy_第7张图片
  • 使用git log查看提交记录
    橘黄色的40字节的编码(40-character code)是SHA码,即本次提交的指纹码
    除此之外,还包括提交者、提交日期以及提交注释


    Git学习记录:Notes for Codecademy_第8张图片

**How to Backtrack(如何撤销更改) **

  • 编辑一下scene-5.txt文件,git commit之后使用git show HEAD命令查看当前头节点
    scene-5.txt上新增加的内容被标记成绿色并展示出来


    Git学习记录:Notes for Codecademy_第9张图片
  • 在工作目录上的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
Git学习记录:Notes for Codecademy_第10张图片
改动scene-2.txt并add到Staging Area中的状态
  • 重新设置提交节点
Git学习记录:Notes for Codecademy_第11张图片
重设节点之前的状态

现在想要还原提交至指纹码首6位为8f18e6的节点,使用git reset 8f18e6重设节点,重设节点之后,scene-3.txt以及scene-7.txt上的改名变成了unstaged changes

重设节点示意图


Git学习记录:Notes for Codecademy_第12张图片

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分支
Git学习记录:Notes for Codecademy_第13张图片
  • 当merge conflict发生时的处理
    在master分支上对一份文件进行了改动,而后又在其他branch上对文件进行了改动,此时,如果要将其他branch合并到master上,就会发生合并冲突。
    比如说在master的resume.txt文件上改动了第30行并且最终commit到Repository上,而后又切换到了fencing分支,并且在resume.txt文件的同一行进行了改动,然后commit到Repository上,此时再切换回master分支,使用git merge fencing命令,会出现如下结果:
Git学习记录:Notes for Codecademy_第14张图片
Git学习记录:Notes for Codecademy_第15张图片

此时删除掉<<<、===这样的字符,重新编辑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学习记录:Notes for Codecademy_第16张图片
  • 使用git merge origin/master将远程分支上的更新同步到本地
Git学习记录:Notes for Codecademy_第17张图片
  • 多人协作基本流程
    1.从远程服务器同步更新
    2.创建新的分支,以编辑新的内容
    3.commit改动的文件到该新分支上
    4.从远程服务器同步更新(以防在自己编辑新内容的过程中,远程分支上的内容又更新了)
    5.将自己的分支push到远程分支以供review
Git学习记录:Notes for Codecademy_第18张图片
将本地的改动commit到自己创建的本地分支
Git学习记录:Notes for Codecademy_第19张图片
使用git push origin your_branch_name将本地分支的改动提交到远程分支

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 :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 状态

你可能感兴趣的:(Git学习记录:Notes for Codecademy)