Git common command collation and description (details)

Source: Internet
Author: User
Tags using git

Git common command collation and description (details)

    • Git common command collation and description details
      • Installation and Configuration
      • Workspace repository and Staging area
      • Local libraries and remote libraries
        • New Warehouse
        • Cloning from a remote library
      • Common Viewing instructions
      • Common modification Directives
      • Undo changes and version fallback
      • Branch Management
        • Creating and Merging Branches
        • Resolve Conflicts
        • Bug Branch
        • Remote Branch
      • Excellent tutorial Notes

This article is the second in the GIT series blog. In this paper, the instructions according to the use of the scene (build, view, modify, branch) to classify, introduce the basic meaning and usage of instructions, easy to access.

Installation and Configuration

Refer to my previous blog: The installation and configuration of git under each operating system platform

Workspaces, repositories, and staging area
    • Workspace : The directory you can see on your computer, like my Learngit folder is a workspace.
    • repository : The workspace has a hidden directory. Git, this isn't a workspace, it's a git repository.
    • Staging Area : There are a lot of things in Git's repository, the most important of which is the staging area called stage (or index), and the first branch master that git automatically creates for us, and a pointer to master called head.

When we add the file to the Git repository, it's done in two steps:

  1. The first step is to add the file git add , in effect, to add the file to the staging area;
  2. The second step is to commit the git commit changes, in effect, to commit all the contents of the staging area to the current branch.

Because git automatically created the only master branch for us when we created the Git repository, now git commit is committing the changes to the Master branch.

Simple Comprehension : The file changes that need to be submitted are put into staging area, and then all changes to staging area are submitted at once.

Detailed knowledge of workspaces and staging area and git basics-record every update to the warehouse

New warehouses for local libraries and remote libraries
    • Set up a remote library (empty, do not add readme.md, or the back will not push up)
    • Local new Folder
    • git initTo initialize the repository, you can find one more. Git directory in the current directory, which is git to track the management repository. Don't be blind to change
    • The name of the remote library is origin , that's what Git does by default.
    • git remote add origin [email protected]:michaelliao/learngit.gitThis command is executed under the local Learngit warehouse. The warehouse names in these two locations do not need to be the same because they are linked by executing this command in the local repository directory (the name of the remote library is included in the command)
    • git push -u origin masterPush all the contents of the local library to the remote library. Push the contents of the local library to the remote, with git push the command, actually pushing the current branch master to the remote. Since the remote library is empty, the first time we push the master branch, with the-u parameter (Push and association), GIT will not only push the local master branch content to the remote new master branch, It also associates the local master branch with the remote Master Branch and simplifies the command at a later push or pull.
    • git push origin masterPush the latest changes to the remote library after each local commit
Cloning from a remote library

Suppose GitHub already has a remote library, but not locally and needs to be cloned locally, and the remote library's name is calledgitskills

    • git clone [email protected]:michaelliao/gitskills.gitIf you clone a local library, you will have one more folder under the current folder gitskills .
    • cd gitskillsinto the cloned local library, the default name is the same as on GitHub.
    • git push origin masterThe push branch is the push of all local commits on that branch to the remote library. When pushing, specify the local branch so that git pushes the branch to the remote branch of the remote library
Common Viewing instructions
    • git statusView the current status of the warehouse
    • git diff 文件名See what changes are made to the file
    • git diff 版本号1 版本号2 --statView a list of the two different versions of the file, including the number of modified rows and additions and deletions. The parameter changes --name-status to the previous display change letter (a,m, etc.), no line number
    • git logShow commit log from nearest to furthest
    • git log --pretty=onelineSimplified display of log output, commit id very long, detailed display see here
    • git reflogRecord your every command, the first display is the version of the command after the release of the first seven bits, so that even if you clear the screen or restart, you can find a version of the version number, you can easily fall back to that version
    • git branchView the current branch. git branchcommand lists all branches with a number in front of the current branch *
    • git log --graph --pretty=oneline --abbrev-commitUsing git log with parameters, you can see the merging of the branches. git log --graphyou can see the branch merge diagram with the command
    • git remoteTo view information for a remote library
    • git remote -vShow more detailed information
Common modification Directives
    • git add readme.txtAdd, but do not commit
    • git commit -m "提交描述"Submit, only add after the submission is valid . "Change file->add file, and so on", the second modification is invalid, will not be submitted, will only successfully commit the first modification.
Undo changes and version fallback
    • git checkout -- 文件名Kill, or discard, the workspace, back to the staging state, without staging (ie, no add)
    • git reset HEAD 文件名The staging state is canceled and the workspace content is unchanged, but the status changes to not staged.

Simply put, there is no add-on modification, only need to be git checkout -- 文件名 able to undo, add the changes, first changed to git reset HEAD 文件名 No add, and then git checkout -- 文件名 revoked. Examples of operations can be seen in this picture

    • git reset --hard HEAD^Is rolled back to the previous version
    • git reset --hard 某版本号前几位With historical information on the command line (if you don't clear the screen), find the version number of a version back to the specified version. Not necessarily the full version number, just like the example of this command, as long as the previous about 7, 8 bits so can.
Branch management Create and merge branches
    • git checkout -b devCreate a new branch: Dev, and switch to the Dev branch. So this command has two functions. The git checkout command adds -b a parameter representation to create and switch, equivalent to the following two commands: git branch devgit checkout dev
    • git branch dev, the new branch is the new pointer, pointing to the current commit
    • git checkout devSwitch to Dev Branch
    • git checkout masterWhen the Dev branch is done, we can switch back to the Master branch (at this point the changes in the Dev branch are not visible on master )
    • git merge devThis is a command executed on the master branch to merge the work on the Dev branch onto the master branch
    • git branch -d devDelete the merged branch. Deleting a branch is deleting a pointer
    • git branch -D devGit friendly reminder, the Dev branch has not been merged, if deleted, will be lost changes, if you want to forcibly delete, you need to use the git branch -D dev command
    • git rebase masterVariable base. Executing the command under the current branch (not master) is equivalent to merging the current branch and Mater Branch, similar to the merge operation, but with a different commit history, the log of the rebase operation is cleaner. Refer to Git branch-variable base
Resolve Conflicts

Assume that the same file was modified on the master branch and the Feature1 branch

    • git merge feature1Executes the command on the Master branch, merging with the Feature1 branch. In this case, git cannot perform a "quick merge" and can only attempt to merge its own changes, but this merge may conflict, indeed! Git tells us that there is a conflict with the Readme.txt file and must be resolved manually before committing. git statusYou can also tell us about conflicting documents.

When merging branches, GIT uses fast forward mode if possible, but in this mode, the branch information is discarded after the branch is deleted. If you want to force the mode to be disabled, Fast forward git will generate a new commit at merge, so that branching information can be seen from the branch history

    • git merge --no-ff -m "merge with with no-ff" devPrepare to merge the Dev branch, note that the --no-ff parameter means disable Fast forward, because this merge creates a new commit, so with the-m parameter, write the commit description in
Bug Branch

Git also provides a stash function that can "store" the current work site, and then continue to work after resuming the site later

    • git stashSave the Job Site
    • git stash listView Work site
    • git stash applyRecovery work site, but after recovery, stash content is not deleted and there are multiple work sites that can be restored to a git stash apply [email protected]{0} specific site
    • git stash dropDelete the contents of stash
    • git stash popIt also removes the Stas content while recovering.
Remote Branch

This section covers only a few common operations

    • git fetch originThis command finds which server "origin" is, fetches locally-not-found data, and updates the local database, moving the pointer to the origin/master new, updated location
    • git push (remote) (branch)Pushes the local branch to update the branch with the same name on the remote repository. As mentioned earlier git push origin master , push the local master branch to the remote Master branch and, more complicated, push the git push origin serverfix:awesomebranch local Serverfix branch to the Awesomebranch branch on the remote repository
    • git push origin --delete serverfixAlternatively git push origin :remotebranch , delete the remote Serverfix branch
    • git pullIn most cases it is meant to be git fetch followed by a git merge command. Refer to Git remote operation for detailed understanding and documentation git-pull
Excellent Tutorials & Notes
  • Liaoche's git tutorial (highly recommended)
  • Pro Git
  • Manage your projects with Git and GitHub-basic operational learning
  • Manage your projects with Git and GitHub-strategies for a real development environment

Author @brianway More articles: personal website | CSDN | Oschina

Git common commands collation and description (verbose)

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.