Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check the differences between a local and GitHub repository before the pull [duplicate]

Tags:

git

github

People also ask

How can you tell the difference between a local and remote repository?

You can git branch -a to list all branches (local and remote) and then choose the branch name from the list (just remove remotes/ from the remote branch name. Example: git diff main origin/main (where "main" is the local main branch and "origin/main" is a remote, namely the origin and main branch.)

What is the difference between git clone and git pull?

git clone requires a git init on the repo level and creates an additional layer of repo folder in the local repo. Personally I don't see the need for a . git file in the repo level and also an additional layer of repo folder. IMO, git pull is a more elegant method to create a new local repo.

How do I pull changes from a cloned repository?

To pull down (i.e. copy) the changes merged into your fork, you can use the Terminal and the git pull command. To begin: On your local computer, navigate to your forked repo directory. Once you have changed directories to the forked repo directory, run the command git pull .


git pull is really equivalent to running git fetch and then git merge. The git fetch updates your so-called "remote-tracking branches" - typically these are ones that look like origin/master, github/experiment, etc. that you see with git branch -r. These are like a cache of the state of branches in the remote repository that are updated when you do git fetch (or a successful git push).

So, suppose you've got a remote called origin that refers to your GitHub repository, you would do:

git fetch origin

... and then do:

git diff master origin/master

... in order to see the difference between your master, and the one on GitHub. If you're happy with those differences, you can merge them in with git merge origin/master, assuming master is your current branch.

Personally, I think that doing git fetch and git merge separately is generally a good idea.


If you're not interested in the details that git diff outputs, you can just run git cherry which will output a list of commits your remote tracking branch has ahead of your local branch.

For example:

git fetch origin
git cherry master origin/master

Will output something like:

+ 2642039b1a4c4d4345a0d02f79ccc3690e19d9b1
+ a4870f9fbde61d2d657e97b72b61f46d1fd265a9

It indicates that there are two commits in my remote tracking branch that haven't been merged into my local branch.

This also works the other way:

git cherry origin/master master

It will show you a list of local commits that you haven't pushed to your remote repository yet.


And another useful command to do this (after git fetch) is:

git log origin/master ^master

This shows the commits that are in origin/master, but not in master.

You can also do it in the opposite way when doing git pull to check what commits will be submitted to the remote.