1

Now here is the commit id 41f9f4e392ab50db264e0328de7d69f1f10646eb, I want to get this commit id code which were modified only.

I use git show 41f9f4e392ab50db264e0328de7d69f1f10646eb to see modified files, how can I download these both after modified and before modified version via Linux git command? In a word, I just want to compare the differences from before and after commit id 41f9f4e392ab50db264e0328de7d69f1f10646eb to merge code easily.

Any idea will be helpful, thanks in advance.

  • If you want to compare what's different, git show already shows the differences. Why do you need the files? – muru Jan 31 '18 at 4:02
  • @ muru I need to get the midified files(before & after) then compare them, and merge them to another code branch, so I need the modified(before & after) files. – Huang Jan 31 '18 at 4:33
2

To see the state of files before and after a given commit, check out its parent:

git checkout 41f9f4e3~1

then the commit itself:

git checkout 41f9f4e3

However, it’s hardly ever necessary to look at details of a change in this way. In your case, to apply a commit to another branch, cherry pick it:

git checkout ${target_branch}
git cherry-pick -x 41f9f4e3

(replacing ${target_branch} as appropriate). The -x option adds a comment in the commit message with the origin of the cherry-pick.

1

Thanks all, I have found the correct way, here it is.

#!/bin/bash
from_id=$1
to_id=$2
#echo $from_id
#echo $to_id
diffpath='patch/diff.log'
newpath='patch/new/'
oldpath='patch/old/'
rm -rf patch
mkdir -p $newpath
mkdir -p $oldpath
git diff $from_id $to_id --raw > $diffpath
cat $diffpath | while  read line
do 
    #echo =====================================
    #echo $line
    OLD_IFS="$IFS"
    IFS=" "
    arr=($line)
    IFS="$OLD_IFS"   
    #echo ${arr[4]}
    filepath=${arr[4]##* }
    #echo $filepath
    newid=${arr[2]%%...}
    #echo $newid
    oldid=${arr[3]%%...}
    #echo $oldid  

   if [ "$newid"x != "0000000"x  ]; then   
     newfilepath=${newpath}${filepath}
     echo $newfilepath
     dirpath=${newfilepath%/*}
     echo $dirpath
     mkdir -p ${dirpath}
     git cat-file -p $newid >  ${newfilepath}
   fi

   if [ "$oldid"x != "0000000"x  ]; then 
     oldfilepath=${oldpath}${filepath} 
     echo $oldfilepath
     dirpath=${oldfilepath%/*}
     echo $dirpath   
     mkdir -p ${dirpath}
     git cat-file -p $oldid >  ${oldfilepath}
   fi   
done 

You will found the new directory named patch in current working directory after doing this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.