Must-know git basic commands and common problems

<h2>Basic pull and upload file commands</h2>

0.git pull update the file first/git pull origin dev; git status  View the addition, deletion and modification status of files
1.git clone Online Inventory Address;   git clone -b  dev(remote branch)  Link
2.git add file or folder to submit    or git add .(Indicates submitting all modified files and adding them to the temporary storage area)

Note: git add Only new or changed files are added to the index area. (deleted files will not be added)
3.git commit -m 'Remark';Submit to staging area  -m(The default is master the branch) / Skip checking commits: git commit --no-verify commit -m 'note'
3.git push ;formal submission
4.git log  View the commit log or git log --pretty=oneline View more clearly

5.  Associated remote branch:
   (1) git branch --v View associations
   (2) Associated remote branch: git branch --set-upstream-to=origin/The name of the remote branch The name of the local branch         
ps:After associating the remote library, use the command git push -u origin master first push master all contents of the branch;
or: git checkout -b local branch name origin/Remote branch name Create a local branch and automatically associate the remote branch

Thereafter, after each local commit, whenever necessary, the command can be used git push origin master Push the latest changes;

Git tracks modifications. Every modification, if you don’t use git add to the temporary storage area, it will not be added to the commit.

Use the git diff HEAD -- qq.html command to view the difference between the latest version in the workspace and the repository
<h2>Common basic commands</h2>

1. git diff Before submitting the file, you can use this command to see which files have been modified
2. git log  After uploading the file, you can view the submitted historical information 
        git log --pretty=oneline --abbrev-commit find historical commits commit id
        git log --graph View the branch merge diagram. or git log --graph --pretty=oneline --abbrev-commit
3. git reset --hard HEAD^ rollback to previous version
4. git reset --hard 09BH(The first four digits of the hexadecimal system in the historical version are sufficient, git find automatically)   back to the specified version
    if closed git window is available git reflog Command to view the versions committed between
    git rebase --onto cbe8527^ cbe8527 delete a commit op.html or git show view file content
5. git checkout -- qq.html  or git restore op.html  Bundle qq.html All modifications to the file in the workspace are undone. There are two situations here:
    (1)one is qq.html Since the modification has not been put into the temporary storage area, now, undoing the modification will return to the state exactly the same as the version library;
    (2)one is qq.htmlt After it has been added to the temporary storage area, it has been modified. Now, undoing the modification will return to the state after adding to the temporary storage area.
6. git diff HEAD -- qq.html   View the difference between the latest version in the workspace and the repository
7. git reset HEAD <file> You can undo the modification in the temporary storage area), put it back into the work area,

      git reset HEAD -- . undo all
     git reset HEAD -- filename revoke a specific goal
      git rm -cached filepath remove file from cache

8.git reflog Look at the command history to determine which future version to go back to.
9. git rm qq.html  Delete Files     git rm -r css/  delete folder
10.git remote View the information of the remote library or use git remote -v show more detailed information  

11.git push origin dev(branch name)  Push the corresponding branch
 However, it is not necessary to push the local branch to the remote, so which branches need to be pushed and which ones do not?
master The branch is the main branch, so it must be synchronized with the remote at all times;
dev The branch is a development branch, and all members of the team need to work on it, so it also needs to be synchronized with the remote;
bug Branches are only used for local fixes bug,There is no need to push it to the remote, unless the boss wants to see how many fixes you have made every week bug;
feature Whether the branch is pushed to the remote depends on whether you cooperate with your small partners to develop on it.
12. git tag v1.0(version number) Type a version number for the project(Label)
    git push origin --tags Push all local tags that have not been pushed to the remote
    git tag -d v0.9 To delete a tag, delete it locally first and then git push origin :refs/tags/v0.(delete a remote tag)
    Order git tag <tagname>Used to create a new label, the default is HEAD,You can also specify a commit id;
    Order git tag -a <tagname> -m "blablabla..."Label information can be specified;
    Order git tag Can view all tags
    13.git commit --amend Modify the comments of the last commit

<h2>Branches and basic processing</h2>

1. git checkout -b dev create dev branch, then switch to dev branch or git switch  -b dev
    -b The parameter means to create and switch
            Equivalent to these two commands:
             git branch dev
             git checkout dev
 2. git branch  view current branch;    View existing local and remote branches: git branch -a
 3. git merge dev  Merge subbranch into main branch master,After merging, view the main branch, which is the same as the content of the sub-branch
   Fast-forward information, Git Tell us that this merge is in "fast-forward mode", that is, directly master direction dev of the current commit, so the merge is very fast.
Git Heavy use of branches is encouraged:

View branch: git branch
 Create a branch: git branch <name>
switch branch: git checkout <name>or git switch <name>
create+switch branch: git checkout -b <name>or git switch -c <name>
Pull the remote branch: git fetch origin dev(dev is the branch name of the remote repository)
Merge a branch into the current branch: git merge <name>
Delete local branch: git branch -d <name>; Delete the remote branch: git push origin --delete dev

<h2>Tackling common problems</h2>
1. Does the remote warehouse still have the file after the local git rm file?

$ git add -u will only process modified or deleted files, but will not process newly created files

$ git commit -m "delete test"

$ git push

2. Handle common merge branch conflicts

Meaning on the picture:

conflict(content): merge conflict in q.html

Automatic merge failed; fix conflicts, then commit result.

Git tells us that there is a conflict in the qe.html file, and the conflict must be resolved manually before submission. git status can also tell us about conflicting files;

(1)git log --graph --pretty=oneline --abbrev-commit to see the merge of branches

(2) Manually edit the file that failed to merge with Git to the content we want, and then submit it.

(3) Delete the feature1 branch

3.git add, etc. cannot operate files?

4. During the Git operation, it suddenly displays:

Another git process semms to be running in this repository, e.g. an editor opened by 'git commit'. Please make sure all processes are terminated then try again. If it still fails, a git process remove the file manually to continue...

The translation means that git is occupied by another program, and restarting the machine cannot solve it.
The reason is that Git encountered a crash during use, and some locked resources were not released.

Solution: Enter the .git file under the project folder (display hidden folders or rm .git/index.lock) and delete the index.lock file.

  1. Error handling when git commit

    husky > pre-commit (node v12.18.3)
    ‼ Some of your tasks use `git add` command. Please remove it from the config since all modifications made by tasks will be automatically added to the git commit index.

Solution: delete or rename the .git > hooks > pre-commit file
Enter the hooks folder and find the pre-commit file, which is the root cause of the commit failure.
What this file does is:
A pre-commit (client) hook that runs a code style check before Git types a commit message.
Report an error if the code does not conform to the corresponding rules.
After we delete the file, and then commit, we found that it can be submitted successfully.

  1. git merge branch appears warning: could not open directory 'xxx': Permission denied...
    Solution: This error indicates that there is no permission to operate the file, just close the editor and code runner

<h2>Branch management strategy</h2>

Usually, when merging branches, Git will use Fast forward mode if possible, but in this mode, after deleting the branch, the branch information will be lost.

If you want to forcibly disable the Fast forward mode, Git will generate a new commit when merging, so that the branch information can be seen from the branch history.

The following shows the git merge in --no-ff mode:

 1.Create and switch branches
    git switch -c dev
 2. Modify the file and commit a new commit
    git add qe.html
    git commit -m 'change qe.html'
  3.switch back master main branch
    git switch master
  4. ready to merge dev branch, please note--no-ff parameter, which means disable Fast forward: 
      $ git merge --no-ff -m "merge with no-ff" dev This method of merging results in master Create a new commit node on the branch to complete the merge
  5.  Because this merge creates a new commit,so add-m parameter, put commit describe     write it in.
      After merging, we use git log Take a look at the branch history:
      $ git log --graph --pretty=oneline --abbrev-commit

branch strategy
In actual development, we should follow several basic principles for branch management:

First of all, the master branch should be very stable, that is, it is only used to release new versions, and it cannot be used to work on it;

Where do you work then? The work is all on the dev branch, that is to say, the dev branch is unstable. At some point, such as when version 1.0 is released, the dev branch will be merged into the master, and version 1.0 will be released on the master branch;

You and your friends are all working on the dev branch, and everyone has their own branch, just merge to the dev branch from time to time.

When merging branches, you can use the normal mode to merge by adding the --no-ff parameter. The merged history has branches, and it can be seen that the merge has been done, but the fast forward merge cannot be seen that the merge has been done.

<h2>Bug branch</h2>
In team collaboration, bugs are continuous, so each bug can be fixed through a new temporary branch, after the fix, the branch is merged, and then the temporary branch is deleted.

If there is a bug now, let's do it: .

1.git stash  Store the content of your current branch first, and continue working after restoring the site later;
2.git switch master Switch to the master branch, ready to create a bug the branch(issue-101);
3.git switch -c issue-101  Create bug the branch(issue-101);
4.  git add qe.html
    git commit -m "fix bug 101"  Revise bug and submit
5.After the repair is complete, switch to master Branch, and complete the merge, and finally delete issue-101 branch:
  git switch master;
  git merge --no-ff -m "merged bug fix 101" issue-101
  git branch -d issue-101
 6.bug fixed, now it's time to go back to dev branch work 
    git switch dev
    git stash list View previously stored content:
    stash@{0}: WIP on dev: f52c633 add merge
 7. Now you need to restore the stored content, there are 2 ways to restore:
    (1)use git stash apply recovery, but after recovery, stash content is not deleted, you need to use git stash drop to delete
    (2)use git stash pop,while recovering stash The content is also deleted
    reuse git stash list look, you don't see any stash content
    you can multiple times stash,When restoring, first use git stash list View and then restore specified stash,With the command:
    git stash apply stash@{0}
8. exist master fixed on branch bug Then, let's think about it, dev The branch was earlier from master Branched out, so, this bug In fact, at present dev also exists on the branch.

what's the matter dev Fix the same on the branch bug?Repeat the operation once, and submit it?

same bug,to be in dev To fix, we just need to put 4 c805e2 fix bug 101 The changes made by this commit are "copied" to dev branch.
Note: we only want to copy 4 c805e2 fix bug 101 The changes made by this commit do not take the entire master the branch merge come over.

For the convenience of operation, Git specifically provides a cherry-pick command that allows us to copy a specific commit to the current branch:
    git branch
* dev
$ git cherry-pick 4c805e2
[master 1d4b803] fix bug 101
 1 file changed, 1 insertion(+), 1 deletion(-)

Git automatically makes a commit to the dev branch. Note that the commit of this commit is 1d4b803, which is different from the master's 4c805e2, because the two commits only have the same changes, but they are indeed two different commits. With git cherry-pick, we don't need to manually repeat the bug-fixing process on the dev branch.

When fixing a bug, we will fix it by creating a new bug branch, then merge it, and finally delete it;

When the work at hand is not completed, first git stash the work site, then go to fix the bug, after the fix, then git stash pop, and return to the work site;

If you want to merge the bugs fixed on the master branch into the current dev branch, you can use the git cherry-pick <commit> command to "copy" the changes submitted by the bug to the current branch to avoid duplication of effort.
<h2>Feature branch</h2>

When adding a new feature, in order not to disrupt the main branch master, we create a new branch on our current branch (dev)

1. git switch -c feature-1  New branch write function
2.git add <file>
3.git commit -m 'feature-1'
4.switch back dev,ready to merge
  git switch dev
  If this function is not applicable during merging and needs to be deleted, what should I do?
  as follows:
   git branch -d feature-1
   Error: error: The branch 'feature-1' is not fully merged.
If you are sure you want to delete it, run 'git branch -D feature-vulcan'.
Destruction failed. Git friendly reminder, feature-1 The branch has not been merged. If it is deleted, the modification will be lost. If you want to delete it forcibly, you need to use uppercase-D parameter. .

Now we force delete:
git branch -D feature-1  can be successful

1. Grab the branch:
refer to

Tags: Javascript Front-end git

Posted by masson on Mon, 16 Jan 2023 06:52:23 +0300