Today, I submitted two changes to nodeclub for the first time, both of which involve the compatibility of Some browsers. The manager quickly merged my changes so that I can easily get started. In the evening, I went back to work on git's daily operations. I sorted out some git operations and wrote them here. I am happy to be open-source ~ ~
Git beginner's Guide Version rollback
- View history
git log
Orgit log --pretty=oneline
: List all submission records
commit 333123123erer123sd123123213123213123
The submitted ID is next to commit.
- Return the corresponding version
There are two methods to return the version
First:
git reset --hard HEAD^
: Previous Version
git reset --hard HEAD^^
: Previous Version
git reset --hard HEAD~66
: Last 66 versions
Second
git reset --hard commitID
Returns the ID of the corresponding version.
Commitid can be incomplete, and git will automatically complete
- Return to the previous version
Regret to return to this version.git log
Only information earlier than this version can be viewed
Okay.git reflog
Allows you to view historical operations.
Local repository Management
- Add local changes and submit
The local warehouse is divided into three regions.
Work ZoneProject catalog
Temporary Storage AreaStage of the version Library
BranchMaster and other branches
After modifying the project in the workspace, you can usegit status
View modified File Information
Put it in the stage of the temporary storage zone for usegit add somefile.cxs
Then submit it to the local branchgit commit -m "some message"
(If you make changes at work but do not add but directly commit, you will be prompted to add the information, but you can also usegit commit -am "some message"
Add a parameter.)
This time modification has been submitted to the local branch
- Local temporary storage zone management
A.Add
git add
Command to add a file to the temporary storage area. If the file is modified after an add operation, you must add the file again.
(Git manages modifications. When adding a file, the changes to the current file are not the files themselves.)
B.View
git diff HEAD -- somefile.cxs
Command to view the differences between the current workspace file and the version Library
C.Undo
# On branch master
# Your branch is ahead of ‘origin/master‘ by 2 commits.
# (use "git push" to publish your local commits)
#
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# modified: README.md
#
# Changes not staged for commit:
# (use "git add <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# modified: README.md
#
When we submit changes to the local warehouse, add changes to the temporary storage area, or usegit status
The above prompt is displayed when you view the status. The above prompt tells us three points
- There are two commit in the local branch, to be submitted as a remote warehouse, use
git push
Submit.
- There is a change in the temporary storage area of the version library. to be submitted to the local repository, use
git reset HEAD fileName
Delete modifications from the temporary storage area
- A file is modified in the workspace.
git add fileName
Submit to the temporary storage area, usegit checkout -- fileName
Restore the files in the workspace to the last submitted state. The last submission has two statuses. First, if you have used add to submit to the temporary storage area, the status will be restored to the last submitted to the temporary storage area. Second, if the temporary storage space is empty, it will be restored to the State at the last commit.
Remote Repository
- Add remote Repository
Usegit remote add <originName> <gitRepo>
Add remote repository address
Usegit push -u <originName> master
Submit the latest local modification to the remote repository.
- Clone remote repository to local
Usegit clone <gitRepo>
Clone remote repository to local
(Gitrepo addresses are divided into two types: SSH and HTTPS.)
Git collaboration and development Basic branch operations
- Create Branch
git branch dev
Create a Dev Branch
git checkout dev
Switch to Dev Branch
Two steps and one step git checkout -b dev
- Branch Switch
git branch
View the current branches
Find the branch andgit checkout <branchName>
Switch
(Note that branch switching is not allowed when you modify or submit a branch or master branch.)
Two solutions are available if switchover is not allowed.
- Solution 1: Delete the changes to the temporary storage area and restore the files modified in the workspace to the level before modification. The two commands involved here have already been discussed,
git reset HEAD fileName
Andgit checkout -- fileName
- Solution 2: Use
git stash
The command saves the current work, which is equivalent to protecting the site.
Usegit stash list
To view the stored work, usegit stash pop
You can also usegit stash apply <stashName like [email protected]{0}>
Reply to a job
- Merge Branch
The operations on Dev and other branches are the same as those on the master.git checkout master
Switch to the master branch and merge the branches. There are two ways to merge the branches.
- Method 1: Use
git merge <branchName>
If you use this method to merge and delete a branch, the branch information will be lost.
- Method 2:(Recommended)
git merge --no-ff -m "some message" <branchName>
The difference between the two methods is as follows:
Method 1:
Method 2:
- Conflict resolution during merge
When we merge branches, this prompt will appear if there is a code conflict.
Auto-merging somefile.cxs
CONFLICT (content): Merge conflict in somefile.cxs
Automatic merge failed; fix conflicts and then commit the result
This indicates that there is a code conflict in the somefile. cxs file. The conflict is identified as follows.
<<<<<<< HEAD
Creating in Master.
=======
Creating in dev.
>>>>>>> dev
Modify the code beforegit add somefile.cxs
Thengit commit -m "fixed somefile.cxs conflict"
Submit
- View Project branches and delete branches
git log --graph --pretty=oneline --abbrev-commit
Command to display the branch status of the project in a graphical manner.
git branch -d <branchName>
You can delete a branch. Sometimes it is required that the branch has not been merged into the master.git branch -D <branchName>
Command to delete Branch
- Multi-person collaboration mode
The steps for multi-person collaboration are as follows:
Usegit push origin <branchName>
If an error occurs, the code of the remote branch conflicts with you.
If the code conflicts, usegit pull origin
Capture remote warehouse merging. If you want to insure yourself, you can firstgit fetch origin master:tmp
Thengit diff master tmp
Check the differences between the remote repository and your code. Thengit merge tmp
Resolve the code conflict, submit it to the local repository, and then push it to the remote repository.
If you develop a branch of the remote repository, you need to create a branch locally. The command is as follows:git checkout -b dev origin/dev
(When cloning, you can only see the master branch, but not the other branches.)
Then usegit branch --set-upstream <branchName> origin/<branchName>
Associate the local branch with the remote warehouse branch, and then perform the two steps mentioned above