Compared with GIT and SVN, this article is easy to understand


Welcome to Tencent cloud + community to get more Tencent technology practice dry goods~

This article was published by Tencent gongfeng in the column of cloud + community


Starting from the comparison between GIT and SVN, this paper introduces how to start using git through git SVN, and summarizes the common commands of GIT used in high frequency.

I. git vs SVN

Git and SVN are better than others. Everyone has different experiences.

Git is distributed and SVN is centralized

This is the biggest difference between GIT and SVN. If we can master this concept, we can basically understand the difference between the two. Because Git is distributed, GIT supports offline work and can perform many operations locally, including the branch function to be launched in the future. Svn must be connected to the Internet to work properly.

Git has many complex concepts and SVN is easy to use

All developers who master GIT and SVN at the same time have to admit that git has too many commands to master in daily workadd,commit,status,fetch,push,rebaseIf you want to master it, you must also master itrebaseandmergeThe difference between them.fetchandpullBesides, there are alsocherry-picksubmodulestashAnd other functions, just these nouns are very convoluted.

In terms of ease of use, SVN is much better, easy to use and friendly to novices. But on the other hand, more git commands mean more functions. If we can master most of GIT functions and realize the mystery, we will find that we will never go back to the era of SVN.

Git branch is cheap, SVN branch is expensive

In version management, branching is a very common function. Before the release, branches need to be released for large demand development, feature branches are needed, large teams also have development branches, stable branches, etc. In the process of large team development, there is often the need to create and switch branches.

Git branch is a pointer to a commit, while SVN branch is the directory of the copy. This feature makes git branch switching very fast, and the creation cost is very low.

And git has a local branch, SVN has no local branch. In the actual development process, we often encounter some unfinished code, but need to deal with other problems urgently. If we use git, we can create a local branch to store the unfinished code, and then return to the local branch to finish the code after the problem is solved.

II. Git core concept

One of the core concepts of Git is workflow.

  • Workspace is the actual directory in your computer.
  • The index is similar to the cache area, which temporarily saves your changes.
  • The repository is divided into local warehouse and remote warehouse.

When switching from SVN to git, the most difficult and incomprehensible are staging area and local warehouse. After using git skillfully, you will find that it’s almost divine design. Because of the existence of both, many works are easy to manage.

Generally, the code submission is divided into several steps:

  1. git addCommit from workspace to staging area
  2. git commitSubmit from staging area to local warehouse
  3. git pushorgit svn dcommitSubmit from local warehouse to remote warehouse

In general, remember the following commands to do your daily work (the picture comes from the network):

Compared with GIT and SVN, this article is easy to understand[git command]

III. git SVN common commands

This command is for developers who use git SVN, please master it.

If the server uses SVN, but the local wants to experience git’s local branch, offline operation and other functions, you can useGit-SVNFunction.

Common operations are as follows (pictures are from the network):

Compared with GIT and SVN, this article is easy to understand[ Git-SVN ]

#Download an SVN project and its entire code history and initialize it as a git code base
$ git svn clone -s [repository]

#View the current version Library
$ git svn info

#Retrieve changes from all branches of the remote warehouse
$ git svn fetch

#Retrieve the changes of the current branch of the remote warehouse and merge it with the local branch
$ git svn rebase 

#Upload the local warehouse of the current branch to the remote warehouse
$ git svn dcommit

#Pull new branch and submit to remote warehouse
$ svn copy [remote_branch] [new_remote_branch] -m [message]

#Create local branch corresponding to remote branch
$ git checkout -b [local_branch] [remote_branch]

IV. initialization

Starting from this section, the following commands apply to GIT andGit-SVN

#Create a git code base in the current directory
$ git init

#Download a project and its entire code history [git only]
$ git clone [url]

Five, configuration

#List all configurations
$ git config -l

#Configure aliases for commands
$ git config --global checkout
$ git config --global commit
$ git config --global status
$ git config --global branch

#Set user information when submitting code
$ git config [--global] "[name]"
$ git config [--global] "[email address]"

Git user’s profile is located in~/.gitconfig

The configuration file for git’s single repository is located at~/$PROJECT_PATH/.git/config

Vi. addition and deletion of documents

#Add all files from the current directory to the staging area
$ git add .

#Add specified file to staging area
$ git add <file1> <file2> ...

#Add the specified directory to the staging area, including its subdirectories
$ git add <dir>

#Delete the workspace file and put this deletion in the staging area
$ git rm [file1] [file2] ...

#Stops tracking the specified file, but it remains in the workspace
$ git rm --cached [file]

#Rename the file and put it in the staging area
$ git mv [file-original] [file-renamed]

Add the filename file1 to the. Gitignore file, and git will stop tracking the status of file1.

Seven, branch

#List all local branches
$ git branch

#List all local branches和远程分支
$ git branch -a

#Create a new branch, but still stay in the current branch
$ git branch [branch-name]

#Create a new branch and switch to it
$ git checkout -b [new_branch] [remote-branch]

#Switch to the specified branch and update the workspace
$ git checkout [branch-name]

#Merge the specified branch to the current branch
$ git merge [branch]

#Select a commit to merge into the current branch
$ git cherry-pick [commit]

#Delete local branch, - D parameter forces branch deletion
$ git branch -d [branch-name]

#Delete remote branch
$ git push [remote] :[remote-branch]

Eight, submit

#Submit staging area to warehouse area
$ git commit -m [message]

#Submit workspace and staging area changes directly to the warehouse area
$ git commit -a

#Show all diff information on submit
$ git commit -v

#Submit staging area to warehouse area, merge to last modification, and modify last submission information
$ git commit --amend -m [message]

#Upload local designated branch to remote warehouse
$ git push [remote] [remote-branch]

Nine, pull out

#Download all changes to the remote warehouse (GIT only)
$ git fetch [remote]

#Show all remote warehouses (GIT only)
$ git remote -v

#Display the information of a remote warehouse (GIT only)
$ git remote show [remote]

#Add a new remote warehouse and name it git only
$ git remote add [remote-name] [url]

#Retrieve the changes from the remote warehouse and merge with the local branch (GIT only). If using git SVN, please refer to section 3
$ git pull [remote] [branch]

#Retrieve the changes of the remote warehouse and merge with the local branch (GIT only). If git SVN is used, please refer to section 3
$ git pull --rebase [remote] [branch]

Ten, revocation

#Recover the specified files from the staging area to the workspace
$ git checkout [file]

#Recover all files from the current directory of the staging area to the workspace
$ git checkout .

#Restore workspace to specified commit
$ git checkout [commit]

#Resets the specified file for the staging area, consistent with the last commit, but the workspace does not change
$ git reset [file]

#Reset staging area and workspace to match last commit
$ git reset --hard

#Resets the pointer of the current branch to the specified commit, and resets the staging area, but the workspace does not change
$ git reset [commit]

#Reset the head of the current branch to the specified commit, and reset the staging area and workspace at the same time, consistent with the specified commit
$ git reset --hard [commit]

#Create a new commit to undo the specified commit
$ git revert [commit]

#Place uncommitted changes in storage
$ git stash

#Restore the contents of the storage area to the current workspace
$ git stash pop

Xi. Inquiry

#View workspace file modification status
$ git status               

#View the specific content of workspace file modification   
$ git diff [file]

#View modified contents of staging area file
$ git diff --cached [file] 

#View version library modification records
$ git log                  

#View someone's submission
$ git log --author=someone 

#View the history and specific modification content of a file
$ git log -p [file]        

#View the specific modification content of a submission
$ git show [commit]

XII. Others

Written in the back

From SVN to git, in addition to the basic concepts and common commands listed in this article, including but not limited toHow to switch from SVN server to git serverBranch model managementAnd so on. This article is limited in length, but not introduced to the very important knowledge points will be listed in the reference materials, hoping to be an extension of this article.

Related reading
[daily course recommendation] machine learning practice! Quick introduction to online advertising business and CTR knowledge

This article has been released by Tencent cloud + community authorized by the author. Please click

Search focus on the public number “cloud plus community”, get the technical dry goods for the first time, pay attention to reply 1024 to send you a technical course gift!

A great deal of practical experience of technology is in Yunjia community!