Introduction to Git for Data Science

这个课程我觉得还是有些用处的, 虚拟环境用起来也很方便, 很多不同的命令也可以直接实验. 代码敲完让我从只会add, commit, push到知道stage, checkout, 冲突处理.


check the state of a repository

git status

How can I tell what I have changed?

image.png

git status shows you which files are in this staging area, and which files have changes that haven't yet been put there.

What is in a diff?

diff --git a/report.txt b/report.txt
index e713b17..4c0742a 100644
--- a/report.txt
+++ b/report.txt
@@ -1,4 +1,5 @@
-# Seasonal Dental Surgeries 2017-18
+# Seasonal Dental Surgeries (2017) 2017-18
+# TODO: write new summary

  • A line starting with @@ that tells where the changes are being made. The pairs of numbers are start line and number of lines (in that section of the file where changes occurred). This diff output indicates changes starting at line 1, with 5 lines where there were once 4.

How can I tell what's going to be committed?

git diff -r HEAD
The -r flag means "compare to a particular revision", and HEAD is a shortcut meaning "the most recent commit".

git diff -r HEAD path/to/file


How do I commit changes?

git commit -m "Program appears to have become self-aware."
重新提交commit flag
git commit --amend - m "new message"


How can I view a repository's history?

git log

How can I view a specific file's history?

git log path

How do I write a better log message

git commit without -m "message"

How does Git store information?

blob: binary large object image.png

What is a hash?

Every commit to a repository has a unique identifier called a hash (since it is generated by running the changes through a pseudo-random number generator called a hash function). This hash is normally written as a 40-character hexadecimal string like 7c35a3ce607a14953f070f0f83b5d74c2296ef93, but most of the time, you only have to give Git the first 6 or 8 characters in order to identify the commit you mean.


How can I view a specific commit?

git show 0da2f7


What is Git's equivalent of a relative path?

HEAD, HEAD~1, HEAD~2. 分别表示最新的commit, 前一个, 再前一个

git show HEAD~1


How can I see who changed what in a file?

git annotate file


How can I see what changed between two commits?

git diff ID1..ID2, 两个点需要打, 不是省略号

git diff HEAD..HEAD~2


How do I add new files?

git add file


How do I tell Git to ignore certain files?

.gitignore file.


How can I remove unwanted files?

git clean -n, show you a list of files that are in the repository, but whose history Git is not currently tracking.

git clean -f will then delete those files.


How can I see how Git is configured?

git config --list

附加选项 - --system: for all users on the computer - --global: per-user - --local: per-projece

类似于变量名的局部屏蔽, 每一层都会覆盖上一层


How can I change my Git configuration?

git config --global setting value

user.name, user.email.

How can I commit changes selectively?

If you make a mistake and accidentally stage a file you shouldn't have, you can unstage the additions with git reset HEAD and try again.


Chapter 3, 'Undo'

How do I re-stage files?

add之后又再次编辑了文件 直接git add

How can I undo changes to unstaged files?

git checkout -- filename unstaged file 意味着未被add

Use this command carefully: once you discard changes in this way, they are gone forever.


How can I undo changes to staged files?

reset HEAD 用于撤销add的文件, 然后使用checkout拉出unstaged文件的上一个状态.

git reset HEAD path/to/file git checkout -- path/to/file


How do I restore an old version of a file?

将checkout命令中的 -- 替换为版本的hash

git checkout 2242bd report.txt

只输出最近3次的log

git log -3 file


How can I undo all of the changes I have made?

git reset HEAD

git checkout -- .


Chapter 4, 'Working with branches'.

How can I see what branches my repository has?

git branch

How can I view the differences between branches?

git diff branch-1..branch-2

How can I switch from one branch to another?

git checkout branch-name

How can I create a branch?

git checkout -b new-branch-name

How can I merge two branches?

git merge source destination
# without ..


How can I merge two branches with conflicts?

首先合并分支

git merge soure dest

然后使用git status查看冲突文件.

在文件中冲突的地方, git会添加类似如下的标记.

<<<<<<< destination-branch-name
...changes from the destination branch...
=======
...changes from the source branch...
>>>>>>> source-branch-name

要处理冲突, 首先要移除标记, 然后编辑冲突内容(如只保留一个, 或其他处理方式), 最后add file, 提交commit.


Chapter 5, 'Collaborating'.


How can I create a brand new repository?

git init project-name


How can I turn an existing project into a Git repository?

`git init path


How can I create a copy of an existing repository?

git clone URL

git clone /path/to/project

git clone /project/name newprojectname


How can I find out where a cloned repository originated?

git remote
git remote -v


How can I define remotes?

You can connect any two Git repositories this way

git remote add remote-name URL
git remote rm remote-name


How can I pull in changes from a remote repository?

git pull remote branch

What happens if I try to pull when I have unsaved changes?

commit or revert local changes

How can I push my changes to a remote repository?

git push remote-name branch-name

What happens if my push conflicts with someone else's work?

先pull到本地, 解决冲突, 再push.

Comments !