How do I check whether the remote repository has changed and I need to pull?
Now I use this simple script:
git pull --dry-run | grep -q -v \'Already
Run git fetch (remote)
to update your remote refs, it'll show you what's new. Then, when you checkout your local branch, it will show you whether it's behind upstream.
I use a version of a script based on Stephen Haberman's answer:
if [ -n "$1" ]; then
gitbin="git -C $1"
else
gitbin="git"
fi
# Fetches from all the remotes, although --all can be replaced with origin
$gitbin fetch --all
if [ $($gitbin rev-parse HEAD) != $($gitbin rev-parse @{u}) ]; then
$gitbin rebase @{u} --preserve-merges
fi
Assuming this script is called git-fetch-and-rebase
, it can be invoked with an optional argument directory name
of the local Git repository to perform operation on. If the script is called with no arguments, it assumes the current directory to be part of the Git repository.
Examples:
# Operates on /abc/def/my-git-repo-dir
git-fetch-and-rebase /abc/def/my-git-repo-dir
# Operates on the Git repository which the current working directory is part of
git-fetch-and-rebase
It is available here as well.
Here's my version of a Bash script that checks all repositories in a predefined folder:
https://gist.github.com/henryiii/5841984
It can differentiate between common situations, like pull needed and push needed, and it is multithreaded, so the fetch happens all at once. It has several commands, like pull and status.
Put a symlink (or the script) in a folder in your path, then it works as git all status
(, etc.). It only supports origin/master, but it can be edited or combined with another method.
I based this solution on the comments of @jberger.
if git checkout master &&
git fetch origin master &&
[ `git rev-list HEAD...origin/master --count` != 0 ] &&
git merge origin/master
then
echo 'Updated!'
else
echo 'Not updated.'
fi
I suggest you go see the script https://github.com/badele/gitcheck. I have coded this script for check in one pass all your Git repositories, and it shows who has not committed and who has not pushed/pulled.
Here a sample result:
The below script works perfectly.
changed=0
git remote update && git status -uno | grep -q 'Your branch is behind' && changed=1
if [ $changed = 1 ]; then
git pull
echo "Updated successfully";
else
echo "Up-to-date"
fi