I'm pretty new to git and I got the basic workflow down. Now I'm trying to improve my skills a bit with git.
Is there some easy way for me to review all of my changes before I push to a remote branch and open a PR?
It would be nice to make sure I didn't forget to remove any unnecessary comments and code. The main thing I could do is simply open the PR and check on Github "files changed" and review it there and do another commit and push if I need to clean things up.
But I'm not sure if there's a way to do this before the PR is even opened. I'm using Android Studio btw so if there's a way in Android Studio (which is basically IntelliJ) then that would be good.
To see all the diff in tracked files but not staged:
git diff
or
git diff path/to/a/given/file
to see the diff only for a file. You can also see the diff in a given sub-directory of your project:
git diff path/to/a/dir/
If you have already staged the changes with git add, you can see what patch you have staged with
git diff --staged
You can also specify a path with --staged.
Make sure you've staged some changes. Otherwise, git commit -v will show you a block similar to what you posted, but not do anything. You can stage changes manually with git add, or if the files are already versioned, you can use git commit -a -v to stage and commit the changes.
For example:
$ echo "more foo" >> foo.txt
$ git commit -v
# On branch master
# Changes not staged for commit:
# (use "git add <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# modified: foo.txt
#
no changes added to commit (use "git add" and/or "git commit -a")
Staging the change shows the diff with git commit -v:
:: git add foo.txt
:: GIT_EDITOR=cat git commit -v
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# modified: foo.txt
#
diff --git a/foo.txt b/foo.txt
index 257cc56..a521556 100644
--- a/foo.txt
+++ b/foo.txt
@@ -1 +1,2 @@
foo
+more foo
Aborting commit due to empty commit message.
If you just want to see the diff without committing, use git diff to see unstaged changes, git diff --cached to see changes staged for commit, or git diff HEAD to see both staged and unstaged changes in your working tree.
UPDATE: given your edit, what you really want are the git diff derivatives above. I'm not sure how Aptana Studio works. It may not follow the typical command line git flow. On the command line, you'd stage your changes, and then commit. And the above git diff commands are what you'd use to examine those changes. I typically alias them as git unstaged, git staged, and git both by adding this to my ~/.gitconfig:
[alias]
# show difference between working tree and the index
unstaged = diff
# show difference between the HEAD and the index
staged = diff --cached
# show staged and unstaged changes (what would be committed with "git commit -a")
both = diff HEAD
How to see changes in git commits, before pushing them | Drupal.org
how does git know what change you're commiting?
linux - How to get the modified files(before and after) from git version server? - Unix & Linux Stack Exchange
Can I see all of my changes before I push them and open a PR?
Videos
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.
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.