GIT commands to automate repo mirroring - bash

I'm working for a client who has their own secure git repository, which is only accessible by a VPN connection. My company has its own secure VPN, where our GIT server resides. Some colleagues don't have access to the client's repo and updating our repo is very time-consuming.
I wrote a small sh script that asks us to connect to the client's VPN, and once connected it fetches all relevant repos. The script then asks us to disconnect and connect to our own VPN, after which the script must push the changes.
I'm having trouble with this last part. How can I have git push everything I just fetched? This is not only the typical "develop" and "master" branches but also new branches with unknown names (e.g. newFeature/XXXX, where XXXX is unknown and can change).
Thanks in advance for your help!

See "Set up git to pull and push all branches", which includes, as commented:
git push --all origin
In your case:
git push --all -u
if you add -u once, e.g. git push --all origin -u, tracking is setup and after that you can simply use git push.

Related

Bash Script: Git fetch unable to fetch latest updates to remote branch

I am writing a bash script for automatic git interaction. The issue is that once I run my script with the changes, I am able to push correctly, but on next run, git fetch does not show me the latest commits(on remote branch) which I did using the script.
I am able to see my change commit of my script on our repo(gitlab), but if I run the script again with other commit, during the fetch part, I dont see any ref updates
branch HEAD -> FETCH_HEAD
If I check manually doing git fetch origin command, it updates the refs correctly, but the git fetch ${_gitUrl} does not update the refs of my remote. I am using the gitUrl constructed because I dont want a uName and password prompt, and I did not want to use ssh here.
How can I use git fetch with my https url to reliably update my remote refs? Please help.
My git push is: git push ${_gitUrl} "${_branchToPush}:${branchName}"
My git fetch is: git fetch --prune "${_gitUrl}"
I have also tried git fetch "${_gitUrl}"
My gitUrl is: https://uName:pwd#repo/project.git
My git config --get remote.origin.fetch
+refs/heads/*:refs/remotes/origin/*
Checking the HEAD after fetch: git log origin/${branchName}
How can I use git fetch with my https url to reliably update my remote refs?
What remote refs? It seems you think that git fetch/pull/push "${_gitUrl}" makes git match the URL back to remote name? No, git doesn't do that.
One reason is efficiency. Why spend time mapping URL to remotes?
Another reason is unambiguity. What if there are multiple remotes with the same URL? Git allows that. To what remote Git should map the URL in that case?
So no, Git doesn't do that. Name your remote yourself.
I found a work-around for this. As pointer out by #phd that git does not map url to remotes, meaning on git fetch ${_gitUrl} will not update my remotes(I was assuming origin/*) will be updated.
For this to work, I mapped the remote name(origin) with the url:
git remote set-url origin ${_gitUrl}
git fetch ${_gitUrl}
This is correctly fetching the remotes from url. I dont know if this is best solution, as now the password is stored from the url and visible on git config --list.

Backing up full remote git repository with history to another remote server

Assume that I have to GIT servers behind a fence, say at git.mycompany.com (with gitea UI) and at git.myclient.com (with github-like UI), both heavily security with VPN, multi factor-authentication etc. I want to deliver my full repository myProduct to git.myclient.com/alice/myProduct since Alice is my point of contact.
Can I do that directly without the detour over a local repository on my computer?
Since I am working remote, and the uplink of mycompany.com is much faster than my own...
My current lengthy and slow approach
In detail, the detour over my computer looks as follows:
Using the (github-like) user-interface, create an empty repository at git.myclient.com called myProduct.
Make sure that my local repository is up to date with git pull.
Check my current remote origin with git config --get remote.origin.url, see e.g. an answer to How can I determine the URL that a local Git repository was originally cloned from?
In my case, the result simply is https://git.mycompany.com/b--rian/myProduct.git
Change this configuration to the destination repository with git remote set-url origin git#git.myclient.com:alice/myProduct.git.
Generate a key-value pair for ssh using ssh-keygen -o, see https://git-scm.com/book/en/v2/Git-on-the-Server-Generating-Your-SSH-Public-Key
Ask Alice to navigate to Settings > GPG and SSH keys (usually at
https://git.myclient.com/settings/keys) and ask her to add my New SSH key from the previous step.
Make sure that the SSH agent is running on my windows box, if not start it with eval $(ssh-agent -s) inside the GIT Bash, see https://help.github.com/en/github/authenticating-to-github/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent
Now, finally, I can push most things to the client using git push --all origin. This is the slow step which I would like to speed up.
The tags have to be pushed separately, I heard: git push origin --tags, see How do you push a tag to a remote repository using Git?
Wind back everything by setting the remote.origin.url to back what it was in step 3, in my case it is a git remote set-url origin https://git.mycompany.com/b--rian/myProduct.git.
Is there an easier way?
Maybe you could use Git mirroring on git.myclient.com.
It's up to clients VCS (Git). Most implementations have mirroring options.
For example, if the client is using Gitlab:
https://docs.gitlab.com/ee/user/project/repository/repository_mirroring.html
I think it would be much simpler to add a second remote... and then push into that remote:
git remote add second url-to-new-repo
git push second master develop # push master and develop to second
And so on.

How to push a new code to an existing git repository in github

I need to push my modified new java code to my old git repository in github but I do not have old code in my pc. How to do that?
I had push a code before my github account before. Now I don't have that old code in my pc. How do I pull the project into my pc and after making changes, push again to the same repository?
I do not have much experience in github, so please help me to improve skills on github.
Check your remote first to see where it is pointing to by
$ git remote -v
origin ssh://git#<old-git-url>/<project>.git (fetch)
origin ssh://git#g<old-git-url>/<project>.git (push)
Change the pointing to GitHub
$ git remote set-url origin ssh://git#<github-url>/<project>.git
Now your repo is pointing to Github
Now you can make your changes and then add them and do a commit and finally push to remote branch; say you are on master.
git add <file>
git commit -m <commit message>
git push origin master
I had to do just that and achieved it like this.
Setup. I assume you have a new remote repository with files that may not cause a conflict with the directory you currently have locally.
Git clone from the git repository you need to push to. Just make sure you create a new directory for the cloned code.
Copy the contents of the cloned repository into the local directory that has your current code. Make sure to copy the .git (hidden) file.
cd into your local directory and run git remote -v. You should see the remote repository git address.
git add -A to add whatever change you require and commit it.
Finally git push
You need to make sure that your local repository (the one that is on your computer) is connected to the remote repository (the one that is on the GitHub servers).
After this, you need to add the modified file to the staging area. Say, you have a file test.txt that you have modified, you would add it to the staging area by typing
git add test.txt
After that you would need to commit those changes. You can do that by
git commit -m "commit message"
And that's it, you have now saved those changes and recorded them in the version control. But the changes that you made have only been recorded in your local repository and you would need to push these changes to the remote repository (the GitHub servers). You can do this by
git push origin master
It would take a few seconds (depending on your internet speed and the project file size) to push these changes to the remote servers. Once it's done, you can open that repository on GitHub and see the changes for yourself.
Just to amend this a bit. I believe "master" is now "main" in some instances or platforms. If this answer isn't working for you, try swapping that out. It worked for me.

Configure git to be accessed in my network from portable HDD under Windows

I need to have a portable git setup in order to access a git repository in my LAN from a portable HDD, including the computer where the HDD is connected, under Windows. The HDD might be moved from time to time between the computers in my network. I wish to avoid SSH for the moment.
I have installed the portable version of the git, I have made a batch to set the PATH to the requested directories specified in the documentation before running git-bash or git-cmd.
I see it runs, I have made a bare repository, let's say in a path like m:/repo.git. Then, I got stuck as I don't know how to configure the remote in order to do the first push as `git push repo master' from my project path.
I think I should do a 'git remote add repo ' but I fail to set the correct URL or something. I am aware I should change the URL each time the HDD is moved or change the remote.
What are the correct setup steps?
Then, I got stuck as I don't know how to configure the remote in order to do the first push as `git push repo master' from my project path
Let git create that setting for you:
git clone m:/repo.git
cd repo
git --work-tree=..\myproject add .
git commit -m "first commit"
git push
That will import the files of your project in a local repo, which will be able to push back to your bare repo on M:\.
UNC paths are supported too
git.exe clone "d:/dev/SDK" "//comp1/Proj/git/SDK/"

Git connect using ssh on windows

I have read some questions around but still didn't found the solution to this problem
I have git on windows and I want to connect to github using ssh.
Following this tutorial https://help.github.com/articles/generating-ssh-keys
I have successfully setup my keys
If I open a git-bash and try to ssh github I am able to connect, so this works
ssh -T git#github.com
Hi username! You've successfully authenticated, but GitHub does not
provide shell access.
this means that git-bash actually sees my keys. However if I try to do a push
git push origin master
git prompts me for username and password
Thank you for your help
EDIT: solved by using the git protocol instead of the http protocol, my fault
so replace this
https://github.com/_user_/_repository_.git
whit this
git#github.com:_user_/_repository_.git
in the remote link
No need to remove/add, just do something like this:
git remote set-url origin git#github.com:username/repo.git
You probably cloned a remote that uses the https protocol, rather than the git protocol. You need to redefine your remote for origin.
# Change this as needed.
github_username="$LOGNAME"
# Replace your remote, then push.
git remote rm origin
git remote add origin "git#github.com:${github_username}/${PWD##*/}.git"
git push --tags --set-upstream origin master

Resources