Git: Push project folders into their repositories - bash

I'd like to write a script that, when run, pushes all the last updated files into the matching GitHub repositories.
Here's what I tried:
myPath=absolute_project_path
for i in $*
do
if [[ -d $myPath/$i ]]
then
cd $myPath/$i
git add ./*
git commit -m "update"
git remote set-url origin https://usr:pwd#github.com/username/$i.git
git push -f -u origin main
fi
done
But this is adding all the folders to all the directories: so myrepo1 in GitHub, gets myrepo1+myrepo2 files, and myrepo2 gets myrepo1+myrepo2.
Does anybody know the reason why?
Notes on the workarounds tested, if needed: It seems that the staging needs to be emptied every time before changing file for another repository.
But i tried this also:
- Did a backup of my folders, then ran git rm -f, git add, git commit, git push (caused local folder to get deleted), re-copied from backup, and re-ran the script, but didn't help
- Also tried to manually remove all the folders on GitHub and re-push them, but caused Git to want me to do a Pull. But once the Pull has been done (which caused the deletion of my local files), a re-copy from backup and a new Push caused Git to ask to make a new Pull again... reason why of the push -f

It is only a suggestion, but for this you can use already existing tools like myrepo.
Simply register your repos:
mr register ~/gitHub/repo_1
mr register ~/gitHub/repo_2
...
then if you want to push all your repos, simply type:
mr push
if you wan to pull them all, type
mr update
an so on... read to doc.

Related

git - How to get files changed with specific commitid without update to head?

I want to make a automated test system. Users upload or change their script, when they push file to git, system will get the commit action, and know the commit id, like 3c6a88d527ccaefabf98d00f47f04789f01e2830, and boot a docker to run changed script.
at first, git clone all file to a directory, after that get what files is changed by order:
git diff-tree --no-commit-id --name-only -r <id>
After a long time, there will be a lot of scripts in the repository, I don't want to pull all the scripts to the local directory every time I test.Is there any command to pull only part of the files I want?
# Shows commit logs and diff output
git whatchanged <sha-1>
Using diff-tree (as you figured out)
# Fetch out changes (no need to checkout branches)
git fetch --all --prune
# Now print out the changes
git diff-tree --no-commit-id --name-only -r <id>

Can I pull from git when local and remote changed the same files but not the same lines

If I have a file in git that I changed the first and on remote I changed the last line, like so:
test.py - local
+ import test
import bla
...
...
...
test.py - remote
import bla
...
...
...
+ print(done)
If I try to git pull I get an error that I have local changes that will be removed, but if I stash them and apply them after it is working great.
# option 1:
git pull # error: Your local changes to the following files would be overwritten
# option 2:
git stash
git pull
git stash apply # work great without conflict
Is there a way to tell git to behave like the second option in one command?
git pull has an option --autostash which stashes local modification first, and applies the stash entry when done. However it works only with --rebase or -r. In your case you can use git pull -r --autostash in one command.
The default pull is merge-pull. --rebase indicates rebase-pull which applies the local unpushed commits onto FETCH_HEAD. Merge and rebase work differently. You may not want rebase in some cases. Anyway, when you think it's appropriate to make rebase-pull, -r --autostash does the job.
You can also define an alias or a function to do the job in one command.

having trouble with a git sparse checkout (folder)

here is my git server tree:
----a/
|--b/
|--c/
|--d/
|--e/
|--f/
|--g.txt
|--h.readme
here is my git version tree
---------------->master
|---->myBranch [files are up to date here]
here is my jenkins server tree
-----A/
|--B/
|--C/
I am trying to checkout the content of repository "e" in repository "B"
I just need the last version of the current branch i'm working on, and I don't want to commit, just to get some up-to-date read-only scripts to run, so depth 1 is okay I guess.
Anyway, The problem I'm facing is that I'm able to to a sparse checkout with
#############################
##### 1/ configuring local folder
#############################
git init
git config core.sparseCheckout true
#an empty repository with the remote is created:
git remote add -f origin ssh://guillaumedg#domain.com:port/myProjects/thisProject.git
#add repository "e" to be checked out:
echo e/*> .git/info/sparse-checkout
#############################
##### 2/ fetching/updating files
#############################
git fetch
git checkout myBranch
I know I'm close, but what I get is this:
-----A/
|--B/
| |----a/
| |--d/
| |--e/
| |--f/
| |--g.txt
| |--h.readme
|--C/
not this (which is what I want)
-----A/
|--B/
| |--f/
| |--g.txt
| |--h.readme
|--C/
any hints?
by the way I tried different ways of using "depth 1" for "step 2" with no success:
git fetch --depth 1 $(url) $(branch)
git checkout $(branch)
--> error: pathspec 'myBranch' did not match any file(s) known to git.
git fetch --depth 1 $(url) $(branch)
git checkout
--> fatal: You are on a branch yet to be born
git fetch --depth 1 $(url) $(branch)
git checkout -b $(branch)
fatal: A branch named 'myBranch' already exists.
git fetch
git checkout $(branch)
--> works as describe above
Git doesn't store directories, only files. So, given your drawing here:
----a/
|--b/
|--c/
|--d/
|--e/
|--f/
|--g.txt
|--h.readme
the repository has some commits that have files named:
a/d/e/f/g.txt
and:
a/d/e/h.readme
You can direct your Git to check out these specific files, using either command line options or the sparse checkout option you're attempting to use. Their names will still be a/d/e/f/g.txt and a/d/e/h.readme.
Your computer probably requires that these names be created by making a directory (or folders) named a, then making one named d in a, and so on. Git will do that. You cannot make Git use any other name for these files if you're going to use git checkout to extract them.1
So, that leaves you with the option of not using git checkout to get these files. You could get the commit (as you are doing already), then use git show on individual path names, for instance. The standard output of git show will show the file's contents; you can redirect this to a file name of your choice.
Since you don't want the whole repository, consider using git archive to turn one commit into an archive. Since you don't want the whole commit either, consider adding to this git archive command the name(s) of the file(s) that you want archived.2 You may then have an un-archiver that can skip the a/d/ part of the path names of each archived file. For instance, GNU tar has --strip-components.
1I think it would be nice to be able to remap stored file names to in-file-system names, for many reasons, including the case-folding issues and cheeky Linux programmers creating files named CON and LPT. :-) But Git can't, at least not today.
2This isn't required, and if you're downloading this kind of git archive from a hosting server such as GitHub, you may not be able to do that. But if you can do that, you'll save some amount of space and transfer time. Given your ssh://user#host URL, you can probably run ssh user#host "cd path/to/repo; git archive ..." and pipe the result to a local tar -xf -, for instance.

How can I archive old git tags and push my changes to origin?

I'd like to get rid of some old tags. But I don't want to delete them, I just want to archive them so that I can restore them if necessary.
I am refering to the answer of a smiliar question: How can I archive old git tags?
I have created a new folder ref/archive/tags and moved all affected tags to this folder.
Suppose I can't make the changes directly in Origin, but have to do it in my local repository. How can I push the changes then?
I'm using the following command:
git push origin refs/tags/:refs/tags/
refs/archive/tags/:refs/archive/tags/
And receive the following feedback:
Everything up-to-date
My colleagues can fetch the archive folder, but the old tags remain under refs/tags. What am I doing wrong?
Tags will not be deleted from your coworker's repo unless he deletes them. Just like you can't change a tag without deleting it first. The other option is to clone your repo to a new directory.
To delete all local tags:
for x in `git tag`; do
git tag -d $x
done
git remote update
At the end I used the following script. It archives all tags which are not named "test". Then all tags in the directory "refs/archive/tags" are deleted directly on the origin. Push the archive directory, delete all local tags and fetch the tags from origin. Maybe there are simpler solutions, but this one worked for me like this.
mkdir refs/archive/tags
shopt -s extglob
mv refs/tags/!(test) refs/archive/tags/
ls refs/archive/tags/ | xargs -n 1 -I% git push origin :refs/tags/%
git push origin refs/archive/tags/*:refs/archive/tags/*
git tag -l | xargs git tag -d
git fetch origin +refs/tags/*:refs/tags/*
My colleagues have to use the following script:
git tag -l | xargs git tag -d
git fetch origin +refs/archive/*:refs/archive/* +refs/tags/*:refs/tags/*
Restore all tags if neccessary:
mv refs/archive/tags/* refs/tags/
git push origin --tags

Git for Windows "No tags file" Response from "git diff" Command

Git version: 2.14.2.2
Whenever I run git diff on a repository I am greeted with the response No tags file. I have tried running the command on multiple repositories, multiple consoles (Cmd, PowerShell, MINGW64, Visual Studio Command Prompt) and all have the same response.
Strangely, the git log command also fails. Many other commands work, however, such as git status, git pull, etc. It seems to be only log and diff.
I have uninstalled Git entirely and reinstalled. Restarted my system. Tried referencing the git.exe directly (which yields the exact same response). Nothing is working and I have not seen this error anywhere else. I compared my user configs with those of a colleague and they are identical.
Some portion of the command executes properly, because if I supply two commit hashes, and I intentionally break one, the response I receive is:
It seems like another program may be hijacking the git diff command. I believe this because I'm not sure "No tags file" is even a possible Git response. Not sure what else it would be.
To make things even more confusing- my ultimate goal is to run the git diff within the context of an msbuild and it DOES EXECUTE CORRECTLY. Now, I could be satisfied with this, but I need to modify the diff command slightly, and running a full build each time is not productive, nor easy to troubleshoot. There is a task within the build script that runs an Exec command and it has no issues performing the diff. I'm also able to execute a Diff Against Current within SourceTree, which to the best of my knowledge, runs a git diff behind the scenes.
Any help would be very much appreciated.
:: Edits ::
Various commands:
git diff HEAD~1 HEAD
git diff master~1 master
git diff <commit-hash-1> <commit-hash-2>
git log HEAD~1..HEAD
git log master~1..master
git log <commit-hash-1>..<commit-hash-2>
Output:
Every one of the commands above returns the same No tags file response, in all of my repos.
Cat Head:
cd .git
cat HEAD
ls -R refs
Output:
New Repo:
mkdir testrepo
cd testrepo
git init
echo "file1" > file1.txt
git add .
git commit -m "initial commit of file1.txt"
echo "Hi there!" > file2.txt
git add .
git commit -m "added file2.txt"
git log
git diff HEAD~1 HEAD
Output:
git config -e:
git config --global -e:
::Edits 2::
I uninstalled all of my diffing/source control tools (SourceTree, Git, SVN, WinMerge, KDiff). Installed the portable version of Git. Opened CMD to a repo, put in full path to the git.exe portable and it still returned the No tags file response.
I also reviewed all of my path variables for: git, vim, ming, mintty and anything else that seemed suspect, but didn't find any.
I have restarted after performing all steps, and yet the problem persists.
::Edits 3::
I have a different user on my laptop, switched to that user and the git diff works properly, so clearly there is something with my main user that is conflicting. Will continue to look into my User directory for issues.
Here are the steps I'd take in this situation:
Try the following and check the response:
git diff HEAD~1 HEAD
git diff master~1 master
git diff <commit-hash-1> <commit-hash-2>
Try the same with log:
git log HEAD~1..HEAD
git log master~1..master
git log <commit-hash-1>..<commit-hash-2>
I'm actually guessing that your refs are messed up, which means that the direct hashes might work, but the HEAD and/or master one may not.
Look into the .git/refs folder
From the main repo folder:
cd .git
cat HEAD
ls -R refs
Hopefully, HEAD is pointing to a branch, and if master is checked out, cat HEAD output should look like:
ref: refs/heads/master
Then, the ls -R refs, should show a heads folder, with files for each of your local branches (i.e. master and possibly others). You also likely have refs/remotes and refs/tags directories.
If any of these things are radically different or missing, that could be your issue...
Since you have reinstalled git, create a brand new repo and try the same commands:
mkdir testrepo
cd testrepo
git init
echo "file1" > file1.txt
git add .
git commit -m "initial commit of file1.txt"
echo "Hi there!" > file2.txt
git add .
git commit -m "added file2.txt"
git log
git diff HEAD~1 HEAD
If this last one works, then git is likely working okay, but some tool you have is messing things up.
Post your config from git config -e and git config --global -e - maybe we can see something?
When googling for the "No tags file" message, the first results I get all talk about vi.
I do not understand why git would try to execute vi when running git diff or git log, could it be that your system is configured to use vi as a pager ?
# some possible places which could influence that :
echo $PAGER
echo $GIT_PAGER
git config --get core.pager
When digging in the documentation for less, I found that less can use a ctags file, to spot "the file that contains this tag".
So you can also look at the list of variables that influence the behavior of less :
# from bash :
# env will list all the defined environment variables
env
# the ones that impact 'less' should contain "LESS" in their names :
env | grep LESS

Resources