Raphael Raphael - 3 years ago 153
Git Question

git diff between current workspace and master

There are many similar questions but my question is little different.

I am a tester and have limited knowledge about git. I have cloned a project and created a local workspace. When I want to get latest source code, I use

git pull
command. Correct me if I am wrong..I think that I don't have a branch. Now I want to know the changes between my workspace and master. In short I want to know the changes that are going to be applied on my workspace.

I have tried
git diff master
but it returns nothing.

Please Help.

Thank You.

Answer Source

This is the real basics of Git. I strongly recommend going through a visual git tutorial (just google that). Also, I recommend getting the open source GUI tool Git Extensions so you can visually see your commits/branches and learn different commands easier.

Some terminology and beginner's tips:

  • You cloned a remote repo (usually called origin by default) into a local repo on your PC.
  • You do have a workspace locally, but it's a combination of several things:
    • The local Git repo itself, where all the information about the commit history is stored, this allows you to recreate the working directory for any commit.
    • The Working Directory - these are ongoing changes and differences that have not yet been committed into the repo
    • The Index - this is where changes from the Working Directory have been staged and are ready to be committed.
    • The Stash - this is a sort of temporary commit, where you can save the current changes from your Working Directory and/or Index temporarily while you go off and do something else in your repo.
  • Cloning actually creates a duplicate of the remote repo on your PC, that you can commit changes to your new local repo separately from the remote repo (then sync up with it using pull and push). Not only can you commit changes locally, updating the repo, but before you can commit changes locally, you must stage them into your index. Only items in the index get committed, and then only commits get pushed to the remote repo.
  • When you pull from the remote repo, it brings down any new commits from the current branch you have checked out (with git fetch), and then will git merge your local master branch into the remote master branch that you just pulled down.
  • Unless you have created a new branch and committed locally, you are likely in the master branch.
  • Merging may cause a merge conflict that you would have to resolve before committing the merge locally, and then finally pushing your local commits to the remote.

How to see changes ready for committing:

  • If you want to see the list of changes in your working directory (unstaged) and your index (staged and ready for a commit), type git status.
  • If you also want see the actual changes in each file that is changed, then you can type git diff HEAD.

Although it's really easy in Git Extensions to see the current changes - just hit the "Commit" button, and a window pops up with the Working directory on top left, index on bottom left, and if you click on any file in them, you'll see all the changes for that file on the Right side.

This is Not the Diff You're Looking For

(Sorry - couldn't resist the Star Wars quote)

Okay, I finally understand your core question - you are not making changes to the local repo, you just want to see the changes between the repo before the pull and after the pull - to see what changed since the last time you pulled.

This can be done by getting the hash of the last commit on master before the pull, and then using:

git diff <hash of prior commit> HEAD

HEAD refers to whichever commit is currently checked out, either the current commit of the branch you checked out, or a particular commit if you checked one out directly (this is a state called a "detached HEAD"). HEAD will point to master which will should point to the latest commit in the repo after a successful pull.

Instead of using HEAD, you could put the actual hash of the latest commit. Note that if you know how many commits were pulled down, you don't even need to know the commit hashes. For example, if the prior commit was 2 commits back, you can use:

git diff HEAD~2 HEAD

The HEAD~2 just looks 2 commits back in the log from HEAD, and uses that hash.

To do this with a GUI, in Git Extensions, you can just right-click on the prior commit and select "Compare > Compare to current branch" in order to pull up the diff window with the changes between that commit and the current location of your current branch.

The currently checked out branch/commit is shown in bold in Git Extensions, and should always be at the top of the list and have a >master tag next to it for your situation.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download