git uses common problems (commit code, merge conflicts)


Common git commands (introduction, see below for detailed parameters)

git fetch get remote branch update
git branch view local branch
git branch -r view remote branch
git branch -a view all branches (local branch and remote branch)
git clone address clone code
git checkout switch branch
git pull sync to local (pull)
git push to the server
git checkout -b dev origin/master Create a dev branch based on the main branch
git add . Add to the cache
git commit -m "commit instructions" Submit the contents of the temporary storage area to the local warehouse
git commit -a -m "commit instructions " Skip the cache area operation, directly submit the content of the workspace to the local warehouse
git status to view the current status of the warehouse

git configuration

git config –global user.name "username" # Set user name
git config –global user.email "user mailbox" #Set mailbox
git config user.name Check whether the username is configured successfully
git config user.email Check whether the mailbox is configured

Git commit code steps

git pull --> git status --> git add --> git commit --> git push (synchronization->view status->commit cache->push to git library->submit code to remote warehouse)

git pull

Before submitting the code every time, first synchronize the remote code to prevent overwriting other people's code; if there is a conflict, first back up your own code, (you can look down, code conflict merge), merge your own code into it, and then submit the code . (You can git pull or git pull origin + branch)
insert image description here
Already up-to-date appears to indicate that the local code has been updated to be consistent with the remote warehouse.

git status

git status View the current status (this step can be omitted)
The red font shows the file you modified.
insert image description here

git add

Generally, it is situation 1, submit all
**Situation 1: **If you check the current status of git status and find that all the files you have modified have to be submitted, then you can directly use git add . to put all your content added to the local git cache

**Situation 2:** If you check the current status of git status and find that there are some files that you don’t want to submit, then use git add xxx (the file link in red text in the above picture) to submit some files to the local git cache .
insert image description here

git commit

>git commit -m '备注'

insert image description here

git push

git push origin 分支名称

insert image description here

git code conflict merge problem

Your local changes to the following files would be overwritten by merge:

Approach 1: Give up native code

git reset --hard
git pull

Method 2: Merge Code

git stash  -->  git pull -->  git stash pop   (暂存->拉取->合并)

git stash : Save the current work progress, and can save all uncommitted modifications (workspace and temporary storage area) to the stack for subsequent restoration of the current working directory. You can also use git stash save, which is equivalent to git stash, the difference is that you can add some comments

git pull : Pull the server warehouse code into the local warehouse)

git stash pop : Pop the code you just stash into the local stack to the local (merge code) (you can also use git stash apply, the difference: use apply to restore, the information in the stash list will continue to be retained, and use pop to restore , the information in the stash list will be deleted.)

git stash list : After storing to the top of the local stack, you can use git stash list to view your locally stored stash logs

git stash clear : Clear the Git stack, the original stash nodes will be cleared

When merging code:
insert image description here

Common commands and detailed parameters

git add adds files to the repository:

git add file name Add a file in the workspace to the temporary storage area
git add . Add all files in the current workspace to the temporary storage area
git add -u Add all modified or deleted file information in the tracked file to the temporary storage area, do not process untracked files
git add -A Add all modified or deleted file information in tracked files to the temporary storage area, including untracked files
git add -i Enter the interactive interface mode, add files to the cache area as needed
# Submit the files in the temporary storage area to the local warehouse:
git commit -m "commit instructions" Submit the contents of the temporary storage area to the local warehouse
git commit -a -m "submit instructions" Skip the operation of the cache area and directly submit the contents of the work area to local warehouse

git diff Compare file similarities and differences

git diff The difference between the work area and the temporary storage area
git diff branch name The difference between the work area and a certain branch, the remote branch is written like this: remotes/origin/branch name
git diff HEAD The difference between the work area and the HEAD pointer
git diff commit id file Path The difference between the current version and the historical version of a file in the
work area git diff –stage The difference between the work area file and the last submission (use –cached before version 1.6)
git diff version TAG View all changes since a certain version
git diff branch A Branch B compares the difference between branch A and branch B (also supports comparing two TAGs)
git diff branch A...branch B compares the changes of the two branches after they are separated
In addition: if you only want to count which files have been changed and how many lines have been changed , you can add the –stat parameter

git log view history

git log View all commit records (SHA-A checksum, author name, email address, submission time, submission description)
git log -p -number of times to view the number of recent submission records
git log –stat Briefly display the content changes of each submission
git log –name-only displays only the list of modified files
git log –name-status displays the list of newly added, modified and deleted files
git log –oneline lets the submission record output in a streamlined line
git log –graph –all –online graph Display the merge history of the branch
git log --author=Author query the author's submission record (add a --all-match parameter when using grep at the same time)
git log --grep=filter information List the submission information that contains the filter information in the submission record
git log -S query content is similar to –grep, there is no space between S and query content
git log fileName View the modification record of a file

git reset code rollback

git reset HEAD^ Revert to the last submitted version
git reset HEAD^^ Revert to the last submitted version, that is, multiple ^, and so on or use ~ times
git reflog
git reset –hard version number
–soft: just change The HEAD pointer points, the buffer area and the work area remain unchanged;
–mixed: modify the HEAD pointer, the content of the temporary storage area is lost, and the work area remains unchanged;
–hard: the HEAD pointer is modified, the content of the temporary storage area is lost, and the work area restores the previous state ;

Repository related operations

Delete the repository file: git rm file name The version
in the repository replaces the version in the workspace: git checkout — test.txt

Operations related to remote warehouse

Synchronize the remote warehouse: git push -u origin master
Push the content of the local warehouse to the remote warehouse: git remote add origin [email protected]: account name/warehouse name. git
Clone the project from the remote warehouse to the local: git clone [email protected] :git account name/warehouse name.git
View remote library information: git remote
pulls remote branches to local warehouse:

git checkout -b local branch remote branch # will create a new branch locally, and automatically switch to this branch
git fetch origin remote branch: local branch # will create a new branch locally, but will not automatically switch, and checkout
git branch –set- upstream local branch remote branch# Establish a link between the local branch and the remote branch
Synchronize the remote warehouse update:: git fetch origin master

Branch related operations

create branch

Create a branch: git checkout -b dev -b means to create and switch branches. The
above command is equivalent to the following two:
git branch dev create branch
git checkout dev switch branch

View branch: git branch

Merge branches: git merge

git merge dev #Used to merge the specified branch (dev) into the current branch
git merge –no-ff -m “merge with no-ff” dev #Add –no-ff parameter to merge in normal mode, merged history There is a branch, it can be seen that a merge has been done

For example : there are two branches in the project: test, test2
execute git merge origin/test2 in the test branch (merge the test2 branch into the test branch)

Delete branch: git branch -d dev

View the branch merge graph: git log –graph –pretty=oneline –abbrev-commit

undo a commit

git revert HEAD # Undo the latest commit
git revert version number # Undo a commit

git username and password related configuration

The first thing to do after installing Git is to set user information (global can be replaced by local to take effect in a separate project):
git config –global user.name “username” # set username
git config –global user.email “ User mailbox" #Set mailbox
git config user.name to check whether the user name is configured successfully
git config user.email to check whether the mailbox is configured

Guess you like

Origin blog.csdn.net/qq_43940789/article/details/130100265
Recommended