Git installation configuration and basic usage (Windows)

Source: Internet
Author: User
Tags diff using git

http://www.open-open.com/lib/view/open1428900970330.html Installing Git

1) Windows

Installing msysgit,:http://msysgit.github.io/

When installing, the basic choice is the default setting, but:

On the adjusting your PATH environment page, tick run Git from the Windows Command Prompt

2) Ubuntu

Use the command "Git--version" to see if it is installed and the version is 1.9.5 or higher. If not installed or the version is too low:

    1. $ sudo apt-get install git-core Git-gui git-doc GITK

3) Mac

http://sourceforge.net/projects/git-osx-installer/, not only can install Git itself (choose 1.9.5 or above), but also the GUI installation package

Start git

1) Windows

Windows: Using the Windows-brought command-line interface

You can run the git command line directly under Windows ' own command-line interface, such as

    1. D:/test> git Help

When there are special parameters in the command, double quotation marks are added. Like what

    1. d:/test> git log head^


The special symbol ^ will be misunderstood by Windows, so add double quotes and write

    1. d:/test> git log "head^"


Windows: Using Msysgit's own bash

Using bash doesn't have to add double quotes like the above. The easy way to start git bash is in Windows Explorer, right-click popup menu for the appropriate directory, Git bash. Alternatively, you can access it from the Windows Start menu.

For the first time use, click on the upper right corner of the interface, select the "Properties" item in the menu, and in the popup dialog box, tick "quick edit mode" and "insert mode" to make the copy paste easier in the future.

Note that there are pros and cons, this bash's support for Chinese is not very good.

2) Linux

    1. $ git Help

Set up Git

Whether Windows or Linux or Mac, it is recommended that you at least config the following

  1. git config--global user.name "Test" # Please change your name unless you happen to call Wukong.sun
  2. git config--global user.email "[email protected]" # ibid.
  3. git config--global push.default simple # If you want to use a low-version git (like 1.7.x), well, then don't set the current, otherwise your git doesn't support
  4. git config--global core.autocrlf false # Let Git get out of the Windows/unix line-break conversion
  5. git config--global gui.encoding utf-8 # Avoid Chinese garbled in git GUI
  6. git config--global core.quotepath off # to avoid git status display Chinese file name garbled

The last two configuration is about the Chinese garbled, basically enough.

The configuration is also required on Windows:

    1. git config--global core.ignorecase false

Set up SSH

On the Linux command line, or windos on the Git Bash Command Line window (anyway, don't use iOS), type:

    1. $ssh-keygen-t rsa-c "[Email protected]"

Then return all the way, do not enter any password and so on, generate SSH key pair. A directory is then generated. SSH with two files in it: Id_rsa, id_rsa.pub

If you are on Linux, you need to tell the local system the private key:

    1. $ Ssh-add ~/.ssh/id_rsa


Then copy the contents of the public key to the Gitlab. The specific methods are:

Displays the contents of the SSH public key:

    1. $ cat ~/.ssh/id_rsa.pub

Open GitHub page: https://github.com/settings/profile, select SSH keys, then click on Add SSH key to id_ the SSH public key just now Rsa.pub (the user directory under Windows found the. SSH folder to see) the content paste in. Do not need to fill title,title will be generated automatically.

Note: You need to copy the word "Ssh-rsa" at the very beginning.

Start using

1) Create a new Git repository

    1. $ mkdir Git_repo
    2. $ CD Git_repo
    3. $ git init
    4. $ echo "Test" > README.MKD
    5. $ git Add README.MKD
    6. $ git commit-m "add readme.mkd file"
    7. $ git Remote add origin [email protected]:username/test.git
    8. $ Git push-u Origin Master


2) Use a git repository that already exists

    1. $ CD Git_repo
    2. $ git Remote add origin [email protected]:username/test.git
    3. $ Git push-u Origin Master


Note that if prompted Fatal:remote origin already exists, then the local repository already has a remote address. You can use Git remote RM origin to delete origin, or use git remote add other_name [email protected]:username/test.git to add (remember to use git push when committing -U other_name Master).

3) Commit to multiple remote repositories at once

Assume that the existing warehouse address is: [Email protected]:username/test.git

  1. $ git clone [email protected]:username/test.git
  2. $ CD Test
  3. $ vim. Git/config
  4. [Core]
  5. repositoryformatversion = 0
  6. FileMode = True
  7. Bare = False
  8. Logallrefupdates = True
  9. [remote "origin"]
  10. url = [email protected]:username/test.git
  11. url = [email protected]:username/test.git
  12. url = [email protected]:username/test.git
  13. Fetch = +refs/heads/*:refs/remotes/origin/*
  14. [branch "master"]
  15. Remote = origin
  16. Merge = Refs/heads/master

Then the first commit will need to execute GIT push-u Origin master, and then only need to execute git push to commit the changes to the above three remote repositories.

Note: In Git 2.0, the default push action will be changed to "push only the current branch to the remote repository". If you want to continue using the GIT push both command, you need to manually set GIT push's default action git config--global push.default matching.

Push.default has a few simple actions, here are the matching and simplicity, and the two meanings are push local branches to the remote repository and push local current branch to the upstream branch. This explanation seems to be not accurate enough, man git-config to see the detailed instructions.

4) Create orphan branches on existing warehouses

Orphan branch meaning there is nothing in the branch, and there is no association with the other branches that were created earlier.

    1. $ git clone [email protected]:username/test.git
    2. $ CD Test
    3. $ git checkout--orphan new_branch
    4. Switched to a new branch ' New_branch '
    5. $ git rm-rf. # Delete all files in old working directory tree
    6. $ rm. Gitignore # If you have the file, delete it.
    7. $ echo "Orphan branch" > README.MKD
    8. $ git Add.
    9. $ git commit-m "add readme.mkd file"
    10. $ GIT push origin New_branch

5) Submit a single branch to the remote Git repository

The git push command, by default, commits all branches (branch) to the Git repository, and sometimes you just want to commit a branch to a remote repository, then you need to use Git to push Origin HEAD. Of course, you can also use the git config--GLOBAL push.default tracking command to change the default action for Git push, which means that the default is to commit only the current branch to the remote git repository when executing git push.

Common git directives


The following are some of the common git directives that can be easily understood.

1) git config

It's a good idea to configure your personal information and preferences before using Git. The following command does not have to explain the meaning of it, after executing the following command will be in your home directory (~) to generate a file ~/.gitconfig.

  1. $ git config--global user.name "username"
  2. $ git config--global user.email [email protected]
  3. $ git config--global core.editor vim
  4. $ git config--global merge.tool vimdiff
  5. $ git config--global color.status auto
  6. $ git config--global color.branch auto
  7. $ git config--global color.interactive auto
  8. $ git config--global color.diff auto
  9. $ git config--global push.default simple
  10. $ git config--global alias.co checkout
  11. $ git config--global alias.ci commit
  12. $ git config--global alias.st status
  13. $ git config--global alias.last ' log-1 HEAD '
  14. $ git config--global alias.unstage ' reset HEAD--'

2) git add

Add file contents to the index (staging file), a few simple examples:

    1. $ git Add.
    2. $ git Add--all
    3. $ git add *.txt
    4. $ git Add directory/*.sh

Suddenly you don't want git add, then execute the following command:

    1. $ git reset.
    2. $ git reset *.txt
    3. $ git reset directory/*.sh

3) git rm

Delete the index and the files in the working directory at that time.

    1. $ git RM filename
    2. $ git rm-f *.txt
    3. $ git rm-r.

4) Git commit

Record the current changes to the repository, i.e. commit changes to the local repository.

    1. $ git commit-m "Add a file and remove a file"

Suddenly you don't want git commit, then execute the following command:

    1. $ git reset head^

After you commit, you find that you have added one less file:

    1. $ git commit-m ' msg '
    2. $ git Add forget_file
    3. $ git commit--amend

Your commit has been push to the remote branch (master) and now you want to go back:

    1. $ git clone [email protected]:username/test.git
    2. $ CD Test
    3. $ git reset head^
    4. $ git push-f Master

5) Git status

View the status of the current working directory, that is, which files were modified, added, and deleted.

    1. $ git status

6) Git Checkout

Checking out a branch and directory into the current working directory can be understood simply as a command to switch branches.

The following commands switch to branch Branch1 and create a new branch new_branch, respectively.

    1. $ git checkout branch1
    2. $ git checkout-b new_branch

To cancel a local change:

    1. $ git checkout--file_name

7) Git Branch

Lists, creates, and deletes branches.

The following directives list local branches, all branches, remote branches, create, delete, force delete branches, respectively.

    1. $ git Branch--list
    2. $ git Branch--all
    3. $ git Branch--remotes
    4. $ git Branch New_branch
    5. $ git Branch--delete branch_name
    6. $ git branch-d branch_name

Deleting remote tracking branch is the branch that git branch-r command lists.

    1. $ git branch-r
    2. $ git branch-d-R origin/develop

8) Merging branches

If there is a conflict, then manual resolution of the conflict is possible.

    1. $ git checkout branch_name
    2. $ git Checkout Master
    3. $ git Merge Branch_name

9) Delete Remote Branch

If you no longer need the previous branch after merging the branch, you can delete it locally and remotely.

    1. $ git branch-d branch_name
    2. $ git branch-d branch_name
    3. $ git push origin:branch_name

This command is intriguing, where origin is your remote repository name (git remote-v can see it).

) Git diff

See what's changed.

    1. $ git diff filename
    2. $ git diff.
    3. $ git diff revision1 Revision2
    4. $ git diff branch1 Branch2

Diff staging (added to index) files:

    1. $ git Add.
    2. $ git diff--cached

View the redundant Tab or Space in your codes:

    1. $ git diff--check
    2. $ git diff--check--cached

From: http://blog.csdn.net/iam333/article/details/45023681

Git installation configuration and basic usage (Windows)

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.