The use of Git

Source: Internet
Author: User

1. Version Library (REPOSITORYY)

The workspace has a hidden directory .git , which does not count as a workspace, but a git repository. (Can be seen by command Ls-ah)

Git's repository contains a lot of things, the most important of which is called the stage (or index) of the staging area, as well as git for us to automatically create the first branch master , and pointing master to a pointer called HEAD .

Common commands:

git bash common commands: git config--GlobalUser.Name"name"# Configure a global user name, which is used by all git repositories on your computer to configure git config--GlobalUser.email"Email"# Configure the global mailbox git init # Create an empty repository git add test.py # Add File to version Library of staging area git commit-M"commit a Python file"# commits all the contents of the staging area to the current branch (the following character channeling is the intention to interpret the current commit) Git status # Current workspace and cache state git diff test.py                                # If you modify the test.py file, you can see the different git add after the change. # Add all the changed files in the current directory to the staging area (or add the unused files in the current directory to the buffer) git log # shows the most recent commit log to the furthest commits git logs--pretty=Oneline # Action with git log, one line shows a commit record git reset--hard head^ # rolls The current branch back to the previous version (HEAD is the current version, which is the last version of commit,head^, head^^ the penultimate version, head~4bottom Fourth version) git reset--hard4596780# Roll the current branch back to commit id=4596780 corresponding version (the commit ID can be displayed via the git log command, here 4,596,780 is the first seven bits of the ID) git reset HEAD test.py # To modify the staging area test.py file Undo, back to workspace git reflog # Displays all command logs submitted by git checkout--test.py # Undo changes to the file (back to the last git add or git commit state) git checkout--test.py # can also restore deleted files to the latest version of Git rm test.py # Delete repository files, then git commit required-M submission

2. Creating branches and merges (branch)

When we create a new branch, for example dev , Git creates a new pointer called dev , points to the master same commit, and then HEAD points dev to it, indicating that the current branch is dev on:

From now on, the work area is modified and submitted to the dev branch, such as after the new submission, the dev pointer moves forward one step, and the master pointer does not change:

If our dev work is done, we can merge it into the dev master top. How does git merge? The simplest way to do this is to directly master point to dev the current commit and complete the merge:

After you merge the branches, you can even delete the dev branches. Deleting a branch is to delete the dev dev pointer and delete it, leaving us with a master branch:

当两个分支masterAnd feature1 each has a new commit, which becomes this:

In this case, you cannot quickly merge, conflicts occur when you merge, and you need to manually modify the conflict before committing the changes:

Common commands:

-b Dev                     # Create dev branch and switch to that branch git branch dev                            # Create dev branch git checkout dev                         # switch to dev branch git branch                                 # View current branch git mer GE Dev                             -D Dev                         # Remove Branch devgit Stash                                # hold the current state and store it separately git stash list                             # View the working status of the archive git stash Apply
    # restores the state of the archive, but does not delete the stored state content git stash drop                            # Deletes the state content stored when the archive is saved git stash pop                            # resumes the working state of the archive and deletes the stored state contents of git stash apply [ Email protected]{0}                # Restore the specified stash (via Git stash list view all archived stash)

3. Remote Repository GitHub

After you create an empty warehouse Gitdemo on GitHub, you can clone from this repository to a local repository, or you can associate an existing local repository with it, and then push the contents of the local repository to the GitHub repository. On GitHub You can also clone someone else's warehouse to your own warehouse and clone it to a local state similar to the following:

After the modification of the local repository is submitted, it can be pushed to the remote repository for synchronization, but can only be pushed to its own remote repository, so that when multiple people collaborate, they can commit their own local modifications and stay in sync with the remote repository.

The working mode of multi-person collaboration is usually this:

    1. First, you can try to git push origin branch-name push your own changes;

    2. If the push fails, because the remote branch is newer than your local, you need git pull to first try to merge;

    3. If there is a conflict in the merge, resolve the conflict and submit it locally;

    4. No conflict or conflict resolution, and then git push origin branch-name push to Succeed!

(If git pull "No tracking information" is indicated, the link relationship between the local branch and the remote branch is not created, using the command git branch--set-upstream-to=origin/branch-name Branch-name, associate the local branch with the remote branch. )

Common commands:

remote repository git Remote add origin https://github.com/silence-cho/gitdemo.git #在远程添加origin Repositorygit push-u Origin Master # associates the local repository with the remote origin git push Origin master                                                  # Push the local master branch to the corresponding branch of the remote library (after local modification commits the local library, then synchronize) Git push Origin dev # Push the local dev branch to the remote repository git clone https://Github.com/silence-cho/gitdemo.git # Cloning remote libraries to local (when local libraries are not established)git remote # View information from the repository git remotes-v # View Remote Library, information more details git pull # Synchronize updates from the remote repository branch to the local warehouse branch Git pull Origin master--allow-unrelated-Histories # synchronizes remote Master branch updates to the local repository and allows the merging of unrelated histories (if git pull prompts: Refusing merge unrelated histories, with this command) Git branch--Set-upstream-to=origin/dev Dev # Associates the local branch dev with the remote Dev Branch (Origin/dev). (Git Pull hints "No tracking information", indicating that the local repository does not have a branch and remote Repository Dev Branch Association, local need to set up an affiliate branch)

4,git Bash Combat record

Add the local tests.py file to the local repository and push it to the remote repository, documenting a more difficult process

Join the Local repository:

推送远程仓库:

Reference blog:

1,https://www.cnblogs.com/alex3714/articles/5930846.html

2,https://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000

The use of Git

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.