The use of Git

Source: Internet
Author: User

git configuration file
    1. Git's configuration file location
      For all users:/etc/gitconfig
      For current User: ~/.gitconfig

    2. To view the configuration method

      --list
    3. Ways to modify Configurations

      git config --global user.name "wangyubin"  (修改的是~/.gitconfig)git config --system user.name "wangyubin"  (修改的是/etc/gitconfig)
Git basic usage
  1. Clone existing Warehouse

    URL  (URL支持git,ssh,http,https等各种协议)
  2. Each state of a file in Git

    • There are no related records for this file in the Unstaged-git warehouse
    • There is a record of this file in the Modified-git warehouse, and this file is currently changed
    • Staged-Append, delete or modify files are temporarily saved, these additions, deletions and modifications are not submitted to the GIT repository
    • Commited-Files that have been appended or modified are submitted to the local Git repository (most of which are in the Git repository, so git status does not show these files)
  3. View the status of files in a git repository

    status
  4. Initialize a warehouse

    git init

    Generate the. Git directory under the current folder, complete the initialization, at which point all files under this folder are in the Unstaged state

  5. Append file

     git add  test.c    

    test.c file becomes staged state, other files are unstaged status

    5.1 Append file result 1-revert to previous state (to unstaged)

     git rm--cache test.c    

    5.2 Append result of file 2-commit to Git repository (changed to commited)

     git commit-m " my message "   
  6. Modify a file

    "aaa"  >> test.c

    TEST.C status changed to modified

    6.1 Modifying the results of a file 1

    git add test.c  (暂时保存修改的内容,即变为staged)

    There are 2 paths to choose from:
    6.1.1 Cancel the temporary save.

    git reset test.c  (状态变回modified)

    6.2.2 commit the staged changes to the Git repository

    git commit -m "my message"

    6.2 Modifying the results of a file 2

    test.c  (将test.c恢复为git仓库中的最新版本,即变为commited状态,test.c的内容和5.2节一样)
  7. deleting files
    7.1 Deleting files from git repositories and disks

    rm test.c  (当前目录中删除了test.c,在git仓库中暂时删除了test.c,相当于staged状态)

    7.1.1 Remove test.c from the Git repository

    "my message"  (git仓库以后不再维护test.c)

    7.1.2 deleted the wrong, restore the operation just now

    test.c  (恢复到删除前的状态,当前目录中已删除的test.c也恢复了,test.c仍文commited状态)

    7.2 Delete files from the Git repository only

    rm --cache test.c (当前目录中没有删除了test.c,仅在git仓库中暂时删除了test.c,相当于staged状态)

    7.2.1 Remove test.c from the Git repository

    git commit -m "my message"  (git仓库以后不再维护test.c,但是当前目录中仍然有test.c)

    7.2.2 deleted the wrong, restore the operation just now

    git reset HEAD test.c  (和7.1.2一样)

    7.3 Recovery after mis-deletion
    If a file is deleted and the commit is found to be deleted incorrectly. can also be restored,

    git log  commit号  (恢复到未删除前的commint号,此时删除的文件也恢复到磁盘上了)git checkout master  (备份好删除的文件后,再回到最新状态)
Git remote repository
  1. View Remote repositories
    1.1 Simple View-all warehouses

    (只能查看远程仓库的名字)

    1.2 See more-all warehouses

    (远程仓库的名字及git地址)

    1.3 Viewing information for a single warehouse

    git remote show [remote-name]
  2. New Remote Warehouse

    add [shortname] [url]ex. git remote add mc git://www.host.com/gitdir/mycode.git
  3. Modify Remote repositories

    git remote rename [oldnanme] [newname]
  4. Delete a remote repository

    git remote rm [remote-name]
  5. Data for remote warehouses
    5.1 Getting Data

    git fetch [remote-name] (获取仓库的所有更新,但是不自动合并当前分支)git pull (获取仓库的所有更新, 并且自动合并到当前分支)

    5.2 Uploading data

    push [remote-name] [branch-name]ex. git push origin master
git tags
  1. List labels
    1.1 See all tags

    tag

    1.2 Viewing a tag

    git show [tag-name]
  2. New label
    2.1 lightweight tag

     git tag [tag-name        

    2.2 Tagged tag

     git tag-a [tag- name]-M  "tag message"      

    2.3 Late append tag

     git log --pretty=oneline (see all the commit numbers) git tag-a [tag-name] [before the commit number] 
  3. Delete a label

    tag -d [tag-name]
  4. Submit tags to remote repositories

    git push [remote-name] --tagsex. git push origin --tags
Git branch
  1. View and Switch branches

    --merged (查看已经合并的分支)git branch --no-merged (查看还没合并的分支)git checkout [branch-name] (切换到某个分支)
  2. New Branch

    git branch [branch-name]  (新建一个分支)git branch -b [branch-name] (新建一个分支并切换到这个分支上)
  3. Merging branches

    merge [branch-name]ex. 将分支btest合并到主分支mastergit checkout mastergit merge btest

    Conflicting files are listed in merge and need to be manually merged

    After you manually resolve the conflict, use Git status again to see if there are any unmerged files.
    If there are no conflicting files, you can commit the merge by Git commit.

  4. Delete Branch

    git branch -d [branch-name]或者 git branch -D [branch-name] (强制删除某个还未合并的分支)
  5. Remote Branch related
    5.1 New Remote Branch

    1. git checkout [Local_branch] (first go to the branch you want to upload)
    2. git remote add [Remote_repo] [Remote_branch]
      (here [Remote_branch] is the name of the remote branch, which is generally the same as [Local_branch],
      [Remote_repo] is the name of the remote repository)

    3. 2 pushing data to a remote branch

      git push [Remote_repo] [Remote_branch]

    4. 3 Deleting a remote branch

      git push [Remote_repo]: [Remote_branch] (note that there is a ":" in front of the remote branch)

  6. Another way to merge branches: Yan and

Can simplify the submission record on master, making the code easy to rewind,
But in the public warehouse with a certain degree of risk.
Yan and I basically do not use, here will not repeat.

Server to create a git repository and use it as a remote repository

In fact, Git is a distributed SCM. There is no question of who is the server, who is the client, and the GIT repository on the server, referring to the Git repository as the final release version of the multi-person co-development.
This git repository is the equivalent of the one you build on GitHub, and it will put the code you've made on each computer on top of it for unified management.

  1. Server (remote git repository)

    • Generate an account for git services (typically git)

      groupadd gpxxxuseradd -m -g gpxxx gitxxx
    • Initializing a server-side git repository

      cd ~/mkdir git-repocd git-repomkdir test.gitcd test.gitgit --bare init
  2. Client (local git repository)

    • Create a new local git repository

      cd ~/gitlocalmkdir testcd testgit init
    • Initializing the Local warehouse

      READMEgit add READMEgit commit -m ‘first commit for init‘
    • Set git user information

      git config --global user.name "wangyubin"git config --global user.email "[email protected]"
    • Associate a remote Warehouse

      git remote add origin [email protected]<server address>:~/test.git/
    • Submit a local warehouse to a remote

      push origin master
Some problems with git pull when git pull, the remote file is in conflict with the local file

If the remote repository is updated by someone else, and the content of the update conflicts with the content of my own local edits. At this point, executing git pull may have the following message:

Auto-merging path/to/conflict-fileCONFLICT (content): Merge conflict in path/to/conflict-fileAutomatic merge failed; fix conflicts and then commit the result.

Use a text editor such as Vim or Emacs to edit the conflicting file Path/to/conflict-file, where there is a similar display in the conflict

<<<<<<< HEAD    App_Log.logger.debug(u‘开始时间: ‘ + utils.datetime2str(datetime.datetime.now()))    file = request.FILES.get(‘file-xxx‘)    App_Log.logger.debug(u‘结束时间: ‘ + utils.datetime2str(datetime.datetime.now()))=======    file = request.FILES.get(‘xxxx‘)>>>>>>> 3602514cc2bf1b3a64470b31ad79e07fe372add5

=====<<<<<<< HEAD on top is local content
=====The >>>>>>> 3602514cc2bf1b3a64470b31ad79e07fe372add5 below is the remote content (this commit number will be different each time)
According to the actual situation, delete the superfluous content (including ===== >>>>> <<<<<<), modify the conflict, if the local code, the following results will be:

App_Log.logger.debug(u‘开始时间: ‘ + utils.datetime2str(datetime.datetime.now()))file = request.FILES.get(‘file-xxx‘)App_Log.logger.debug(u‘结束时间: ‘ + utils.datetime2str(datetime.datetime.now()))

Then git commit-am ' submitted information ' to resolve the conflict.
Finally, you can also synchronize local modifications to a remote git repository: git push

Git pull, there are uncommitted files locally

When updating from a remote repository, if there is a file a that is not commit locally, the remote repository's a file has been modified. At this point, git pull has the following information:

6a707cc..f93575d  master     -> origin/masterUpdating 6a707cc..f93575derror: Your local changes to the following files would be overwritten by merge: apps/myapp/utils.pyPlease, commit your changes or stash them before you can merge.Aborting

At this point, if you do not want to commit the local file (which may just be a temporary modification), but it is like updating the remote repository, you can:

$ git stash    # 先将自己的改变保存起来Saved working directory and index state WIP on master: 6a707cc ...HEAD is now at 6a707cc ...$ git pull # 从远程仓库更新Updating 6a707cc..f93575d... ...$ git stash pop # 将自己的修改合并到更新后的代码中

Last step if there is a conflict, follow the steps in the previous section to resolve the conflict and modify the conflicting file with a text editor.

Conflicts when git branches merge

When you edit the same file for the branch and main branch that you are developing, you may have conflicts when you merge on the main branch.
The following constructs an example of a conflict:

$ git Branch test# Create a branch test, but not into the test branch, at this point on the master branch.$ vim xxxx# Edit an existing file on the master branch$ git commit-am' XXX message '# Commit changes to the master branch$ git checkout test# switch to test branch $ vim xxxx # before editing in The files edited on master can be edited in the same place, causing conflicts $ git commit-am  ' xxx message ' Span class= "Hljs-comment" ># Commit changes to test branch $ git checkout master  # Switch to Master branch $ git merge test # merges the test branch into the master branch, as it edits the same File, there will be conflicts auto-merging xxxxconflict (content) : merge conflict in xxxx automatic merge failed; fix conflicts and then Commit the result.             

Finally, use the text editor to modify the conflicting file, referring to the steps in the previous section to resolve the conflict.

Extracting patches from Git

There are several ways to extract patches:

$ git format-patch -1     commit 和上次 commit 之间的不同, 并生成patch文件$ git format-patch -2 # 提取本次 commit 和 上上次 commit 之间的不同, 并生成patch文件$ git format-patch commit号1 commit号2 # 提取2次commit号之间的不同, 并生成patch文件 (commit号可以通过 git log 来查看)$ git format-patch tag1 tag2 # 提取2次tag之间的不同, 并生成patch文件 (tag可以通过 git tag 来查看)
Extract the specified version of the source code via git

This feature is useful when deploying.

--format=tar --prefix="tagxx/" tagxx > ../tagxx.tar  # 获取 tagxx 的源码, 加了 --prefix 的作用是在最终的 tagxx.tar 中加了一层文件夹 tagxx

The tagxx above can also be a commit number.

git usage (GO)

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.