Is there a way to get all conflicting code snippets (not just files) after a git merge? - linux

Once I execute the git merge A B command, I can get the list of conflicting files using linux command like
git diff --name-only --diff-filter=U
But, is there a way to get conflicting code snippets along with file names? I want to create a report with just the conflicting code snippets.
For example, I need a report like this
src/com/xyz/ABC.java
<<<<<<< Branch_A
this.codeFromA();
=======
this.codeFromB();
>>>>>>> Branch_B
..and the same section repeats for all conflicting files.
Note that there are other lines in the files. But, only the conflicting code piece is returned.

Just run git diff.
Here is explained how it works:
Since Git stages any merge results that are successful, when you run git diff while in a conflicted merge state, you only get what is currently still in conflict

If you are using eclipse, you can get this information from Git staging view.

Related

List LFS tracked files in pre-receive hook

I am trying to write a git pre-receive hook that rejects LFS files larger than a certain size (among other things).
I am trying to execute git lfs ls-files -l -s <new-ref-value> in my script, but it returns
2ec20be70bb1be824e124a61eabac40405d60de62c76d263eff9923f18c098ed - binary.dll (63 B)
Could not scan for Git LFS tree: missing object: a405ce05ac78ea1b820d036676831a474ddf8f90
I cannot even ignore the error message because it stops after the first file.
I guess that the problem has to do with the fact that the commits have not been "validated" on the remote yet. The frustrating thing is that the information that I need (new file paths + sizes) is accessible since it's printed for the first file.
Is there a way to run the git lfs ls-files command with the new ref value successfully at this stage?
Can I obtain the list of the added file paths and sizes in any other way?
EDIT: If that's relevant in any way, the Git server is a GitLab instance in its default configuration.

git sparse-checkout ignore specific file type

I have a git repository with a bunch of large csv in them, which I don't want to clone, so I came across git sparse-checkout and this post: https://github.blog/2020-01-17-bring-your-monorepo-down-to-size-with-sparse-checkout/
From this post I took following:
git clone --no-checkout https://github.com/john_doe/repo-with-big-csv.git
cd repo-with-big-csv
git sparse-checkout init --cone
Then I edit the .git/info/sparse-checkout and add the following (adapted from example in page above):
/*
!**/*.csv
But it doesn't seem to work properly. After git pull some folders are cloned, some are not. I also noticed a warning, when I do git sparse-checkout list I get:
warning: unrecognized pattern: '**/*.csv'
warning: disabling cone pattern matching
/*
!**/*.csv
What's the proper way to ignore a certain file type only?
See "Git sparse checkout with exclusion" and make sure to use Git 2.26.x, which has some fixes for the git sparse-checkout command.
When in cone mode, the git sparse-checkout set subcommand takes a list of directories instead of a list of sparse-checkout patterns
If core.sparseCheckoutCone=true, then Git will parse the sparse-checkout file expecting patterns of these types. Git will warn if the patterns do not match.
You need to only use restrict patterns based on folder prefix matches.
The OP Frode Akselsen adds in the comments:
my example is actually working: the folders that don't show up just contain only .csv files, hence, after applying the rules in .git/info/sparse-checkout, nothing is in the folder anymore and therefore Git doesn't show the folder.
I confirm Git will only show content: if folder has no file (no "content"), said folder is not visible.

How to fix merge conflicts for a lot of files in git?

I am using the git mergetool command to fix conflicts. However I have thousands of conflicts, is there way to simplify this so I get everything from the remote?
I am asked to enter c, d or a in the command.
{local}: deleted
{remote}: created file
Use (c)reated or (d)eleted file, or (a)bort?
Since I have thousands of files, I don't want to keep sending c. Is there way to just do this in bulk?
You can solve this outside of git mergetool: run git status --porcelain to get a list of all unmerged files and their states in machine-readable format.
If your Git is new enough, it will support --porcelain=v2. See the git status documentation for details on the output formats. Output format v2 is generally superior for all purposes, but you should be able to make do with either one.
Next, you must write a program. Unfortunately Git has no supplied programs for this. Your program can be fairly simple depending on the specific cases you want to solve, and you can use shell scripting (sh or bash) as the programming language, to keep it easy.
Since you're concerned about the cases where git mergetool says:
Use (m)odified or (d)eleted file, or (a)bort?
you are interested in those cases where the file name is missing in the stage 1 ("base") version and also missing in the stage 2 ("local") version, but exists in the stage 3 ("remote") version. (See the git status documentation again and look at examples of your git status --porcelain=v2 output to see how to detect these cases. Two of the three modes will be zero.) For those particular path names, simply run git add on the path name to mark the file as resolved in favor of the created file.
Once you have marked all such files, you can go back to running git mergetool to resolve additional conflicts, if there are any.
Note that your "program" can consist of running:
git status --porcelain=v2 > /tmp/commands.sh
and then editing /tmp/commands.sh to delete all but the lines containing files that you want to git add. Then change all of those lines to read git add <filename> where <filename> is the name of the file. Exit the editor and run sh /tmp/commands.sh to execute all the git add commands. That's your program!
supposing you want their change and modified yours you can do a pull as like:
git pull -X theirs
Other stackOverflow answers
git pull -X
git merge strategies this link will help understand any other merge strategies for the futuro
If you want that all the change you did will be deleted and you will be sync with the remote.
You should do the following:
git stash
git pull
And if you want to restore the change you did you should type:
git stash pop
Basically 'git stash' is moving the change to a temp repository.
you can learn more in:
NDP software:: Git Cheatsheet

How to count the lines changed between two commits in gitlab?

I need to count line changes in the sources in between the particular period of time.
That means, by using the commit Id can I know the line changes count in the source?
I don`t want the line changed per user. I need all the line changes count in between that commits
My gitlab community Edition version is 8.14.3
Update
All my sources exist in the mycompany.gitlab.com. How can I achieve my above doubt?
In your git working directory, run:
git diff <commit-1> <commit-2> --shortstat
You should get an output as following:
7 files changed, 39 insertions(+), 107 deletions(-)
All code referenced can be found in https://docs.gitlab.com/ee/api/commits.html
Get the all the commits (include the since and until parameters):
https://gitlab.example.com/api/v3/projects/:project_id/repository/commits
Response:
Get the differences of a commit
https://gitlab.example.com/api/v3/projects/:project_id/repository/commits/:sha/diff
Split the above endpoints diff field to get the lines changed and sum for each commit.
I found the answer by using below steps
Clone the particular repository to the local machine by using the command git clone https://gitlab.company.com/testgroup/TestProject1.git
Move the source by using the command cd foldername
Use the example command to get the line details git diff --stat

Git checkout untracked issue

I'm collaborating with a few other people on a Drupal website which we are version controlling Git. We setup a local Git repository containing our commits.
After a colleague pushed some updates and I fetched and merged into my local dev branch, I began experiencing the following problems:
user#server:/var/www/Intranet/sites/intranet/modules/custom$ git checkout dev
error: The following untracked working tree files would be overwritten by checkout:
themes/bigcompany/panels/layouts/radix_bryant_flipped/radix-bryant-flipped.png
themes/bigcompany/panels/layouts/radix_bryant_flipped/radix-bryant-flipped.tpl.php
themes/bigcompany/panels/layouts/radix_bryant_flipped/radix_bryant_flipped.inc
Please move or remove them before you can switch branches.
Aborting
The issue above typically shows up when I try to checkout into other branches which fails and I am effectively trapped in my current branch.
Referring to this question, there is a suggestion my issue is related to the gitignore file. However, my .gitignore file has nothing indicating any part of my themes directory should be ignored as the following shows:
# .gitignore for a standard Drupal 7 build based in the sites subdirectory.
# Drupal
files
settings.php
settings.*.php
# Sass.
.sass-cache
# Composer
vendor/
# Migrate sourec files
modules/custom/haringeygovuk_migrate/source_data
As mentioned above, my attempts to execute git checkout into any branch fails with the message above. I decided to force it with the -f switch and successfully switched into my target branch but I lost a couple of hundred lines of code - which I'd love to avoid going forward.
I work on a Linux-Ubuntu VirtualBox which my colleagues prefer working in a WAMP setup and use the Git Bash terminal emulator for executing the Git commands. Could the difference in environments be causing these serious issues?
How can I resolve this issue?
Well, the situation is rather simple. You, in your current branch, don't have certain files under the control of Git, but at the same time, you have those files in your working tree. The branch you're trying to switch to, has those files, so git would need to override files in the working tree to perform checkout.
To prevent possible data loss, Git stops the process of switching the branches and notifies you that you should either add those files under the control of Git in a separate commit in your current branch, and only then perform the switch, or simply remove those files from the git way.
Likely you have chosen the second way. Generally you should "force" any operation only if you really understand what you're doing.

Resources