-1

I'm working on a project and I need to identify all the files changed/committed in a local branch in GIT. Looking for an easy way which can go through all the commit and extract the files committed to the branch.

Biswajit Maharana
  • 531
  • 1
  • 8
  • 23
  • Unclear. Every commit consists of all the files, changed or not. If that’s what you want to know it’s easy. Is it? Also what does extract mean? – matt Apr 10 '20 at 04:12
  • Okay to make it more specific, let's assume after creating a development branch from master, I committed and pushed some of my code files like: ```1st commit -> xyz.c, demo.sh -> and pushed to development branch``` ```2nd commit -> run_schedule.perl, helloworld.c -> and pushed to development branch``` and so on. I want to now find the files committed in both the commit, a GIT command which can read all the commits and give me : ```xyz.c, demo.sh, run_schedule.perl, helloworld.c``` – Biswajit Maharana Apr 10 '20 at 04:29
  • Does this answer your question? [List all the files that ever existed in a Git repository](https://stackoverflow.com/questions/543346/list-all-the-files-that-ever-existed-in-a-git-repository) – Saurabh P Bhandari Apr 10 '20 at 05:00
  • @SaurabhPBhandari The command listed here is giving a lot of files which I don't think exists in my branch. – Biswajit Maharana Apr 10 '20 at 05:11
  • Files don't exist in *branches*. Files exist in *commits*. You need to pick some set of commits to query. Usually, you would pick exactly *two* commits—an old one, and a new one—and compare them to see what *changed* between those two. – torek Apr 10 '20 at 09:10
  • If you want to have Git walk, one commit at a time, backwards through commits *reachable from* a branch name, use `git log `. To have Git *compare* each parent with each child (except for merge commits), add `-p` to this `git log` command. To see only the *names* of files changed, along with what the change was, use `--name-status`. To ignore what the change itself was and get only the *name*, use `--name-only`. – torek Apr 10 '20 at 09:12

4 Answers4

1

If I understand your requirement, you need the diff between the local repo and the remote repo.

Let's take a look at what is different from our last commit to local repo from the remote repo by using the git diff command (diff of the local repo with the remote repo).

$ git diff HEAD origin/HEAD

Edit 1: As per your comment to this answer, try this if you are only interested in the list of names.

git diff --name-only HEAD origin/master
Khanna111
  • 3,627
  • 1
  • 23
  • 25
1

You are looking for git diff --name-only.

It takes two parameters. The first would be the name of your branch. The second would be the SHA at the point where the branch branched off from the parent branch. If you don't know what that is, git merge-base will tell you.

Example (here, develop is the common working branch, mybranch is my own):

$ git merge-base mybranch develop
4e5e735e9656dcf34a99827af6c2660a6c796d6f
$ git diff --name-only mybranch 4e5e73

You will see the list of names you are looking for.

matt
  • 515,959
  • 87
  • 875
  • 1,141
0

You can use the following comand

git log

This will show you the history of commits of the branch you are in, like this

commit a1b2c3d4e5 (feature/example-branch)
Author: exampleauthor <example@gmail.com>
Date:   Thu Mar 26 10:23:31 2020 -0300

    [REPO-3333] Example pull request (#333)

After that, you can use git cherry-pick (commit code) to extract the commits you want into a new branch. I use this when I have broken something and i don't know where.

Hope it helps you :)

0

This might work. Please try this:

git log| grep commit | tr 'commit' ' ' | while read -r line ; do  echo "Processing $line"; git show --pretty="" --name-only $line; done

PS: Ignore the fatal lines, that comes from git log having response like 'Initial commit' rather than commit IDs which is not relevant here I guess.

Nandu Raj
  • 2,072
  • 9
  • 20