How to git push to two repositories while git pull from one repository - linux

I'm working on linux server (non-desktop) and I have two repositories. One is for development (github), the other is a local repository of my lab (gitlab).
What I need is to keep the two repositories always exactly the same, and I can push and pull on my dev repository, whereas only push on my local repository so that my colleagues can git pull from my lab repository. My lab-repository is used only for sharing my project with my colleagues: I do nothing but git push and they do nothing but git pull. That's all.
Well, you may ask why don't use the dev repository. That's because they have no right to connect to the Internet.
I don't like to execute many commands for each action. For example, for now I have to execute the commands below to git push to the two repositories:
cd devRepo
# coding here
# git add & git commit & git push here
cd ../myLabRepo
cp -r ../devRepo/* ./
# git add & git commit & git push here
This is kind of boring. Ofc, I know I can use bash script but I'm wondering if there are some specialized tool to do so or maybe Git has the capacity to do so.

You can specify more than one push location for the same remote.
git remote set-url --add --push <remote name> <push url>
Check this with git remote -v
You should see the original push and fetch URLs, along with the extra push URL you just added.

You can add the whole stuff that you have into a bash script and run that. Otherwise, you can add a remote to your first repository and do a push there as well, instead of copying your whole project.
git add & git commit & git pull origin master & git push origin master & git push local master

Related

problem with my git repo, I lost all my commits?

I was trying to upload a file via terminal.I am trying to learn git.
**
git add /Users/serra/Documents/useSDWebImage
git commit -m "learning on process"
git remote add origin
git push -u origin master
**
then I got this message error: failed to push some refs to ....
I tried those commands then I have only the file I want to upload
to repository I have lost rest of the repository.What should I do?
**
git pull --rebase origin main
git push origin main
git pull origin master
git push origin master
git push origin master --force
**
TL;DR
You need to find a backup, somewhere, of origin/master.
git reflog might help you.
The error
First, let's go over your first four commands and explain what the mistake was.
In the first two commands, a git add and git commit, you are creating a commit in your local sandbox, building on whatever was in your sandbox beforehand.
In the third command, you add your remote, which means that your commit was not made on top of that remote, but on top of something else.
In your fourth command, Git correctly tells you that you cannot push, because your local sandbox is not built on top of the remote. It's important to pay attention to Git's error messages, they're almost always informative.
Now, you're not showing outputs from any of the commands you ran, but I'm guessing git push origin master --force is what deleted stuff on your remote.
What you should have done
Normally, you clone the remote first, then commit stuff on top of it:
git clone <URL>
cd <SANDBOX_DIR>
git add <file inside this directory>
git commit
git push
How to fix this
There are several ways, but they're all based on recovering stuff from a backup that hopefully exists somewhere.
If your remote is GitHub, it keeps a history of references, you should be able to find that old master branch somehow there. If your remote is some other server, that is hopefully true too. If another team member has a sandbox that had the valid master you want to restore, they can fix things by doing a git push origin master --force in their own sandbox, but make sure they understand what they're doing before they do it.
My answer here is not complete, because you didn't provide enough information. I will update it if you update your question with more details about your remote.
Update - git reflog might help
When you did git pull origin master, assuming master is the branch you care about, you probably got a local copy of the correct master commit you want on origin. You can use git reflog to see the history of your local HEAD in your sandbox. If you find the good commit there, this solution could work:
Here I'm assuming these is not work you want to preserve in your sandbox.
git checkout master
git reset --hard <good commit> # WARNING: destroys any uncommitted local changes
At this point, check that your sandbox contains the files you want to restore to origin. If so, proceed with this command:
git push --force origin master
But be warned, this will overwrite what's on origin. It's only appropriate if you have carefully validated that master in your sandbox really contains the history you want to restore on origin.

git push --mirror - internal server error

I have created a git repo using (GIT API) and initiated it with git init. (but no git add or git commit command) . when i try to use the git push --mirror , it gives internal server error.
I saw the following bug (100+ ref will give internal server error)
https://github.com/isaacs/github/issues/1253
even my repo got 500+ refs.
Do i need to use git add and git commit to push the repository to github with mirror command ?
when i do the git add and git commit on local repo, i can push the content.
I'm doing all those using the API, and is it possible to use git push --mirror without any git commit or git add commands?
When i go to the remote repo, i can see the GitHub quick setup page, asked to execute git add, commit etc...
my questions is how should i avoid that GitHub quick setup page,and push my contents to the remote repo without executing git add, or git commit ?
I don't understand what's your content.
If you have file and don't add them and don't commit, you have no content, as far as git is concerned.
You need to have at least a commit to push your repository, otherwise you're pushing nothing.

Can't find my git repository url

So I think I am confused - but for some reason I cannot push or pull to my git repository.
I have a linux web server and have a web folder in /var/www/bcs.net.nz/
I did a git init bcs.git in this folder (I have also tried .git)
and then I thought I could do git clone git#bcs.net.nz:bcs.git to clone it on the local machine or git push on the remote machine.
I have also added a git remote add origin git#bcs.net.nz:bcs.git on the remote machine.
After all of that I still cannot push and pull anything.
I am a bit stuck.
git add . & git commit -m "initial commit" work fine.
git clone git#bcs.net.nz:bcs.git would mean to clone a (preferably bare repo) which is in /home/git/bcs.git
You could initialize one there, (git init --bare /home/git/bcs.git), and add a post-receive hook (chmod 755 /home/git/bcs.git/hooks/post-receive) which will checkout the code in the live server folder
#!/bin/sh
git --work-tree=/var/www/bcs.net.nz --git-dir=/home/git/bcs.git checkout -f

How to add a git repository to Ubuntu server

I have a node.js project local in my computer, right now, I want to push this project to remote Ubuntu server. I have create a project.git on Ubuntu server, and pushed to it. Right now, I want to run this project on the server, but how do I access this folder, it's just a git directory. The process I am following is like below:
Ubuntu server:
mkdir /home/git/project
cd /home/git/project
git init
Local computer:
git remote add origin root#someserver.com:/home/git/project
git add .
git commit -am "Initial Commit"
git push origin master
It push succeed. Right now, I want to execute this node.js project and run it on the server, but how to turn this git repository to a folder, so I can cd into it and then node app.js after?
If you want to see the code on the server side, you should init the repository without --bare option or clone it somewhere else on the server.
Bare repositories store only git specific files.
UPDATE:
If you create a repository without --bare option, you can't push anything to that branch where your server is staying. Better solution is if you create a bare repository and clone it somewhere else on the server. Notice that, you should use there git pull command if you want to see your fresh code.

Added BitBucket repo as remote on GitHub

I've just installed git on Ubuntu, now I want to work on my repo in BitBucket. I'm a little confused on how to do so. I can't do the following:
git remote add BitBucketRepo git#bitbucket.org:dir/file.git
As it returns the following error:
fatal: Not a git repository (or any of the parent directories): .git
It clearly is pointing to a git repo, so why is it lying to me?
Also, it is worth noting I am using SSH and I have successfully paired my GitHub account to my computer.
You need to run this command from a local git repository (a directory in which you have run git init or git clone) - otherwise git remote doesn't know which local repo you want to add the remote for.
It should be as simple as cd my-local-dir, where my-local-dir is the directory containing your local (cloned) git repository.
If you don't yet have the repo available locally:
git clone git#github.com:...etc... my-local-dir
cd my-local-dir
git remote add ButbucketRepo git#bitbucket.org...
git push -u ButbucketRepo master
This will clone your code from Github into the my-local-dir directory, add your BitBucket repo as a remote repository, push your code up to Bitbucket and set the local master branch to track the BitBucket remote's master branch.
Tracking means that commands that involve a remote like git push will automatically use the BitBucket remote's master branch. If you don't want that behaviour, skip the -u option.

Resources