Git Introduction

Posted by ppera on Tue, 14 May 2019 14:21:53 +0200

Git

git core concepts and introductory details

What is git?

Git is a distributed version control software, originally created by Linus Torvaz and released in GPL in 2005. The original goal was to better manage Linux kernel development. It should be noted that this is different from GNU Interactive Tools, a file manager similar to the Norton Commander interface. The initial driving force for git development came from Bit Keeper and Monotone. Git was originally developed as a back end that can be wrapped by other front-ends (such as Cogito or Stgit), but the GIT kernel has matured to be used independently for version control. Many well-known software uses git for version control, including the development process of Linux kernel, X.Org server and OLPC kernel.

Version Control System

Git is the best distributed version control system in the world. Version control system is a system that records changes in a series of files over time so that you can return to a certain version of the system later. Version control system is divided into three categories: local version control system, centralized version control system and distributed version control system.

Local Version Control Systems (LVCS) stores all versions of files on local disks in a certain data format (some VCS are change patches for saving files, i.e. calculating the difference when the contents of files change to save them). This method solves the problem of manual copy and paste to a certain extent, but it can not solve the problem of multi-person cooperation.

Centralized Version Control Systems has no essential change compared with local version control, but only one more central server. The data warehouse of each version is stored in the central server. Administrators can control the rights of developers, and developers can pull data from the central server. Although centralized version control solves the problem of teamwork, its shortcomings are also obvious: all data are stored in the central server, once the server is down or the disk is damaged, it will cause immeasurable losses.

Distributed Version Control System is different from the former two. First of all, in distributed version control systems, such as Git, Mercurial, Bazaar and Darcs, the system saves not the difference of file changes, but the snapshot of the file, that is, the whole file is copied and saved, without concern for the specific content of changes. Secondly, the most important thing is that the distributed version control system is distributed. When you copy the code from the central server, you copy a complete version library, including historical records, submission records, etc., so that even if a machine goes down, you can find a complete backup of the file.

Relationships among Git Workspace, Temporary Zone and Version Library

Working Directory

The working directory is the content extracted independently from a version of the project. These files are extracted from the Git repository's compressed database and placed on disk for your use or modification.

Staging Area

The temporary area is a file that holds the list of files that will be submitted next time, usually in the Git warehouse directory. Sometimes it is also called `index', but in general it is called a temporary area.

Repository

The Git repository directory is where Git stores metadata and object databases for projects. This is the most important part of Git, where data is copied when cloning warehouses from other computers.

Git workflow

1. Modify files in the working directory.
2. Store the file temporarily, and put the snapshot of the file into the temporary area. 3. Submit updates, find files in temporary areas, and store snapshots permanently in the Git warehouse directory.

If a particular version of the file is stored in the Git directory, it belongs to the submitted state. If it has been modified and put into the temporary area, it belongs to the temporary state. If a modification has been made since the last removal but has not been placed in the temporary area, it is the modified state. In the Git Foundation chapter, you'll learn more about the details of these states, how to follow up on file status, and how to skip temporary direct submission.

Git Common Commands Detailed

1.Git file operation

$ git help [command]                  # Display command help
$ git show [$id]                      # Display the content of a submission 
$ git checkout  [file]                # Discard workspace modifications
$ git checkout .                      # Discard workspace modifications
$ git add [file]                      # Submit working document modification to local temporary storage area
$ git add .                           # Submit all modified working documents to the temporary storage area
$ git reset [file]                    # Restore from Temporary Zone to Working Document
$ git reset -- .                      # Restore from Temporary Zone to Working Document
$ git reset --mixed HEAD~1            # Modify Version Library, Modify Temporary Zone, Reserve Workspace
$ git reset --soft HEAD~2             # Modify version libraries, save temporary areas, and save workspaces
$ git reset --hard HEAD~3             # Modify version libraries, temporary areas, and workspaces
$ git commit --amend                  # Modify the last submission record
$ git revert [$id]                    # Restore the status of a submission, and the restore action itself creates a submission object
$ git revert HEAD                     # Restore the status of the last submission
$ git status                          # View the current workspace status 
$ git config --list                   # Look at all users
$ git ls-files                        # View files that have been submitted
$ git commit -a                       # Submit all changes to the current repos
$ git commit -v                       # Parameter - v allows you to see commit differences
$ git commit -m "message"             # Submit to local and add submission information
$ git commit -am "init"               # Add and submit 

$ git log Common Options 

-p Display the difference between each update in patch format. 
--stat Display file modification statistics for each update. 
--shortstat Display only --stat The last row number modification adds removal statistics. 
--name-only Display the list of modified files only after submitting information. 
--name-status Displays a list of new, modified and deleted files. 
--abbrev-commit Display only SHA-1 The first few characters, not all 40 characters. 
--relative-date Shorter relative time display (e.g., 2) weeks ago").  
--graph display ASCII History of branch merging in graphical representation. 
--pretty Use other formats to display historical submissions

$ git rm --cached [file]              # Delete files from the temporary area
$ git rm -f [file]                    # Force Removal of Version-Controlled Files
$ git rm -r --cached [file]           # Recursive deletion - r, useful for folders

$ git diff [file]                     # Compare the differences between current and temporary files
$ git diff [$id] [$id]                # Compare the differences between two submissions
$ git diff [branch1]..[branch2]       # Comparison between two branches
$ git diff --staged                   # Comparing Temporary Zone and Version Library Differences
$ git diff --cached                   # Compare temporary and workspace differences
$ git diff --stat                     # Simply compare statistics

$ git stash                           # Provision of ongoing work
$ git stash push                      # push temporarily into a temporary space
$ git stash list                      # View all cached code
$ git stash clear                     # Empty the cache contents
$ git stash drop                      # Remove a storage
$ git stash save                      # Specify message
$ git stash show                      # View the diff for the specified stash
$ git stash apply stash@{1}           # Remove the specified cache code
$ git stash pop                       # pop files from temporary space
$ git fetch origin dev:dev            # Getting the latest version locally will not automatically merge
$ git pull origin master:master       # Getting the latest version to local will automatically merge

Git branch operation related commands

$ git branch                          # View branch
$ git branch [dev] [master]           # Create dev branch in master
$ git branch -v                       # View the final submission information for each branch
$ git branch -r                       # View remote branch information
$ git branch -a                       # View all branch information
$ git branch -m [aaa] [bbb]           # Rename aaa bbb
$ git branch [name]                   # Create a branch from the current branch
$ git branch -d [branch]              # Delete a branch
$ git branch -D [branch]              # Force deletion of a branch
$ git branch --set-upstream-to origin/test master # Local branch and remote Branch Association
$ git branch --set-upstream-to=origin/master help #
$ git branch --merged                 # View branches that have been merged into the current branch
$ git branch --no-merged              # View branches that have not yet been merged into the current branch

$ git commit [$id] -b [new_branch]    # Create a branch from a history submission record
$ git commit [$id]                    # Cheout from the history submission record, but no branch information, switch to other branches will automatically delete
$ git checkout [name]                 # Switching branches
$ git checkout --track origin/dev     # Switch to remote dev branch
$ git checkout -b [name]              # New from the current branch and switch to name
$ git checkout -b [new_br] [br]       # Creating a new_branch based on branch
$ git merge origin/dev                # Merge branch dev with current branch
$ git merge [branch]                  # Merge branch branches to the current branch
$ git merge origin/master --no-ff     # Do not merge Fast-Foward to generate merge submissions
$ git rebase master [branch]          # To rebase master to branch is equivalent to: git clone [branch] & git rebase Master & git clone Master & git merge [branch]

Git Remote Branch Management

$ git pull                              # Grab all branch updates of remote warehouse and merge them locally
$ git pull --no-ff                      # Grab all branch updates of remote warehouse and merge them locally. Do not merge them fast.
$ git fetch origin                      # Grab Remote Warehouse Updates
$ git merge origin/master               # Merge remote primary branch to local current branch
$ git clone --track origin/branch       # Tracking a remote branch to create a local branch
$ git clone -b [l_b] origin/[r_b]       # Create local branches based on remote branches, with the same function
$ git push                              # All branches of push
$ git push origin master                # Push local specified branch to remote main branch
$ git push -u origin master             # Push the local main branch to remote (if no remote main branch is created to initialize the remote warehouse)
$ git clone [git@xxx.git]               # Cloning remote warehouse
$ git remote -v                         # View Remote Server Address and Warehouse Name
$ git remote show [name]                # View the status of remote server repository
$ git remote add [name] [url]           # Add remote warehouse address
$ git remote rm [repository]            # Delete remote warehouse
$ git remote set-url --push [name] [newUrl]# Modify remote warehouse
$ git pull [rName] [lName]              # Pull out remote branches
$ git push [rName] [lName]              # Push remote branch
$ git push origin [lname]:[rname]       # Local branch push to remote
$ git push origin -d [name]             # Delete remote branch - d can also be used -- delete
$ git remote set-head origin master     # Setting up the HEAD of the remote warehouse to point to the master branch
$ git branch --set-upstream master origin/master

Git Version Back Operations Related Commands

HEAD : current version
HEAD^ : Last version
$ git log                               # View commit information
$ git log --pretty=oneline              # Single-line display of historical records
$ git log --oneline                     # Simple single-line display
$ git reflog                            # View command history 
$ git checkout .                        # Undo all local change codes
$ git checkout [file]                   # Undo all local change codes
$ git reset HEAD .                      # Revoke all add files 
$ git reset HEAD [file]                 # Undo a single add file
$ git reset --soft HEAD                 # Return only commit information and retain the modified code
$ git reset --hard HEAD^                # Return to the last commit version completely without reserving the modified code
$ git revert                            # id of previous commit
$ git reset --hard [branch]             # Return local code to align with git remote warehouse
--hard Parameters discard modifications to the current workspace
--soft Parameters are returned to the previous version, but changes to the current workspace are retained and can be resubmitted

Git tag operation related commands

$ git tag                               # View tab
$ git tag [name]                        # Create version
$ git tag -d [name]                     # Delete version
$ git tag -r                            # View remote versions
$ git push origin [name]                # Create a remote version (local version push to remote)
$ git push origin :refs/tags/[name]     # Delete remote versions
$ git pull origin --tags                # Merge tag s from remote warehouses to local locations
$ git push origin --tags                # Upload local tag to remote warehouse
$ git tag -a [name] -m [message]        # Create annotated Tags

Git Submodule Related Operational Commands

git submodule add [url] [path] # add sub-module
 git submodule init # initialization sub-module, run only once when the warehouse is checked out for the first time
 git submodule update # Update sub-module needs to run every time it updates or switches branches
 Delete sub-modules: 4 steps
1) $ git rm --cached [path]
2) Edit the'.gitmodules'file and delete the related configuration nodes of the sub-module.
3) Edit the'.git/config'file and delete the relevant configuration nodes of the sub-module.
4) Manual deletion of directories remaining in sub-modules

Git Patch Management

git diff ] ../sync.patch                # Generate patches
git apply ../sync.patch                 # Patch up
git apply --check ../sync.patch         #Test whether the patch is successful

Git ignores some files and folders that are not submitted

Create a file named ". gitignore" in the warehouse root directory and write to unnecessary folder names or files. Each element takes up one line, such as

target
*.class

Common commands summarized in the work will continue to be supplemented in the future. If there are omissions, you are welcome to supplement.

Topics: Programming git Linux snapshot Database