Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'git diff' between current workspace and master

Tags:

git

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 the latest source code, I use the 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.

like image 480
Ethan Hunt Avatar asked Oct 15 '25 14:10

Ethan Hunt


2 Answers

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 repository (usually called origin by default) into a local repository on your PC.
  • You do have a workspace locally, but it's a combination of several things:
    • The local Git repository 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 repository
    • 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 repository.
  • Cloning actually creates a duplicate of the remote repository on your PC, that you can commit changes to your new local repository separately from the remote repository (then sync up with it using pull and push). Not only can you commit changes locally, updating the repository, 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 repository.
  • When you pull from the remote repository, 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 repository, you just want to see the changes between the repository 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 repository 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 two commits back, you can use:

git diff HEAD~2 HEAD

The HEAD~2 just looks two 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.

like image 156
LightCC Avatar answered Oct 18 '25 06:10

LightCC


In general, you are always on a branch in Git (an exception might the detached head state). Unless you recall creating a branch, my guess is that you are on the master branch. To see changes you have made relative to where you began, you can use the following from the command line:

git diff HEAD

If you are using an IDE such as IntelliJ or Eclipse, with a Git plugin, then you can visually see many more types of diffs of the files in your workspace.

like image 42
Tim Biegeleisen Avatar answered Oct 18 '25 07:10

Tim Biegeleisen