Github Command Rollup

Source: Internet
Author: User

1. Install.

Msysgit is a Windows version of Git, downloaded from http://msysgit.github.io/, and then installed by default.

After the installation is complete, find Git Bash in the Start menu and pop out a command-line window that shows that the GIT installation was successful!

After the installation is complete, you will need the final step, and at the command line, enter:

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

Because Git is a distributed version control system, every machine must be tell: your name and email address. You may be worried, what if someone pretends to be someone else? This does not have to worry, first of all we believe that we are good and ignorant of the masses, and secondly, there is a fake there are ways to check.

Note that the parameters of the git config command, --global using this parameter, means that all of your git repositories on this machine will use this configuration, of course, you can also assign a different user name and email address to a warehouse.

2. Creating a repository is very simple , first of all, select a suitable place to create an empty directory:

$ mkdir learngit$ cd learngit$ pwd/Users/michael/learngit

pwdThe command is used to display the current directory. On my Mac, this warehouse is located /Users/michael/learngit .

If you are using a Windows system, in order to avoid any puzzling problems, make sure that the directory name (including the parent directory) does not contain Chinese.

The second step is to git init turn this directory into a repository that git can manage by command:

$ git initInitialized empty Git repository in /Users/michael/learngit/.git/

3. Add a Submission

The first step is to use the command git add to tell git to add the file to the repository:

$ git add readme.txt

Execute the above command without any display, that's right, the Unix philosophy is "No news is good news", stating add success.

In the second step, tell git with the command to git commit submit the file to the repository:

$ git commit -m "wrote a readme file"[master (root-commit) cb926e7] wrote a readme file 1 file changed, 2 insertions(+) create mode 100644 readme.txt

A simple explanation of the git commit command, followed by the -m submission of the instructions, you can enter any content, of course, it is desirable to make sense, so that you can easily find changes in the history record.

Too much trouble not to enter the -m "xxx" line? There are ways to do this, but it's strongly not recommended, because it's important for you to read to others. I do not want to enter the description of children's shoes please google, I do not tell you this parameter.

git commitWhen the command executes successfully, it will tell you that 1 files have been changed (our newly added Readme.txt file), inserting two lines of content (Readme.txt has two lines of content).

Why does git need to add files add in commit two steps? Because commit you can submit many files at once, you can have add different files several times, such as:

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





4. Status query. run the git status command to see the results:

$ git status

Although Git tells us that Readme.txt has been modified, it's good to see what's changed. For example, you take a vacation two weeks from abroad, the first day of work, have not remember the last how to modify the Readme.txt, so, need to use git diff this command to see:

$ git diff readme.txt diff --git a/readme.txt b/readme.txtindex 46d49bf..9247db6 100644--- a/readme.txt+++ b/readme.txt@@ -1,2 +1,2 @@-Git is a version control system.+Git is a distributed version control system. Git is free software.

git diffAs the name implies is to view difference, the format is the UNIX common diff format, you can see from the command output above, we added a "distributed" word in the first line.

5. git log The command shows the commit log from the most recent to the farthest , we can see 3 commits, the most recent being the "append GPL", the last Time "add distributed", and the first one is "wrote a Readme file". If too many output information, see dazzling, you can try to add? --pretty=oneline Parameters:

$ git log --pretty=oneline3628164fb26d48395383f8f31179f24e0882e1e0 append GPLea34578d5496d7dd233c827ed32a8cd576c5ee85 add distributedcb926e7ea50ad11b8f9e909c05226233bf755030 wrote a readme file


6. Version. in Git, with the HEAD current version, which is the latest commit 3628164...882e1e0 (note that my commit ID is not the same as yours), the previous version is, the last one is, HEAD^ HEAD^^ of course, write 100 ^ on the last 100 versions. It is easier to count, so write HEAD~100 .

Now we're going to roll back the current version of "Append GPL" to the previous version of "Add distributed", and you can use the git reset command:

$ git reset --hard HEAD^HEAD is now at ea34578 add distributed


7. Revocation. git will tell you that you git checkout -- file can discard changes to the workspace:

$ git checkout -- readme.txt

The command git checkout -- readme.txt means to readme.txt undo all changes to the file in the workspace, here are two things:

One is that readme.txt since the modification has not been put into the staging area, now, undo changes back to the same state as the repository;

One is readme.txt added to the staging area, and then modified, and now, undo changes back to the state after adding to staging area.

In short, let this file go back to git commit git add the last state or time.

Scenario 1: When you mess up the contents of a file in your workspace and want to discard the workspace changes directly, use the command git checkout -- file .

Scenario 2: When you not only changed the contents of a file in the workspace, but also added to the staging area, want to discard the changes, two steps, the first step with the command git reset HEAD file , back to Scene 1, the second step by scene 1 operation.

8. Delete.

One is to remove the file from the repository, then delete it with the command git rm , and git commit :

$ git rm test.txtrm ‘test.txt‘$ git commit -m "remove test.txt"[master d17efd8] remove test.txt 1 file changed, 1 deletion(-) delete mode 100644 test.txt

The file is now deleted from the repository.

The other is wrong, because the repository is still there, so it is easy to restore the deleted files to the latest version:

$ git checkout -- test.txt

git checkoutInstead, replace the workspace version with the version in the repository, which can be "one-click Restore", regardless of whether the workspace is modified or deleted.

 

9. Establish the SSH key . If not, open the shell (open git Bash under Windows) and create SSH Key:

$ ssh-keygen -t rsa -C "[email protected]"

If all goes well, you can find the directory in the user's home directory .ssh , there are id_rsa and id_rsa.pub two files, these two are SSH key key pair, is the private key, can id_rsa not be leaked out, id_rsa.pub is the public key, it is safe to tell anyone.

2nd step: Login to GitHub, open "Account Settings", "SSH Keys" page:

Then, click "Add SSH Key", fill in any title, paste the contents of the file in the Key text box id_rsa.pub .

10. Connect and push.

Run the command in the local learngit warehouse:

$ git remote add origin [email protected]:michaelliao/learngit.git

Please be careful to replace the above with michaelliao your own GitHub account name, otherwise, you are locally associated with my Remote library, the association is not a problem, but you later push is not pushed up, because your SSH key public key is not in my account list.

After adding, the name of the remote library is origin , this is the default for Git, or can be changed to another, but origin this name is known as a remote library.

Next, you can push all the contents of the local library to the remote library:

$ git push -u origin masterCounting objects: 19, done.Delta compression using up to 4 threads.Compressing objects: 100% (19/19), done.Writing objects: 100% (19/19), 13.73 KiB, done.Total 23 (delta 6), reused 0 (delta 0)To [email protected]:monkeyfather/learngit.git * [new branch]      master -> masterBranch master set up to track remote branch master from origin.

Pushing the contents of the local library to the remote, with git push the command, is actually pushing the current branch master to the remote.

Since the remote library is empty, the first time we push master a branch, with -u parameters, Git will not only master push the local branch content of the remote new master branch, but also the local master branch and the Remote master Branch Association, You can simplify the command at a later push or pull.

To associate a remote library, use the command git remote add origin [email protected]:path/repo-name.git ;

Once associated, use the command to git push -u origin master push all the contents of the master branch for the first time;

Thereafter, after each local submission, whenever necessary, you can use the command to git push origin master push the latest changes;

 
 
 

Github Command Rollup

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.