The source code for this blog is available on GitHub.
Note
Top

Case Study - How I Tackle Version Record with Git

Cover Image for Case Study - How I Tackle Version Record with Git
Han Chen
Han Chen

Merge branch and missing files occurred

My branch names to demo, the name itself is too crappy originally.

I change the name from master to branch-will-be-merged, and change other to branch-destination

These 2 branches has pushed to remote before, so basically i do is to delete the remote branches in the first place, and rename the local branches

git branch -a | grep git/
git push origin --delete git/master
git push origin --delete git/other
git branch -m git/master git/branch-will-be-merged
git branch -m git/other git/branch-destination

after everything was set. i want to figure it out the actual reason for merging branch with missing files

typing branches i want to invertigate again

git branch -a | grep git/    

git/branch-destination
git/branch-will-be-merged
git/cherry-pick

In this case study, we've already pinpoint to the CheckFilter.tsx and CheckFilterNon.tsxfiles. Now we know that someone create those 2 files on their develop process, and it's supposed to be in the branch if i merge the main or master files into my branch

now i want to moniter the target files. We can ls the files with keyword in this case

git checkout git/branch-will-be-merged

# incorrect
ls -l components/icons Check*

# correct
ls -l components/icons/Check*

-rw-r--r--  1 chenhanting  staff  621 Mar  9 14:04 components/icons/CheckCircle.tsx
-rw-r--r--  1 chenhanting  staff  587 Mar 13 09:54 components/icons/CheckFilter.tsx
-rw-r--r--  1 chenhanting  staff  387 Mar 13 09:54 components/icons/CheckFilterNon.tsx
-rw-r--r--  1 chenhanting  staff  511 Mar 13 09:54 components/icons/Checkmark.tsx

when we checkout to git/branch-destination, we are supposed not to have new files

git checkout git/branch-destination

ls -l components/icons/Check*  
-rw-r--r--  1 chenhanting  staff  621 Mar  9 14:04 components/icons/CheckCircle.tsx
-rw-r--r--  1 chenhanting  staff  509 Mar 13 10:12 components/icons/Checkmark.tsx

now i want to merge git/branch-will-be-merged to git/branch-destination, and those 2 files should also be there

git checkout git/branch-destination
git merge git/branch-will-be-merged

But nope. Why?

To recap what's happening so far

img

The possible issues are listed as following

There could be a few reasons why the CheckFilter.tsx and CheckFilterNon.tsx files were not merged into the git/branch-destination branch even though they were present in the git/branch-will-be-merged branch. Here are some possibilities:

  1. The files were not staged or committed before the merge. Make sure that the changes made to those files were properly committed to the git/branch-will-be-merged branch before attempting to merge it.
  2. There were conflicts during the merge process. If there were changes made to the same lines of code in the same files in both the git/branch-will-be-merged and git/branch-destination branches, there will be merge conflicts that need to be resolved manually.
  3. The files were excluded from the merge. It's possible that the CheckFilter.tsx and CheckFilterNon.tsx files were explicitly excluded from the merge process using git's file exclusion mechanisms such as .gitignore or .gitattributes files.

To investigate further, you can use the git log command to check the commit history of both branches and see if the changes made to the CheckFilter.tsx and CheckFilterNon.tsx files were properly committed to the git/branch-will-be-merged branch. You can also use the git diff command to compare the contents of those files between the two branches to see if there are any differences or conflicts that need to be resolved.

git/branch-will-be-merged involves CheckFilter.tsx and CheckFilterNon.tsx files, and there isn't no conflict occurred in this case. Probably list 1 and 2 acn be excluded. During the process of elimination, we can dive into list 3 unless there are some possible occurrences

if there is deleted by mistake during the merge process how can i prevent form

If you want to prevent accidentally deleting files during the merge process, you can use the --no-ff (no fast-forward) option when merging. This option will always create a merge commit, even if the changes can be merged automatically.

Here's an example of how to use the --no-ff option:

git checkout branch-to-merge-into
git merge --no-ff branch-to-merge-from

This will create a merge commit with a message like "Merge branch 'branch-to-merge-from' into 'branch-to-merge-into'", and it will include all the changes from the branch-to-merge-from branch.

By creating a merge commit, you can always go back to this commit and recover the deleted files if necessary. Additionally, it provides a clear record of when the merge occurred and which changes were included in the merge.

Here is a interlude and remimnder

if the timeline of git/branch-destinationis rear the git/branch-will-be-merged, espicially thegit/branch-destination has already merged git/branch-destination , you won't able to merge again. Git will show Everything is up to date and skip everything. Ther is no way out

git checkout git/branch-will-be-merged
ls components/icons/Check*

CheckCircle.tsx CheckFilter.tsx CheckFilterNon.tsx Checkmark.tsx

i reset git/branch-destination before the branch has been merged without --no-ff flag and it amazingly back to the normal process. CheckFilter.tsx and CheckFilterNon.tsx are remaining

The following is what i did

img

When I rollback to the commit before merging and merge again. Thing goes well. Nothing wierd happened

it’s the complex process to traced back what was happening lastweek

Observe and make it a move

According to the github record, the author is proceed to finish off the task and keep track of the latest change

img

if that is the case, what i only need to do is to create a new branch, cherry-pick 3 commits on that branch, and finally check things goes well. That's it.

Delete multiple branches at the same time

git branch -a | grep -E '^\s*[0-9]+'
git branch -a | grep -E '^\s*[0-9]+' | xargs git branch -D

This is a command-line instruction that involves the use of Git and a Unix utility called grep. Here's what each part of the command does:

  • git branch -a: This command lists all of the branches in the Git repository, including both local branches and remote branches (i.e., branches on the remote server that you've fetched or cloned).
  • |: This is called a "pipe" and it redirects the output of the previous command (i.e., the list of all branches) to the next command (grep).
  • grep: This is a Unix utility that searches for a specified pattern in a text file (or, in this case, the output of the previous command).
  • -E: This option tells grep to use "extended regular expressions", which allows us to use special characters like + and ^.
  • ': This is a delimiter that indicates the start of the pattern that we want to search for.
  • ^\s*: This is the pattern we're searching for. The ^ character means "start of line", so we're looking for lines that start with whitespace (i.e., spaces or tabs). The \s* part means "zero or more whitespace characters". (I think we don't need to add it here though)
  • [0-9]+: This is the rest of the pattern we're searching for. It means "one or more digits".

So, putting it all together, this command lists all of the branches in the Git repository, and then searches for any branch names that start with whitespace and contain one or more digits. The output will be a list of branch names that match this pattern.

Can you give me more example of ^\s*

The ^\s* pattern is a regular expression that matches any line that starts with zero or more whitespace characters. Here are a few examples of how this pattern can be used:

  • ^\s*Hello: This pattern matches any line that starts with zero or more whitespace characters, followed by the word "Hello". For example, it would match lines like "Hello, how are you?" and " Hello world".
  • ^\s*\d+\.: This pattern matches any line that starts with zero or more whitespace characters, followed by one or more digits, followed by a period. This is a common pattern for matching numbered lists or steps. For example, it would match lines like "1. First step" and " 42. The answer to everything."
  • ^\s*(TODO|FIXME): This pattern matches any line that starts with zero or more whitespace characters, followed by either the word "TODO" or "FIXME". This is a common pattern used in code comments to indicate tasks that need to be completed or issues that need to be fixed. For example, it would match lines like "TODO: refactor this code" and " FIXME: handle edge case".
  • xargs git branch -D: This takes the output of the previous command and passes it as arguments to the git branch -D command, which deletes the specified branches.

img

The following is the binary search in Vercel, and that's the function micmicing the git

img

© 2024 WOOTHINK. All Rights Reserved.
Site MapTerms and ConditionsPrivacy PolicyCookie Policy