GitHub tutorial 12: Compare with diff

preview_player
Показать описание
When you are working on Git repositories, one of the frequent actions is likely to inspect the changes. You may want to find out the differences between your working directory and the staging area before you send git "add" command, or between the staging area and local repository before you send git "commit" command, or between the local repository and remote repository before you send git "push" command.

How can you achieve these goals? Or which Git commands can be used to carry out the comparison task?

For comparison between working directory and staging area, you can use git "diff" command. If you know which specific file to compare, you can also append filename to this command.

For comparison between the staging area and local repository, you can use git "diff" command with option "--staged".

For comparison between local repository and remote repository, you can use git "diff" command followed by local branch name, like "master", and remote repository URL and branch name, for example, origin as default remote URL alias, master as remote branch name. By the way, if you want to find out exactly what "origin" is pointing to, use git command "remote" and option "-v".

Here is an overview of all three commands that we will use in the following demo.

First, let's take a look at how to compare the differences between working folder and staging area.

(demo begins)

Send command "diff" to see what happens. It shows staged file "a" has text: "demo", while unstaged file in working folder has different text: "test git diff commands". It works as expected!

(demo ends)

After we have known how to compare the differences between working folder and staging area, suppose we decide to add the changes from working folder to staging area. Now we are more interested in comparing the differences between the staging area and local repository. Let's continue our experiment.

(demo begins)

First, add the change to staging area. If we send "git diff" command, we should see no difference, or nothing returns. This is expected. It confirms that all the changes have been moved to staging area. Now add option "--staged" to git diff command. We will see that file "a" in local repository still has text: "demo", but staged file "b" has the newly added text: "test git diff commands". It works well.

(demo ends)

Now the changes have been committed to local repository, before we move on to push them into remote repository, you may want to double check if the changes are expected. The is the final task we will do in this tutorial: comparing the differences between local repository and remote repository.

(demo begins)

As planned, send command "commit" with comment "testing diff cmds.". Now if you send command "git diff --staged". No difference is found, since the changes have been transferred from staging area to local repository.

The differences will show up when we compare local repository master branch, with remote repository master branch. Here "origin" is the alias of remote repository URL. The message shows that local file "a" has the modified text, while remote file "b" has original text.

Push the changes to the remote repository. Wait for the transaction to finish. Compare the difference again. Surely, we don't see any difference.

(demo ends)

Thanks for watching!
Рекомендации по теме