Github-Use of git

Source: Internet
Author: User
Tags version control system

Github-Use of git

 

1. Download Software

Https://git-for-windows.github.io/

Ii. Installation

Installed successfully

Iii. Configuration

1. Open "Git"-> "Git Bash"

2. Configure the user

Because git is a distributed version control system, every machine must report its own name and email

Enter

 

$ git config --global user.name "Your Name"$ git config --global user.email "email@example.com"

Note that the -- global parameter of the git config command uses this parameter to indicate that all Git Repositories on your machine will use this configuration, you can also specify different user names and Email addresses for a repository.

 

3. Create a version Library

A version library, also known as a repository, has the English name repository. You can simply think of it as a directory. All files in this directory can be managed by Git, and each file can be modified or deleted, and Git can be tracked, so that you can track history at any time, or "Restore" at a certain time point in the future ".

(1) enter a directory

Ls command --- list all directories

Pwd command ---- display current path

(2) create an empty directory myGit

Run the mkdir command.

Note: If you are using a windows system, make sure that the directory name (including the parent directory) does not contain Chinese characters to avoid any inexplicable problems.

(3) change the directory to a git-managed repository.

Run git init

 

Git builds the repository in an instant and tells you that it is an empty repository (empty Git repository). careful readers can find that there is another repository in the current directory. git Directory, which is used by Git to track and manage version libraries. Never modify the files in this directory manually. Otherwise, the Git repository will be damaged.

If you do not see the. git directory, it is because the directory is hidden by default. You can see it using the ls-ah command.


(4) Add the file to the version Library

Compile a test.txt File

Be sure to put it in the myGit directory (sub-directories also work ). [This is a git repository]

* ********** It takes two steps to put a file into the git repository **********

Step 1: Run git add to add the file to the repository.

If no message is displayed, the message is successfully added. [Unix philosophy is "no message is good news "]

Step 2: Run git commit to submit the file to the repository.

-M is followed by the description of this submission. You can enter any content. Of course, it is best to make sense, so that you can easily find the change record from the history.

Add multiple files at a time

 

$ git add file1.txt$ git add file2.txt file3.txt$ git commit -m "add 3 files."

 

Iv. Operations

1. Run the git status Command to view the result.

Before Modification

After modification

2. Run the git diff command to view the modified content.

3. Run the git log command to view historical operation records.

Git log -- pretty = oneline command to simplify information

4. git reset command version rollback

1) HEAD

First, Git must know which version the current version is. In Git, use HEAD to indicate the current version, that is, the latest commit 3628164... 882e1e0 (note that my submission ID is definitely different from yours). The previous version is HEAD ^, and the previous version is HEAD ^, of course it is easier to write 100 ^ in 100 versions, so it is written as HEAD ~ 100.

2) version number

5. The git reflog command is used to record each of your commands.

6. git checkout-file modification in the workspace is all revoked.

 

Run git checkout -- readme.txtto cancel all modifications to the readme.txt file in the workspace. There are two situations:

Readme.txt has not been put into the temporary storage zone after modification. Now, undo the modification and return to the same status as the version database;

One is that readme.txt has been added to the temporary storage area and is modified again. Now, undo the modification and return to the status after it is added to the temporary storage area.

In short, this file is returned to the status of the last git commit or git add.


Note: The -- In the git checkout -- file command is very important. If there is no --, it becomes the "switch to another branch" command.

7. Run the cat command to view the file content.

 

Summary:

Scenario 1: When you change the content of a file in the workspace and want to discard the modification, run git checkout -- file.

Scenario 2: When you not only disrupt the content of a file in the workspace, but also add the file to the temporary storage area, you want to discard the modification in two steps. The first step is to use the command git reset HEAD file, return to scenario 1. Step 2: perform operations based on scenario 1.


8. Run the rm command to delete an object.

Rm ---- delete the file directly in the file manager,The workspace and version library are inconsistent.

Git rm --- use the command git rm to delete files, and use the command git commit

If you accidentally delete it, You can copy it from the version library again.

Run git checkout -- file

5. remote warehouse

1. Preparations

Step 2: Create an SSH Key. In the user's main directory, check whether there is a. ssh directory. If so, check whether there are id_rsa and id_rsa.pub files in this directory. If there are already two files, you can directly jump to the next step. If no, Open Shell (Open Git Bash in Windows) and create an SSH Key:

As follows:

Step 2: log onto GitHub and open the "Account settings" and "SSH Keys" pages:

Click "Add SSH Key", fill in any Title, and paste the content of the id_rsa.pub file in the Key text box:

Click "Add Key", as shown below:

 

Why does GitHub need an SSH Key? Because GitHub needs to identify that the submission you push is indeed pushed by you, rather than impersonating others, and Git supports the SSH protocol, GitHub only needs to know your public key, you can confirm that only you can push.

Of course, GitHub allows you to add multiple keys. Assume that you have several computers. If you submit them at the company and submit them at home later, you only need to add the keys of each computer to GitHub, you can push data to GitHub on each computer.

Finally, the Friendly reminder is that anyone can see the free Git repository hosted on GitHub (but only you can change it ). Therefore, do not include sensitive information.

If you don't want others to see the Git repository, there are two ways: one is to pay the protection fee and let GitHub convert the public repository to private, in this way, others will be invisible (neither readable nor writable ). Another method is to build a Git server by yourself. Because it is your own Git server, no one else can see it. This method is quite simple and necessary for internal development.


2. Create a remote warehouse

3. associate a remote database

Command reference

Command:

$ git remote add origin git@github.com:wuhaining/wuhaining.github.io.git

 

View

$git remote -v

Push local content to remote database

 

Push the content of the local database to a remote location. Use the git push command to actually push the current branch master to a remote location.

Because the remote database is empty, when we push the master branch for the first time, the-u parameter is added, Git not only pushes the content of the local master branch to the remote master branch, it also associates the local master branch with the remote master branch, and simplifies the command in the future push or pull.


What's the problem here?

According to the online analysis:

The code version in the remote repository is inconsistent with that in the local repository.

 

Solution:

git pull

Automatic merge or manual merge conflict

Git push again


This will synchronize

 

Summary:

To associate a remote library, run the command git remote add origin git @ server-name: path/repo-name.git;

After Association, use the command git push-u origin master to push all the content of the master branch for the first time;

After that, after each local commit, you can use the command git push origin master to push the latest modification as long as necessary;


4. Create a branch

 

First, create the dev branch and switch to the dev Branch:

$ git checkout -b devSwitched to a new branch 'dev'

The git checkout command is added with the-B parameter to create and switch, which is equivalent to the following two commands:

$ git branch dev$ git checkout devSwitched to branch 'dev'

Run the git branch command to view the current branch:

$ git branch* dev master

The git branch command lists all branches. A * sign is marked before the current branch.

 

Test

Change content

Submit

 

Now, after the dev branch is completed, we can switch back to the master Branch:

$ git checkout masterSwitched to branch 'master'

Switch back to the master branch and upload a readme.txt file. The added content is missing! Because the commit is on the dev branch, the commit point of the master branch is not changed at the moment:

 

Now, we merge the work of the dev branch into the master Branch:

$ git merge devUpdating d17efd8..fec145aFast-forward test.txt | 1 + 1 file changed, 1 insertion(+)

The git merge command is used to merge a specified branch to the current branch. After merging, merge the content of test.txt to see that the latest commit of the dev branch is exactly the same.

Note the above Fast-forward information. Git tells us that this merge is a "Fast forward mode", that is, direct the master to the current commit of dev, so the merge speed is very Fast.

Of course, not every merge can be Fast-forward. We will merge other methods later.

After merging, you can safely delete the dev Branch:

$ git branch -d devDeleted branch dev (was fec145a).

After deletion, check branch, and only the master branch is left:

$ git branch* master

Because it is very fast to create, merge, and delete branches, Git encourages you to use a branch to complete a task and then delete the branch after merging. This works the same way directly on the master branch, but the process is safer.

Git encourages extensive use of branches:

View branch: git branch

Create branch: git branch

Switch branch: git checkout

Create + switch branch: git checkout-B

Merge a branch to the current branch: git merge

Delete branch: git branch-d

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.