Novice to learn the use Git Git locally

Each developer will be a version management tool in Git and SVN, I chose to Git, here are some of my experiences

What is Git

Git is the world's most advanced distributed version control system (no one).

A, Git installation

1), linux mounted:

Ubuntu Linux, by a sudo apt-get install git can be done directly Git installation is very simple.

2), windows at installation:

In Git official website to download, then all selected by default, until the point next to it

2. The first use of some settings

1), set the user name and mailbox, to distinguish between users (Note: In the windows, the installation is complete there is a Git Bash in the Start menu, click on this you can open Git, Git is written inside the command)

$ git config --global user.name "Your Name"
$ git config --global user.email "[email protected]"

2) Create the repository

Creating a repository is very simple, first of all, choose a suitable place, create an empty directory:

$ mkdir learngit
$ cd learngit
$ pwd 
D:/JavaWorkSoftware/git/learngit

Note: This last pwd is used to view the current directory, if you use Windows systems, make sure the directory name (including the parent directory) does not contain Chinese.

3), by git init command to become this directory you can manage Git repository:

$ git init
Initialized empty Git repository in D:/JavaWorkSoftware/git/learngit/.git/

Well, your Git even if the installation is complete. Then began to use it.

4), we write a file to the repository
to create a example.txt

This is an example file,
This is creat in Git.

(2) Add the files in it, use git addthe command

$ git add example.txt
warning: LF will be replaced by CRLF in example.txt.
The file will have its original line endings in your working directory

This command is generally not have any tips, but I appeared as implied above.
Meant something: LF (line feed, Line Feed) will be CRLF (carriage return line, CarriageReturn) replace
the original document will have its end of the line in the working directory.
Times this warning is due to the remote folder does not exist, but does not affect submission. We then perform just once.

(3) Submitgit commit

$ git commit -m "first example file"
[master (root-commit) 0cecffa] first example file
1 file changed, 2 insertions(+)
create mode 100644 example.txt

This parameter -m commit command is essential, it is to add a log of this submission, the equivalent of doing a marker, so that we can use in the version control.

tips:在git中只有存纯文本文件,如.txt .java .md 不能存其他文件,同时,这些纯文本文件要是UTF-8编码

Second, version control
to a local warehouse, for example, to explain the version control

1), we talk about a few concepts
work area and staging area

Workspace (Working Directory) , that is in our computer file directory can see here that I / learngit is a work area, there are a lot of branches in the workspace.

Branch (Branching) , by definition, is similar to the branches of foliage. We will decide the entire work area than the trunk, branches are on a better understanding. We are now in the main branch of Git (master) operation will be talked about behind the branch management.

Before we use mkdir learngitthe command to create a workspace that is, there is a hidden directory in the workspace .git, this is the Git repository (Repository) .

Staging area , it is the directory we are currently operating, but the content has not been saved in the workspace

2), Git under the modified file

We operate under the file in Git and how the elephant put into the refrigerator analogy (which is a method of learning I am very fond of):

(1) We example.txt modify files in the current work area (ready elephants, surely someone will ask, do not prepare your icebox? Haha, Git is this refrigerator).

This is an example file,
This is creat in Git.
This is third line.

(2) Each two-step is to be sure the elephant put into the refrigerator, we use the git addcommand

But before the elephant put into the refrigerator, let's learn a few small command
git status 查看当前git 的状态

$git status
On branch master
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)

modified: example.txt

no changes added to commit (use "git add" and/or "git commit -a") 

Example.txt modify the file
has been modified, but this change did not submit to the workspace, you are prompted to commit with git add or git

Another command is git diffused to view the differences between two files before and after the modification, do not do this demonstration.
Another git checkout -- [文件名], undo changes, that this time we do not want to suddenly elephant put into the refrigerator, we only need to undo enough.

$git checkout -- example.txt

This command does not have any tips after the implementation. We followed with a git statusview at our git status, git this means that we do not have any files you want to commit the

$ git status
On branch master
nothing to commit, working tree clean

(3) Well, after the completion of these little episode, we continue our elephant put into the refrigerator last step (modify files within the library), is to close the refrigerator door ( git commit -m).
We just performed the operation undo changes, we have to do so again.

This is an example file,
This is creat in Git.
This is third line.

$git add example.txt
$git commit -m "add third line"

3), the replacement version: the real switch before the multiple versions of
the document for testing, we modified more than a few times this file, which is a few more versions (this slightly, nothing more than vim - add - commit).
After you modify several versions, we use the git logcommand to view the current workspace (actually a branch) state.

$ git log
commit 7b0b4f5c4c9ffc053db65ed0d7835ae62a27e0b6 (HEAD -> master)
Author: Lyn4ever <[email protected]>
Date: Fri Jun 7 00:23:54 2019 +0800

add fourth line

commit 11b13721088ea653042af7986e0d5b1f1b29a9e9
Author: Lyn4ever <[email protected]>
Date: Fri Jun 7 00:16:12 2019 +0800

add third line

commit 0cecffad8b547533728d2c1a9bef581c6c01f359
Author: Lyn4ever <[email protected]>
Date: Thu Jun 6 23:45:24 2019 +0800

first example file

This is easy to understand, is now available in three versions, which is a reverse order, that is, the current version of this document is the most top of this. commit to follow behind that is the version number, followed by two lines is the author and time, and then below is what we wrote in the log when commit -m. (Note: Your file id and I certainly do not like when testing should take your subject)
modified version of the command git reset --hard commit_id, we have to try.
Make sure there are no current git not commit the file, and then view the contents of the file

$ git status
On branch master
nothing to commit, working tree clean
$ cat example.txt
This is an example file,
This is creat in Git.
this is third line.
this is forth line.

Well, we will return to the log as "add third line" this version, the version id is: 11b13721088ea653042af7986e0d5b1f1b29a9e9

$git reset --hard 11b13721088ea653042af7986e0d5b1f1b29a9e9
HEAD is now at 11b1372 add third line

Look Tip, obviously, it is through success. We look at a

$ git status
On branch master
nothing to commit, working tree clean
$ cat example.txt
This is an example file,
This is creat in Git.
this is third line.

Every time to write this id is not too much trouble then, we can only use the first seven characters, but there is a more simple method is to use HEAD^instead. HEAD represents the current version, forward several versions, write a few ^. The previous version is HEAD^, on the upper one isHEAD^^

At this time, we look at one of ourgit log

$ git log
commit 11b13721088ea653042af7986e0d5b1f1b29a9e9 (HEAD -> master)
Author: Lyn4ever <[email protected]>
Date: Fri Jun 7 00:16:12 2019 +0800

add third line

commit 0cecffad8b547533728d2c1a9bef581c6c01f359
Author: Lyn4ever <[email protected]>
Date: Thu Jun 6 23:45:24 2019 +0800

first example file

Only two found the inside, and I want to return to add fourth line that version, is the same way, but I can not find the commit-id how to do? No hurry, git so powerful not let us down. We can git reflogview the operation history git version control

$ git reflog
11b1372 (HEAD -> master) HEAD@{0}: reset: moving to 11b13721088ea653042af7986e0d5b1f1b29a9e9
7b0b4f5 HEAD@{1}: commit: add fourth line
11b1372 (HEAD -> master) HEAD@{2}: commit: add third line
0cecffa HEAD@{3}: commit (initial): first example file

At this time, we found before add fourth line version of id, on it.

$git reset --hard 7b0b4f5

It was so good. git logAlso found three versions

$ git log
commit 7b0b4f5c4c9ffc053db65ed0d7835ae62a27e0b6 (HEAD -> master)
Author: Lyn4ever <[email protected]>
Date: Fri Jun 7 00:23:54 2019 +0800

add fourth line

commit 11b13721088ea653042af7986e0d5b1f1b29a9e9
Author: Lyn4ever <[email protected]>
Date: Fri Jun 7 00:16:12 2019 +0800

add third line

commit 0cecffad8b547533728d2c1a9bef581c6c01f359
Author: Lyn4ever <[email protected]>
Date: Thu Jun 6 23:45:24 2019 +0800

first example file

4), delete files
deleted files, in fact, another modification operations, and modification is the same, first rm fileto remove, and then git commitsubmitted to the workspace to.

$rm example.txt

Then we look at the state I

$ git status
On branch master
Changes not staged for commit:
(use "git add/rm <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)

deleted: example.txt

no changes added to commit (use "git add" and/or "git commit -a")

Found to be deleted, but did not commit. That is the elephant has been taken out from the refrigerator, but not related to the refrigerator door. So we close the refrigerator door
delete start with the library

$ git rm example.txt
rm 'example.txt'

Then submit

git commit -m "delete example"

Then, I want to look at this file and found dead.

$ cat example.txt
cat: example.txt: No such file or directory

Finally, we return back to the previous version to use.

$ git reset --hard 7b0b4f5
HEAD is now at 7b0b4f5 add fourth line

$ cat example.txt
This is an example file,
This is creat in Git.
this is third line.
this is forth line.

tips: delete the file is also modified operation may be performed after rm file, but it has not git rmand git committhe git repository before you can use git checkout -- fileanameUndo

Guess you like

Origin www.cnblogs.com/Lyn4ever/p/10987444.html