Version control management tools are indispensable in team development. Since the company’s project has been changed from SVN to Git recently, this is mainly a review and summary. The main differences between SVN and Git are as follows: SVN is centralized, while Git is distributed. Git has the advantage of being easy to add branches and distributed features locally, and can be submitted offline, which solves problems that SVN cannot solve, such as collaborative development between different teams.

One of the most central concepts in Git is workflow.

  • A Workspace is the actual directory on your computer.
  • A staging area (Index) is similar to a cache area that holds your changes temporarily.
  • A Repository is divided into local and remote repositories.

Quick query of common commands

Here’s a sneak peek (from the Internet) :

Initialize the

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

#Create a new directory and initialize it as a Git code base
$ git init [project-name]

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

configuration

#Listing All Configurations
$ git config -l

#Configure an alias for a command
$ git config --global alias.co checkout
$ git config --global alias.ci commit
$ git config --global alias.st status
$ git config --global alias.br branch

#Sets the user information when submitting code
$ git config [--global | --local] user.name "[name]"
$ git config [--global | --local] user.email "[email address]"
Copy the code

Add/delete/modify files

#Adds the specified file to the staging area
$ git add [file1] [file2] ...

#Adds the specified directory to the staging area, including subdirectories
$ git add [dir]

#Adds all files in the current directory to the staging area
$ git add .

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

#Stop tracking pointing files, but do not delete them
$ git rm --cached [file]

#Rename the file and place it in temporary storage
$ git mv [old] [new]
Copy the code

submit

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

#Submit documents designated for the staging area to the warehouse area
$ git commit [file1] [file2] ... -m [message]

#The commit workspace has changed since the last commit and goes directly to the warehouse
$ git commit -a

#Display all diff information when committing
$ git commit -v

#Use a new COMMIT instead of the previous commit. If the code does not change, the commit information from the last COMMIT is overwritten
$ git commit --amend -m [message]

#Redo the last COMMIT and include new changes to the specified file
$ git commit --amend [file1] [file2]
Copy the code

branch

#Displays all local branches
$ git branch

#List all remote branches
$ git branch -r

#Lists all local and remote branches
$ git branch -a

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

#Create a new branch to establish a trace relationship with the specified remote branch
$ git branch --track [branch] [remote-branch]

#Delete the branch
$ git branch -d [branch-name]

#Deleting a Remote Branch
$ git push origin --delete [branch-name]
$ git branch -dr [remote/branch]

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

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

#Switch to the previous branch
$ git checkout -

#Establish a trace relationship between an existing branch and a specified remote branch
$ git branch --set-upstream [branch] [remote-branch]

#Merges the specified branch into the current branch
$ git merge [branch]

#Derivatives specify a branch to the current branch
$ git rebase [branch]

#Merge a COMMIT into the current branch
$ git cherry-pick [commit]
Copy the code

The label

#Lists all local labels
$ git tag

#Create a label based on the latest submission
$ git tag [tag]

#Remove the label
$ git tag -d [tag]

#Deleting a Remote Label
$ git push origin :refs/tags/[tag]

#Viewing Label Information
$ git show [tag]

#Submit the specified label
$ git push [remote] [tag]

#Submit all labels
$ git push [remote] --tags

#Create a new branch that points to a tag
$ git checkout -b [branch] [tag]
Copy the code

Check the information

#According to the state
$ git status

#Displays the version history of the current branch
$ git log

#Displays the commit history and the files that changed each commit
$ git log --stat

#Search submission history by keyword
$ git log -S [keyword]

#Displays the version history of a file, rename the package file
$ git log --follow [file]
$ git whatchanged [file]

#Displays each diff associated with the specified file
$ git log -p [file]

#Displays the last five commits
$ git log -5 --pretty --oneline

#Displays all submitted users, sorted by number of submissions
$ git shortlog -sn

#Show who modified the specified file and when
$ git blame [file]

#View someone's submission record
$ git log --author=[username] 

#Display staging and workspace differences
$ git diff

#Shows the difference between the staging area and the last COMMIT
$ git diff --cached [file] 

#Displays the difference between the workspace and the latest COMMIT for the current branch
$ git diff HEAD

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

#Displays the files that have changed during a commit
$ git show --name-only [commit]

#Displays the contents of a commit, a file
$ git show [commit]:[file]

#Displays the most recent commits for the current branch
$ git reflog
Copy the code

Remote operation

#Download all changes to the remote repository
$ git fetch [remote]

#Retrieve the remote repository changes and merge them with the local branch
$ git pull [remote] [branch]

#Retrieves the changes from the remote repository and merges them with the local branch base
$ git pull --rebase [remote] [branch]

#Display all remote warehouses
$ git remote -v

#Displays information about a remote repository
$ git remote show [remote]

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

#Upload the local specified branch to the remote repository
$ git push [remote] [branch]

#Push current branch to remote repository forcibly, even if there are conflicts
$ git push [remote] --force

#Push all branches to the remote repository
$ git push [remote] -all
Copy the code

undo

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

#Restores all files in the current directory of the staging area to the workspace
$ git checkout .

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

#Resets the specified file in the staging area, same as the last commit, but with the same workspace
$ git reset [file]

#Reset the staging area and workspace as the last COMMIT
$ git reset --hard

#Reset the pointer to the current branch to specify COMMIT, and reset the staging area, but the workspace remains the same
$ git reset [commit]

#Reset the HEAD of the current branch to specify a COMMIT, and reset the staging area and workspace to match the specified COMMIT
$ git reset --hard [commit]

#Undo changes to all uncommitted files in the working directory
$ git reset --hard HEAD

#Create a COMMIT that cancels the specified COMMIT and applies all changes to the current branch
$ git revert [commit]

#Place uncommitted changes in a storage area
$ git stash

#Restores the contents of the repository to the current workspace
$ git stash pop
Copy the code

Configuring Multiple Accounts

Sometimes we have our own Github account for personal use and the company team uses another GitLab account. In this case, we need to configure multiple accounts for the same device.

1. Generate an SSH key

Generate the corresponding key for Github and Gitlab respectively (by default, the locally generated key is located at /Users/ username /.ssh/), and configure git to access different keys when accessing different hosts. The flow is as follows:

  1. Used in gitbashSsh-keygen -t rsa-c "company email address"Generate the corresponding GitLab key:id_rsaid_rsa.pub.
  2. Will gitlab public key namelyid_rsa.pubIs configured to the company’s GitLab
  3. Used in gitbashSSH -keygen -t rsa -c "github email address "-f ~/. SSH /id_rsa.githubGenerate the corresponding Github key:id_rsa.githubid_rsa.github.pub
  4. The Github public key is/id_rsa.github.pubIs configured to its own Github
  5. Go to the location where the key was generated and create oneconfigFile to add configuration:
Host gitlab                            # Alias of the domain address
    HostName gitlab.com                # this is the real domain address
    User gitlab                        Configure the user name
    IdentityFile ~/.ssh/id_rsa.github  # here is the address of id_rsa
    
Host github
    HostName github.com
    User github
    IdentityFile ~/.ssh/id_rsa
Copy the code

Second, the test

Run SSH -t git@hostName to test the connection between gitlab and Github using sshkey at /Users/ username /.ssh/ :

ssh -T git@gitlab
# prompt if the configuration is correct
Welcome to GitLab, @gitlab! 

ssh -T git@gihub
# prompt if the configuration is correct
Hi github! You've successfully authenticated, but GitHub does not provide shell access.
Copy the code

Configure git repository

Git config file records the basic information of the user, and our account information is also in it. Here we need to configure different user information in different local repositories to access different remote repositories

The config file usually has three locations:

  • System (system-level) is located in git’s installation directory under Windows and contains values that are applicable to all users and libraries on your system. If you pass the –system option to Git config, it will explicitly read and write the file.

  • Global (user level) is located in ~/.gitconfig, specific to your user. You can make Git read or write this particular file by passing in the –global option.

  • Local (repository level) is located at.git/config. Whatever library you are currently using, specific references to that single library take precedence.

User Level Configuration

Because the company’s code is used frequently, set the global (user level) of git configuration file to the company’s Gitlab account:

$ git config --global user.name "gitlab"$git config --global user.email"[email protected]"// Company email addressCopy the code

Warehouse level configuration

Set local (repository level) to github account. Git repository = git repository = git repository

$ git config --local user.name "github"$git config -- $git config --local user.email "[email protected]"// Github email addressCopy the code

Four, the use of

# Previous method: single account
git clone [email protected]:xxxx/xxx.git # default config when configured
git clone git@github:xxxx/xxx.git #config is equivalent to the first statement

Git clone (" git ")
Use the alias of the domain address
git clone git@github:xxxx/xxx.git
git clone git@gitlab:xxxx/xxx.git
Copy the code