This is the 20th day of my participation in the November Gwen Challenge. Check out the details: The last Gwen Challenge 2021

🤞 personal home page: @Qingcheng sequence member Stone 🤞 fan benefits: plus one to one fan group to answer questions, get free rich resume template, improve learning materials, do a good job in the new era of volume king!

Share some solutions to specific problems that need to be solved in the process of managing code with Git in production. Some of them have been tested in production and development environment and can achieve results.

Error: branch code submitted to Master branch

Problem: I need to commit to a new branch, but commit to Master incorrectly. The solution

Create a new branch under master, do not switch to new branch,master:

(master)$ git branch my-branch
Copy the code

Reset the main branch to the previous commit:

(master)$ git reset --hard HEAD^
Copy the code

HEAD^ is short for HEAD^1, which you can further reset by specifying the HEAD to set.

Or, if you don’t want to use HEAD^, find the commit hash you want to reset to (git log can do that) and reset to that hash. Synchronize content to remote using Git push.

For example, the submitted hash that the master branch wants to reset to is a13b85e:

(master)$ git reset --hard a13b85e
HEAD is now at a13b85e
Copy the code

Checkout the newly created branch to continue working:

(main)$ git checkout my-branch
Copy the code

2. Accidentally deleting branches

Problem: I accidentally deleted one of my branches and want to recover the data of the branch. Solution: If you push to remote on a regular basis, it should be safe in most cases, but sometimes it is possible to delete branches that have not yet been pushed to remote. Let’s create a branch and a new file:

(main)$ git checkout -b my-branch
(my-branch)$ git branch
(my-branch)$ touch foo.txt
(my-branch)$ ls
README.md foo.txt
Copy the code

Add the file and make a commit

(my-branch)$ git add .
(my-branch)$ git commit -m 'foo.txt added'
(my-branch)$ foo.txt added
 1 files changed, 1 insertions(+)
 create mode 100644 foo.txt
(my-branch)$ git log
​
commit 4e3cd85a670ced7cc17a2b5d8d3d809ac88d5012
Author: siemiatj <[email protected]>
Date:   Wed Jul 30 00:34:10 2014 +0200
​
    foo.txt added
​
commit 69204cdf0acbab201619d95ad8295928e7f411d5
Author: Kate Hudson <[email protected]>
Date:   Tue Jul 29 13:14:46 2014 -0400
​
    Fixes #6: Force pushing after amending commits
Copy the code

Now let’s go back to the main branch and ‘accidentally’ delete the my-branch

(my-branch)$ git checkout main
Switched to branch 'main'
Your branch is up-to-date with 'origin/main'.
(main)$ git branch -D my-branch
Deleted branch my-branch (was 4e3cd85).
(main)$ echo oh noes, deleted my branch!
oh noes, deleted my branch!
Copy the code

At this point you’ll remember Reflog, an updated version of the log that stores the history of all the actions in the repO.

(main)$ git reflog
69204cd HEAD@{0}: checkout: moving from my-branch to main
4e3cd85 HEAD@{1}: commit: foo.txt added
69204cd HEAD@{2}: checkout: moving from main to my-branch
Copy the code

As you can see, we have a commit hash from the deleted branch, and let’s see if we can restore the deleted branch.

(main)$ git checkout -b my-branch-help
Switched to a new branch 'my-branch-help'
(my-branch-help)$ git reset --hard 4e3cd85
HEAD is now at 4e3cd85 foo.txt added
(my-branch-help)$ ls
README.md foo.txt
Copy the code

Look! We were able to retrieve the deleted files. Git’s Reflog is just as useful if rebasing goes wrong.

3. Explicitly want to delete a branch

Problem: Explicitly wants to remove a useless branch. The solution

Delete a remote branch:

(main)$ git push origin --delete my-branch
Copy the code

You can also:

(main)$ git push origin :my-branch
Copy the code

Delete a local branch:

(main)$ git branch -D my-branch
Copy the code

Boy, haven’t you seen enough? Click on the details of the stone, casually have a look, maybe there is a surprise? Welcome to support the likes/attention/comments, your support is my biggest motivation, thank you!