Common commands for Git and GitHub

Source: Internet
Author: User
Tags git commands

In front of that, this article is followed by Colombia's GitHub series of tutorials, Colombia public number: Googdev

Fog 艹 ..... have been on a business trip (excuse) ... Looked for two months did not write the blog, is really too TM terrible!!! Hurry up and press yajing

git

Note: Before using any of the GIT commands, switch to the GIT project directory

  • git init
    Initialize a git repository

  • git status
    View the status of the current Git repository

  • git Add File
    The state of the file is modified to be added and not submitted, only the buffer exists.

  • git rm–cached file
    The status of the file after you change the add is stateless (removed from the cache)

  • git config user.name yu and git config user.email [email protected]
    Add git users, if not configured before the user will report an error, this is for the current git repository
    git config –global user.name Yu, you can use the –global parameter to set the global

  • git commit-m message
    Submit buffer content with submission information

  • git log
    Show Submission Log

  • git config core.autoclrf false
    Resolves every time you use a git command (add, commit), WARNING:LF will is replaced by CRLF in XXXX
    This is an issue caused by the automatic addition of line breaks (in Linux format), which can be set globally with the –global parameter

  • git Tag v1.0
    Make a v1.0 label for the current state.

  • git tag
    View history Tag Record

  • git branch
    To view the current branch situation

  • git branch a
    Create a new branch with the name a on the current basis

  • git Checkout a
    Toggle Branch to a

  • git checkout-b a
    Combined with the above two commands, if a branch does not exist, create branch A on the current base and switch to a

  • git Checkout v1.0
    Status when switching to tag v1.0

  • git checkout ffd9f2dd68f1eb21d36cee50dbdd504e95d9c8f7
    Switch to a commit, this long sequence is the SHA1 value of each commit, you can use git log to view

  • git checkout test.txt
    Undo Test.txt Changes, note Checkout command can only revoke files that have not been added to staging area

  • git merge a and git rebase a
    Merge the contents of the a branch into the master branch, if you want to switch to the master branch first, the difference between the two practices is:
    Merge is to merge all the contents of a in the rough, rebase will compare the order of content changes, and then merge sequentially

  • git branch-d a
    Delete Branch A

  • git branch-d a
    Force Delete Branch A

  • git config–global alias.co checkout
    alias is checkout as CO, and then when you enter a command, you can use Git co instead of git checkout, if only for current warehouse global is not required

  • How to use the NB of git log
    git log–graph–pretty=format: '%cred%h%creset-%c (yellow)%d%creset%s%cgreen (%CR)%c (bold blue) <%an>% Creset ' –abbrev-commit–date=relative
    Format the output of the log, you can clearly show the log information and branch direction
    It's so hard to write, but it's a tough order. We must give an individual name:
    git config–global alias.lg "Log–graph–pretty=format: '%cred%h%creset-%c (yellow)%d%creset%s%cgreen (%CR)%c (Bold b Lue) <%an>%creset ' –abbrev-commit–date=relative "
    Then you can use git lg .

  • git config–global color.ui true
    Coloring git's output

  • git config–global core.quotepath false
    Set display Chinese file name
    Before setting the Chinese name, I created a new file, the Chinese name,. txt, then use git status to see the following:

    After setting the Chinese name, the following is displayed:

  • The
  • stash Command
    uses the scene, as you are modifying the code on a branch a, but there is an urgent task that requires you to switch to another branch B to do some work,
    and the code on a is still a dabbler, Do not want to go to commit or even do not want to add, this time the Stash command is of great use, if no commit is executed

    • git Stash
      Save the current branch without a commit, then use git status to find that the branch is clean

    • git stash List
      You can see that there's more than one record in staging area

      After the staging succeeds, you can switch to B to do other functions
      B branch of the thing done, you can switch back to the a branch, continue to work before

    • git s Tash Apply

      After execution, the code before the a branch is back, and then you are ready to delete the staging area stash record

    • git stash drop br> is to delete a recent stash record, drop can also follow stash_id to delete the specified record

    • git stash pop
      This command is equivalent to All right, apply and drop, be cautious, use Git stash list to see if the record was actually deleted

    • git stash clear
      Empty staging Area All records, drop deletes only one, drop can be followed by stash_id to delete the specified record, or delete the nearest

  • Conflict Resolution
    Suppose I change the file test.txt at the same time on the a branch and the local branch, and when the merge is over, there will be a conflict.

    Use Git diff to view conflicts

    The conflict resolution is to modify the conflicting file Test.txt to the final content and remove the identifiers
    (identifier: ++<<<<<<< head;++=======; ++>>>>>>>>)
    Modify then add and commit

GitHub

SSH authorization is required before submitting the code to GITHUB, so create an SSH Key

  1. Generate SSH key and use commands in Git Bash or command line:

    ssh-keygen -t rsa  

    The RSA algorithm generates the key, followed by three return keys (no need to enter a password), and then generates two files Id_rsa and id_rsa.pub
    The command execution results will tell you the file default path, where Id_rsa is the key (/c/users/yu/.ssh/id_rsa), Id_rsa.pub is the public key (/c/users/yu/.ssh/id_rsa.pub)

  2. Add SSH key to GitHub
    Go to your GitHub's settings screen,

    Click on the New SSH key button, and fill in the above id_rsa.pub in the key input box below,

    Click on Add SSH Key to do it.

  3. Test secret key is successful

    ssh -T [email protected]  

    First use may prompt github.com can not be trusted, enter Yes

  4. Started dealing with GitHub.

    • Clone a project on GitHub to a local

      Create a new project/repository on GitHub (or with existing ones), assuming the name is: Github-study-test, execute the following command

      git clone [email protected]:yuehongjie/github-study-test.git

      This will clone the project to local, this time the local project itself is already a git repository, do not need to execute GIT init to initialize
      And even if the remote repository is already connected, we just need to modify or add the file under this Github-study-test project, then add and commit, just like a normal local git repository

      How to get the project address : Enter the project on GitHub, click Clone or download, and select Use SSH

    • Submit a code command to GitHub

      git push origin master  

      This commits the local project to the GitHub remote Master Branch

      Sometimes pull before push , update the remote latest code to local , command

      git pull origin master
    • Associating local projects to GitHub
      If we already have a full git repository, and we've done a lot of commits, then it's not appropriate to clone in the first place, then we need to associate the local git repository with the remote GitHub repository.

      1. Create a new project on GitHub hypothesis test

      2. The local project is assumed to be test2 associated to the remote test, using the command:

        git remote add githuborigin [email protected]:yuehongjie/test.git

        This associates the local repository with the remote repository, and the githuborigin in the command is an alias for the remote repository, which can be easily
        The reason for the alias is that we may submit it to the company's remote repository, in addition to submitting it to GitHub, to make it easier to differentiate

      3. Update remote Warehouse to local

        This step is not necessary, I do this because the remote repository created a new readme.md file, and I do not locally, pull command:

        git pull githuborigin master  
      4. Submit content from the local repository to GitHub

        Here I am committed to the master branch and of course can also be submitted to other branches

    P.S. You can use Git remote-v to see which remote warehouses the current warehouse is associated with

git & GitHub

Common Operations Commands:

Create a new branch called develop on the master branch

git branch develop

Note: The new branch is based on the current branch, where the above command is a new branch called develop based on the master branch, at which point the develop branch is exactly the same as the contents of the master branch

Switch to develop branch

git checkout develop

You can put the above two steps and merge, new and automatically switch to the develop branch:

git checkout -b develop

Push the develop branch to the remote repository

git push origin develop

Note: If the remote repository does not have a develop branch when the command executes, the error will be

Push the develop branch to the remote repository, and if the remote repository does not have the branch, it will create

git push origin develop:develop

Note: The second develop of the Develop:develop is the name of the remote branch, which can be picked up, but the recommendation remains consistent so as not to confuse

View the list of local branches

git branch

View the list of remote branches

git branch -r

View all local and remote branches

git branch -a

Delete local Branch A

git branch -d a

Delete local multiple branches simultaneously (A, B)

git branch -d a b

Force Delete local Branch a

git branch -D a

Remove Remote Branch DEVELOP2

git push origin :develop2

If there is a remote branch release, and there is no local, you want to update the remote release branch to the local

git checkout -b release origin/release

If you execute a command error:
>

Error:pathspec ' release ' did not match any file (s) known to Git.
Error:pathspec ' Origin/release ' did not match any file (s) known to Git.

Execute the following command first:

git fetch

Git and GitHub common commands

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.