Distributed code management tool Git

Distributed code management tool Git

The conclusions of others are others, and you must have your own experience after studying.

Git is a distributed version control system, regardless of the code can control other types of files can be versioned, too, is very convenient situation than collaboration. A bit similar to the current WPS online document , but WPS is real-time, and Git is equivalent to finishing offline and uploading online, achieving the same effect.

The so-called distributed management means that the code management does not depend on the central server, and each terminal can be a code server, which greatly reduces the risk of code loss.

1. Git installation and configuration

If you want to do well, you must first sharpen your tools. Compared with the next step of the windows series, let's focus on the Git installation and configuration under the Ubuntu environment. [ GitHub example ]

1. Git installation

##  
sudo apt-get install git
 

2. Git repository configuration

#  repository 
mkdir MySpace
cd MySpace
touch test.txt
touch test.doc


#  git 
git init
ls -ah
#  stage
git add test.txt test.doc  #   git add .
#  -m  
git commit -m "add test.txt file"
#  git 
git commit -a "add test.txt file"


#  GitHub ssh-keygen
cd ~/.ssh
ssh-keygen
cat ~/.ssh/id_rsa.pub


#  GitHub 
# GitHub https://github.com/new 
#  
git remote add origin git@github.com:Crisimple/test_repository.git
#  
git push -u origin master  #  -u


#  
git clone git@github.com:crisimple/test_repository.git
git clone https://github.com/crisimple/test_repository.git      #  
 

2. regret medicine

[ GitHub example ]

1. Version rollback

# 1. 
# 1.1.  
git log
#  
git log --pretty=oneline
#  
git reflog
# -----------------------------------------------------------------------
# 1.2.  
#  
git reset --hard HEAD^
#  
git reset --hard commit_id
#  
git reset --soft HEAD@{1}
#  
git reset --hard HEAD@{1}  #   git reset HEAD~2
 

2. Work area

# 2.1  
git status
# 2.2  
git checkout -- test.txt
# 2.3  
git rm test.txt
 

3. branch management

In a multi-person collaborative project, in order to avoid conflicts between the code developed by yourself and others, pull a branch.

Create a branch of your own, invisible to others, and continue to work normally on the original branch, and work on your own branch, submit first and then submit, until the development is completed, then merge to the original branch at one time , So that it is safe and does not affect the work of others.

1. Create and merge branches

HEAD points to master (that is, the current branch), and master points to commit.

#  dev dev 
git checkout -b dev

git branch dev
git checkout dev

#   *  
git branch

#  master 
git checkout master

#  dev master , git merge  
git merge dev

#  dev dev 
git branch -d dev

# git  switch
git switch master
#  
git switch -c dev

 

Because creating, merging, and deleting branches is very fast, Git encourages you to use branches to complete a task, and then delete the branches after merging. This has the same effect as working directly on the master branch, but the process is safer.

2. BUG branch

In software development, bugs are just like commonplace. Everyone can be repaired by a new temporary branch, merge the branch after repair, and then delete the temporary branch.

#  
git status
#  dev bug dev 
git stash
git status

#  bug master master 
git checkout master
git checkout -b issue-101

#  bug
git add modify_bug.py
git commit -m "fix issue 101"

#  bug master bug issue-101 
git checkout master
git merge --no-ff -m "merge bug fix 101" issue-101

#   dev  
git checkout dev
git status
#  
git stash list
#  stash 
# git stash apply stash git stash drop 
git stash apply stash@{0}
git stash drop
#  git stash pop  stash 
git stash pop

# cherry-pick 
git cherry-pick commit_id
 

3. Feature branch

When adding a new feature, in order not to mess up the main branch. If a new feature is not added, it is best to create a new feature branch, and after the above development is completed, merge, and finally delete the feature branch.

#  feature 
git checkout -b feature-vulcan

#  feature 
git add feature_test.py
git commit -m "add feature vulann"

#  dev 
git status
git checkout dev 
git merge feature-vulcan
#  
git branch -D feature-vulcan
 

4. Multi-person collaboration

Take the common development and merger of code pulled from GitHub as an example.

#  
git push origin dev

#  
git pull git@github.com:crisimple/crisimple.git

#  dev origin dev 
git checkout -b dev origin/dev

#  git remote -v 

#  

#  git push origin branch-name git pull 

#  git checkout -b branch-name origin/branch-name 

#  git branch --set-upstream branch-name origin/branch-name 

#  git pull 
 

5. Resolve conflicts

4. label management

When a version is released, a tag is usually placed in the repository first, so that the version at the time of tagging is uniquely determined. In the future, whenever you take a version of a label, just take out the historical version of that label. So the tag is also a snapshot of the repository, in fact it is a pointer to commit.

Git has commit, why do we need to introduce tags? To simplify the memory, the commit corresponding to the id is too long, it is easy to remember with tag v1.1 and has a meaningful name.

1. Create a label

#  
git branch
git checkout master

#  
git tag v1.0
#  
git tag
#  commit id 
git log --pretty=oneline --abbrev-commit
git tag v1.1 f53c633
#  
git show v1.1
#  -a  -m  
git tag -a v1.2 -m "parameter introduction add"
 

2. Operation label

#  
git tag -d v1.1
#  
git push origin v1.0
#  
git push origin --tags
#  
git push origin :refs/tags/v1.2
 

5. custom Git

1. Ignore special files

Create a special .gitignore file in the root directory of the git workspace, and then fill in the file names to be ignored, and git will automatically ignore these files.

echo " " >> .gitignoer

#  
git check-ignore -v app.py

#  
git add -f app.py
 

2. Configure alias

To simplify git related commands, you can give the original name order an alias.

git config --global alias st status

#  
git config --global alias last 'log -i'

#  config 
#  
cat .git/config
#  
cat .gitconfig
 

6. build a Git server

Build your own git code management warehouse.

#  git
sudo apt-get install git

#  git git 
sudo adduser git

#  
 id_rsa.pub/home/git/.ssh/authorized_keys 

#  
sudo git init --bare sample.git

#  Git .git owner git
sudo chown -R git:git sample.git

#   shell  
git:x:1001:1001:,,,:/home/git:/usr/bin/git-shell

#  
git clone git@server:/srv/sample.git