Git version Management

Source: Internet
Author: User

This article is for individuals to organize notes, refer to the official Git tutorial with Liaoche teacher:

In the previous Git introduction, we learned about the commands and principles of some local git operations, and in this section we'll learn about git remote administration and versioning.

Remote Warehouse

If you want to manage our code on GitHub and modify commits locally, you need to know about the Git remote repository. GitHub is a code hosting platform that git officially provides, and individuals can apply for free, but the content they upload can be easily shared, and if you want to use a private warehouse, you'll need to pay for it.

To add SSH KEY on GitHub:

After registering your account on GitHub, add the native SSH public key so that you do not have to use your account password frequently when working with remote repositories locally.

Ssh-keygen-t rsa-c "[Email protected]"

After the default carriage return, cat. Ssh/, and then add the cat content to the account's SSH key.

Create your first personal warehouse

First register a personal account on GitHub and enter the name of the new warehouse directly in new repository. Add a personal repository locally based on the prompt to use SSH:

git remote add origin [email Protected]:yourname/gitrepo.gitgit push-u Origin Master #-U update all branches origin default remote warehouse name

Once all the local files have been synchronized for the first time, a commit to the file can be used directly:

Git push Origin Master

This pushes the locally updated files directly to the remote management library.

Cloning from a remote repository

There are a lot of great code and apps on GitHub, and if we use someone else's code, we can just pull the code to our local location with the git clone command, and if it's your own code base you can pull it directly:

git clone [email protected]:yourname/gitrepo.git

Branch Management

In the process of development, in many cases we need to manage the branch. For example, if a file is modified locally, then this file is a linear modification, if you want to make multiple changes to several files, and different changes will eventually confirm a final version, the final merge of this branch is the final version of this file, it should be noted that only when the commit is executed command, the local Master branch is established.

To create and switch branches:

git branche dev #创建dev分支git checkout dev #切换分支到dev ===============git checkout-b Dev # Create and switch branches, one command is done

To view branches:

Git branch

After the branch has been modified git add, git commit can switch to merge on the master branch.

Merge dev to Master branch:

git merge Dev

To delete a branch:

git branch-d Dev

When files are modified at the same time on two branches and submitted, conflicting errors occur when Git merges. This is because when we merge, the program does not know which one we need to update, which requires us to manually update the file to resolve the conflict. and then merge.

[[email protected] gitrepo]# git merge test # After both the master and test branches are commit, then the merge will error auto-merging readme.txtconflict (content ): Merge conflict in readme.txtautomatic merge failed; Fix conflicts and then commits the result. [[email protected] gitrepo]# cat readme.txt test<<<<<<< HEAD #查看编辑的文件, the system has made a note of the places we have manually modified. t His was Master =======this is test >>>>>>> test

Once the file is modified, git add and git commit can resolve the conflict again:

[[email protected] gitrepo]# git log--graph--pretty=oneline--abbrev-commit* 52ed4df confilt|\ | * 310f7e7 IT # shows a branch modification * | a8fa78b master|/* b040742 test

with git log--graph command to see the branching merge diagram.

when merging branches, the default is to use Fast forward mode, but using this mode does not record the modification log of other branches, in the actual application, in order to better understand the branch of the modification time, you need to add --no-ff parameters can be combined in normal mode, the merged history has branches, can be seen to have been merged, and Fast Forward The merger would not have been seen as a merger.

During the general development process, soybeans will have the following processes:

1. Leader Create 2 branches in the remote repository: Master and Dev

2, Zhang San and John Doe cloning remote repositories to local

3, Zhang San and John Doe all switch to Dev branch

4. Zhang San create branch Z3, John Doe create branch L4

5. After the development is complete, Zhang San merges Z3 to Dev, John Doe merges L4 to Dev

6. Zhang San and John Doe push the dev branch of the local library to the remote dev

7. Leader pull the Dev and masterd in the remote library, merge Dev to master locally and push to remote master

Bug Branch Management

If you are currently developing on the Dev branch and suddenly have a bug on the line that needs to be repaired immediately, I need to temporarily hide the current work, which is to save the progress git statsh on the current dev branch:

[[email protected] gitrepo]# git stashsaved working directory and index state WIP on dev:f241242 Testhead Test[[email protected] gitrepo]# git status #隐藏工作区之后, the working directory shown is empty # on branch devnothing to commit, working directory CLE An

After saving the current work, we are going to fix the bug on the line, switch to the master branch, and create a repaired issue branch:

[[email protected] gitrepo]# git checkout masterswitched to branch ' master ' [[email protected] gitrepo]# git checkout-b is Sueswitched to a new branch ' issue ' [[email protected] gitrepo]# git branch dev* issue Master

After the repair work is done on issue, execute git add, git commit to commit the code, and then merge the code on the issue branch on master to remove the issue branch:

[[email protected] gitrepo]# git branch  dev* issue  master[[ email protected] gitrepo]# git add readme.txt [[email protected]  gitrepo]# git commit -m  "Fix issue" [issue 8b29da7] fix issue 1  file changed, 1 insertion (+)  [[email protected] gitrepo]# git  checkout master #  back to master merge issue Branch switched to branch  ' master ' [[email  protected] gitrepo]# git merge --no-ff -m  "Fix bug issue"  issue  #  Record Branch log information merge made by the  ' recursive '  strategy. readme.txt |  1 + 1 file changed, 1 insertion (+)  [[email protected]  gitrepo]# git branch -d issue   #删除issue分支Deleted  branch issue  ( WAS&NBSP;8B29DA7).

After the bug fix is complete, we're going back to our dev branch to continue our work:

[[email protected] gitrepo]# git checkout devswitched to branch  ' Dev ' [ [email protected] gitrepo]# git status #  The original Dev branch is empty # on branch  devnothing to commit, working directory clean[[email protected] gitrepo]#  git stash list #  view our hidden branches [Email protected]{0}: wip on dev:  f241242 test[[email protected] gitrepo]# git stash pop #  Show hidden branches and remove hidden branches # on branch dev# changes not staged for commit:#     (use  "Git add <file&gt ..."  to update what will  be committed) #    (use  "GIT&NBSP;CHECKOUT&NBSP;--&NBSP;&LT;FILE&GT; ..."  to  Discard changes in working directory) ##       modified:    file.txt#no changes added to commit  (use  "Git add"  and/or  "Git commit  -a ") dropped refs/[email protected]{0}  (5a7f46b8a24f1a557a37b0378ee75c65387e024a) [email  protected] gitrepo]# git status# on branch dev# changes not  staged for commit:#    (use  "GIT&NBSP;ADD&NBSP;&LT;FILE&GT; ..."  to  update what will be committed) #    (use  "git checkout - - <file&gt  to discard changes in working directory) ##        modified:   file.txt#no changes added to  commit  (use  "Git add"  and/or  "Git commit -a")

There are two ways to recover a hidden branch:

git stash Apply # recover hidden branches

git stash Drop # Delete hidden branch records


git statsh Pop # restores the hidden branches and removes the hidden branches, using a command to do the above two commands.


If you want to develop a new feature, it's best to create a new branch, and if you need to delete one of the new branches you've developed (not committed at this time) delete an uncommitted branch and use git branch-d branchname to force the deletion.

Multi-person Collaboration Branch management

In the multi-person collaborative development team, because everyone will continue to modify files, merging files, will appear when you want to submit their own code, it happens that others have modified the same file, so that your local files and remote file content is not the same, you need to manually resolve the conflict before committing.

[[email protected] gitrepo]# git remote # View remote branch Origin[[email protected] gitrepo]# git remote-v # View Remote Branch details origin [EMA Il protected]:andyskyl/gitrepo.git (FETCH) origin [email protected]:andyskyl/gitrepo.git (push)

Commit is a conflict:

[[email protected] gitrepo]# git add readme.txt [[email protected]  gitrepo]# git commit -m  "Dev2" [Dev 41ad4f8] dev2 1 file changed,  1 insertion (+),  3 deletions (-)  [[email protected] gitrepo]# git  push origin dev   #  Push Dev branch conflict ▽! [rejected]         dev -> dev  (Fetch first) error: failed to push  some refs to  ' [Email protected]:andyskyl/gitrepo.git ' hint: updates were  Rejected because the remote contains work that you dohint: not  have locally. This is usually caused by another repository  Pushinghint: to the same ref. you may want to first merge  the remote changes  (e.g.,hint:  ' git pull ')  before pushing again.hint: See the  ' Note about fast-forwards '  in  ' git push --help '  for details. [[email protected] gitrepo]# git pull   #  use Git pullremote as prompted:  Counting objects: 3, done.remote: Compressing objects: 100%  (2/2),  done.remote: Total 3  (delta 0), reused 3  (delta 0),  pack-reused 0unpacking objects: 100%  (3/3),  done. From * [new branch]      dev         -> origin/devThere is no tracking  Information for the current branch. Please specify which branch you want to merge with. See git-pull (1)  for details    git pull <remote> <branch>      if you wish to set tracking information for this branch  you can do so with:    git branch --set-upstream-to= origin/<branch> dev   #  here has given hints     [[email protected]  gitrepo]# git branch --set-upstream-to=origin/dev dev branch dev set  up to track remote branch dev from origin. [[email protected] gitrepo]# git pull   #执行此命令之后再按照之前的方式修改文件, conflict resolution Auto-merging  readme.txtCONFLICT  (content):  merge conflict in readme.txtautomatic merge  failed; fix conflicts and then commit the result.

Post Conflict resolution submission:

[[email protected] gitrepo]# git add readme.txt [[email protected] gitrepo]# git commit-m "fix m" [Dev 3267ad5] Fix M[[ema Il protected] gitrepo]# git push origin devcounting objects:10, done.compressing objects:100% (4/4), done. Writing objects:100% (6/6), 570 bytes | 0 bytes/s, done. Total 6 (Delta 0), reused 0 (Delta 0) 17a9b60. 3267ad5 Dev-Dev

push branches locally, using git push Origin branch-name , if the push fails, first use the git Pull fetch new commits from remote;

To create a local branch that corresponds to a remote branch, use the

git checkout-b branch-name Origin/branch-name # The names of local and remote branches are best consistent;

To establish the association of local and remote branches, using:

Git branch--set-upstream-to=origin/dev Dev

Label Management

    • The tag can be the identity of each commit, similar to adding an alias to each commit:

[[email protected] gitrepo]# git branch dev* master[[email protected] gitrepo]# git tag v1.0[[email protected] gitrepo]# Git tagv1.0

The default tag is on the most recently committed commit.

    • If you want to tag the previous commit with a commit ID, then you can:

[[email protected] gitrepo]# git log--pretty=oneline--abbrev-commite2ce160 fix bug issue8b29da7 fix issue881136a fix bug Example
[[email protected] gitrepo]# git tag v0.9 881136a #根据log The corresponding commit tag, you can use-m[[email protected] gitrepo]# git tag #添加说明v0.9 # The arrangement of Git tags is by default in alphabetical order of label names v1.0
    • Use tag to view specific information:

[[email protected] gitrepo]# git show v1.0commit e2ce160ad30d5433c033d9be7dc5dfe23ddbfd6dmerge:881136a 8b29da7author: Trying <[email protected]>date:wed Dec 16:16:32 +0800 fix bug issue
    • You can also add a description when you create a new tag:

git tag-a v2.0-m "version 2.0 released" #-a specify tag name-m Add description
    • Delete a label

# git tag-d v2.0deleted tag ' v2.0 ' (was 959f8b1)
    • Push tags to remote

      Because tags added locally are not automatically pushed to remote, if you need to push local tags to remote, use GIT push origin tagname:

# Git push Origin v1.0 # pushes the specified tag
# Git push Origin--tags # push all tags at once
    • Delete a remote label

Deleting a remote tag requires deleting the local label:

# git tagv0.9v1.0# git tag-d v0.9 # Delete local tagdeleted tag ' v0.9 ' (was 881136a) # git push origin:refs/tags/v0.9 # Remove remote tag Sign-[deleted] v0.9

git push origin:refs/tags/tagname

Git Highlights font color:

# git config--global color.ui true

Build a git server

If you don't want to use GitHub's paid warehouse, you can build a private git server for internal use.

To install Git:

Yum Install Git-y

To create a git user:

Useradd git

Prevents git users from logging into the shell, editing the/etc/passwd file, and changing the default shell of the git user to:


Create a directory for the Git repository:


Initialize the GIT repository:

Git init--bare test.git

In the/home/git user's directory, create the key authentication file Authorized_keys, and import the local public key into the Authorized_keys file on the server:

Cat >/home/git/.ssh/authorized_keys

Then go back to the local, you can get the working directory on the GIT server:

[email protected] ~]# git clone [email protected]:/gitrepo/test.gitcloning into ' test ' ... warning:you appear to has Clon Ed an empty repository.

This can be done just like on GitHub.

This article is from the "Trying" blog, make sure to keep this source

Git version Management

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: 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.