-1

I am fairly new to git and am a bit confused as to how to set up my workflow to work in the most convenient way possible.

I am working on my laptop (running OSX) and our much beefier server (running CentOS) through ssh, where I try and run time consuming tests for my Python code. Doing this for a few months I quickly realised that I was having parallel but separate lines of development since the folder structure is quite different.

I have thus taken the time to set up a github repo and cloned it into my laptop. Then I diffed the python files pairwise (server version vs laptop version) manually merged the differences and pushed the files up to Github. Lastly I cloned the repo on the server. Now I have the same version on all three places which is great.

In the future, I would like to avoid the divergence between the different platforms. Ideally I would like to automatically sync the local repo with Github every time I log in (should in theory be possible to do in .bashrc?), and log out, of the server. Essentially I am trying to avoid the chance that I forget manually syncing between the different machines.

How can I achieve this? Are there any potential pitfalls to this strategy?

posdef
  • 6,498
  • 11
  • 46
  • 94
  • That's called continuous deployment. Here's [an article](https://code.tutsplus.com/tutorials/setting-up-continuous-integration-continuous-deployment-with-jenkins--cms-21511) on how to accomplish that with a tool called Jenkins (one popular option amongst web devs and GitHub users... but there are many, many others). – JDB Oct 30 '17 at 17:27
  • To those who voted close on basis of broadness, what specific detail was missing? I am happy to provide more details but I feel the question is specific enough to warrant useful answers. It might be simple ones for many here, but last I checked SO was where people asked questions to learn about things they don't know/understand well. Also, the fact that there may be many possible solutions to a problem doesn't mean that the question is not answerable. – posdef Oct 30 '17 at 18:44
  • [From the FAQ](https://stackoverflow.com/help/dont-ask): "***avoid asking subjective questions where …** every answer is equally valid*" I think it's a perfectly fine question to ask, just not on SO. There are whole books written on CI & CD. There's no one right answer here... it all depends on your experience, budget, security concerns, workflow, preferences, etc. It's a very broad subject. This is very close to a "which tool should I use"-type question, but you avoided asking that directly, so I voted to close as "too broad". (Did not -1, though) – JDB Oct 30 '17 at 19:23
  • Here's a list of some of the books that have been written on this subject: https://ssearch.oreilly.com/?q=continuous+delivery – JDB Oct 30 '17 at 19:26

1 Answers1

0

For git itself, there is no such hooks or settings to automatically sync local repo with remote repo.

The work around is schedule to run a script to sync local repo with remote repo.

The sync script example (sync master branch) as below:

#!/bin/sh

git fetch
flag=0
ahead=$(git log origin/master..master --oneline)
if [[ -z "${ahead// }" ]]; then
  echo "Local repo has no commits un-pushed"
else
  {
    flag=1
    echo "local master branch is ahead of origin/master $flag"
  }
fi
behind=$(git log master..origin/master --oneline)
if [[ -z "${behind// }" ]]; then
  {
    if [ $flag == 0 ]; then
      echo "already sync"
    else
      git push
    fi
  }
else
  {
    if [ $flag == 0 ]; then
      git pull origin master
    else
    {
      git pull origin master --rebase
      git push
    }
    fi
  }
fi
echo "Now the local repo is aync eith remote repo"

To schedule runs the scripts, you can refer:

schedule running a bash shell script in windows

Running .sh every 5 minutes

You can also related issue here.

Marina Liu
  • 36,876
  • 5
  • 61
  • 74