Notes for Studying Git

2019-01-23  本文已影响0人  jjx323

First Lesson

Compare differences for two large files in Linux

Commend: diff -u old_file new_file

Example: old_file.py and new_file.py are two different Python programs:

x = np.linspace(0, 10, 1000)
y = np.sin(x)
plt.plot(x, y)
plt.show()
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0, 100, 1000)
y = np.sin(x)
plt.plot(x, y)
plt.show()

Runing diff -u old_file.py new_file.py, we have the following result:

jjx323@jjx323-5520:~/Downloads$ diff -u old_file.py new_file.py
--- old_file.py 2019-01-14 21:30:38.081065810 +0800
+++ new_file.py 2019-01-14 21:31:13.696811547 +0800
@@ -1,5 +1,6 @@
 import numpy as np
-x = np.linspace(0, 10, 1000)
+import matplotlib.pyplot as plt
+x = np.linspace(0, 100, 1000)
 y = np.sin(x)
 plt.plot(x, y)
 plt.show()
\ No newline at end of file

Minus sign “-” means this line appeared in old_file.py but not in new_file.py, Positive sign “+” means this line appeared in new_file.py but not in old_file.py

Important concepts in Git

  1. Git save different versions manually to make every saved version is meaningful.

  2. User defined check-points are called "Commits" which is the building blocks for version-control in Git.

  3. Navigate to the directory where the code is saved, and type git log. Then you will get the recent "Commits" that you have made. Each one has an id(yellow line in the following example), author, date and a message associated with it.

Screenshot from 2019-01-15 15-23-35.png

In addition, you can use git diff first_id second_id to check differences between two versions.

  1. Each "commit" should address one logical change.

  2. Tracking across files, you can submit multiple files at one time.

  3. Download repository: download the entire history

    Commend: git clone http://......

    Example:git clone http://github.com/udacity/asteroids.git

  4. git checkout commit_id

  5. git workspace

Second Lesson

Compared with the normal folder, there is a hidden file .git in the git repository, which can be seen by commend: ls -a.

Create a repository

To init a git repository, you need the commend: git init.
In the git repository, there are three different spaces.

Compare differences between files

  1. Compare differences between files in working directory and files in staging area:
    git diff with no other parameters.
  2. Compare differences between files in staging area and files in repository:
    git diff --staged with no other parameters.
  3. Compare differences between different versions in repository:
    git diff commit_id1 commit_id2.

Branch

Git allows you to create labels for your commits. These labels are called Branches.
In the following example, orange words are labels, and a commit can has multiple labels.

Screenshot from 2019-01-19 19-42-32.png

git branch: this commend returns the current branches.
git branch branch_name: this commend create a new branch with branch_name.
git checkout branch_name: this commend switch to the branch with branch_name.
Example:

Screenshot from 2019-01-19 19-54-33.png

Combine branches

Combine branches into a single version.
Merging files: originally there are files A,B,D; Jack makes some changes and finally provide files B,D,E; Rachel makes some changes and finally provide files A,B,C,D. The finally merging files must contain files B,D; files A,C,E are unknown
git merge branch1 branch2: merge branch2 into branch1.
git merge branch2: merge branch2 into the current branch.

Some commends

git reset --hard: reset all the files in working directory and staging area to the most recent commit in repository.

上一篇 下一篇

猜你喜欢

热点阅读