What is Git?
Version control tool: used to manage our modification history of files and directories in the development process, convenient to view historical records, for easy viewing.
The main difference between Git and SVN
The SVN is a centralized version control tool, and the version library is centralized on the central server. The first thing to do is download it from a central server.
Git is a distributed version control management system. There is no central server. Everyone’s computer is a complete version library, and you only need to submit your own changes.
advantages
- Suitable for distributed development with emphasis on individuals.
- Public server pressure and data volume are not too high.
- Fast and flexible.
- Conflicts between any two developers can be easily resolved.
- Work offline.
Git Theoretical Basis
The working area
- Working Directory -Working Directory
- The staging area – Stage
- Repository – the Repository
- Remote Git repository -Remote Directory
The working process
1. Add and modify files in the working directory;
2. Put the files that need to be versioned into the temporary storage area;
3. Commit the files in the staging area to git repository.
Git operation
To manage project root execution
Create a working directory with common instructions
Get Git repository
Create a new warehouse
git init // Create a new Git code base in the current directory
Copy the code
Clone remote repository
$git clone [url]Copy the code
git pull & git fetch
Git fetch pulls the latest content from the remote host to the local branch, which the user checks and decides whether to merge to the local branch
Git pull pulls down and merges the latest content of the remote host directly. git pull = git fetch + git merge
-
git fetch
-
Git fetch Remote host address Git fetch Remote host address branch // FETCH_HEAD // Check the latest host status based on the FETCH_HEAD. - Determine whether to update git merge FETCH_HEAD // Merge the latest drop into the codeCopy the code
-
-
git pull
-
Git pull Remote host name Branch nameCopy the code
-
Submit the commit
GIT file manipulation
When Git commits, the main concern is whether the file has changed between two versions. The way to determine whether the file has changed as a whole is to calculate the checksum of the file using sha-1.
Four states of a file
- Untracked: in a folder but not added to a Git library, not involved in version control — Git add.
- Unmodify: The file is stored but not modified.
- No other operations have been performed, Git Add has entered Staged, and Git checkout has been discarded
- Staged: Staged, execute git commit to synchronize changes into the library.
Viewing file Status
git status
Copy the code
Add files and directories
Add files to the staging area
Git add. // all [filename]Copy the code
Example Remove files and directories
git rm --cached <file>
Copy the code
View the differences between modified files
git diff [files]
Copy the code
Git diff --cached git diff --cachedCopy the code
Ignore files
.gitignore
A set of rules
Submit the commit
Commit files from the staging area to the local repository
$git commit [file1] [file2] $git commit [file2] $git commit -v $git commit -v $git commit -v $git commit -- amend-m [message] # amend the last commit, $git commit --amend [file1] [file2]...Copy the code
View the Commit Log
git log
View logs of all branches
“Git reflog” records all updates to all branches of the repository, including those that have been undone.
Viewing the File List
git ls-files
Undo the local warehouse update
git reset –hard
A rollback operation
Git reverses the rollback operation.
- In the workspace
Git checkout -- git checkout --Copy the code
- (Add, not commit)
Git reset HEAD. // All git reset HEAD filesCopy the code
-
(Committed, not pushed)
Git reset --head <commitID>// go back to the last commitCopy the code
-
(push)
git revert head
git push origin master
git reset --hard HEAD^
git push origin master -f
Copy the code
revert & reset
- Revert: Drops a specified change, but generates a new commit
- Reset: Points the head pointer to the specified commit. It will not be found in history.
Reset has three modes: Hard Mixed soft
Hard: Both native source and native uncommitted source fall back to a version
–soft: retain the source code, commit the information is rolled back to a certain version, and does not change locally
— Mixed: Keep the source code, but revert git commit and index information to a version.
Git branch
$git branch -r $git branch -r $git branch [branch-name] $git checkout -b [branch] Commit $git branch [branch] [commit] $git branch --track [branch] [remote-branch] $git checkout [branch-name] $git checkout - $git branch --set-upstream [branch] [remote-branch] $git merge [branch] $git cherry-pick [commit] $git branch -d [branch-name] # $git push origin --delete [branch-name] $ git branch -dr [remote/branch]Copy the code
Git merges branch code into the master branch
Git pull: Get code remotely and merge local versions
First switch to the current branch
git checkout noworigin
Copy the code
Use Git pull to pull down the branch code
git pull
Copy the code
Switch to the main branch
git checkout master
Copy the code
Merge the branch code into the main branch
git merge noworigin
Copy the code
Push up
git push
Copy the code
Tips: Other code merges the same way!