Common GIT Commands

Source: Internet
Author: User
Tags git commands

GIT's general operations

Regular operation is also a few of my own usual commands, learned from pro git this book

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

    2. To view the configuration method

      git config --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

    git clone 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

    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 Result of Append file 1-revert to original state (change to Unstaged)

    git rm --cache test.c

    5.2 Result of Append file 2-commit to Git repository (change to commited)

    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.

    reset test.c  (状态变回modified)

    6.2.2 commit the staged changes to the Git repository

    commit -m "my message"

    6.2 Modifying the results of a file 2

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

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

    7.1.1 Remove test.c from the Git repository

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

    7.1.2 deleted the wrong, restore the operation just now

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

    7.2 Delete files from the Git repository only

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

    7.2.1 Remove test.c from the Git repository

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

    7.2.2 deleted the wrong, restore the operation just now

    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

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

    1.2 See more-all warehouses

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

    1.3 Viewing information for a single warehouse

    show [remote-name]
  2. New Remote Warehouse

    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

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

    git tag

    1.2 Viewing a tag

    show [tag-name]
  2. New label
    2.1 Lightweight Tag

    git tag [tag-name]

    2.2 Tagged Tag

    -a [tag-name] -m "tag message"

    2.3 Late Append Tag

    --pretty=oneline (查看所有的commit号)git tag -a [tag-name] [commit号前几位即可]
  3. Delete a label

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

    --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] or git branch- D [Branch-name] (forcibly deleting a branch that has not yet been merged)    
  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)

    5.2 Pushing data to a remote branch

    git push [remote_repo] [remote_branch]

    5.3 Deleting a remote branch

    git push [remote_repo] :[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

      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:

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 # Creates a branch test, but does not enter the test branch and is still on the master branch. $ vim XXXX # Edit an existing file on the master branch $ gitCommit-am' xxx message ' # submitChange of Master branch $ git checkoutTest # Switch toTest Branch $ vim xxxx # before editing inedit files on master, can edit the same place, cause conflicts $ git commit-am ' xxx message ' # Commit Test Branch modification $ git checkout Master # Toggle To Master Branch $ git merge Test # merges the test branch into the Master branch, where the same file is edited, creating a conflict auto-merging Xxxxconfli CT (content): merge conflict in xxxxAutomatic merge failed, fix conflicts and then comm it 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 # extract this timeCommit and LastCommit differences, and generatepatch file $ git format-patch -2 # Extracts the difference between this commit and last commit, and generates the Span class= "Hljs-keyword" >patch file $ git format-patch commit 1 commit number 2 # extract  2 times commit numbers and generate patch file (commit number can be viewed via git log) $ git Span class= "Hljs-keyword" >format-patch tag1 Tag2 # extract  2 times the difference between tags and generate patch file (tag can be viewed via git tag)       
Extract the specified version of the source code via git

This feature is useful when deploying.

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

The tagxx above can also be a commit number.

Common GIT 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.