Download files from git repo on linux server - linux

I have seen so many articles/questions on how to download specific files from git repo, but none of them seems to match with my case.
What I need
Download spefic files from private git repository either using http's or ssh from linux server

Update - I originally wrote this as a general answer about git, and will preserve that info below. But I see you're talking about github, which does give some additional options.
For example, you could use a command like
curl https://raw.githubusercontent.com/eirslett/frontend-maven-plugin/master/README.md > README.md
to download just the README.md from the frontend-maven-plugin (which I happen to be looking at just now).
You mention yours is a private repo, so you'd have to deal with authentication; I suggest reading up on curl, as it should be able to provide credentials to the repo.
https://curl.haxx.se/docs/manpage.html
Original answer
There's not really a single git command for downloading an individual file from within a remote git repo. The closest I know you can do is this:
First clone the repo without checking out a work tree
git clone -n <repo-url>
Then check out just the file you want
git checkout master -- path/within/repo/to/file
But don't be fooled: This still downloads the entire repo. (That's just how git works.) So in addition to getting ./path/within/repo/to/file you also got ./.git containing all the rest of the data.

You can do in this hackish way: ö(å_å)ö
If this file is on github.com do this.:
wget https://example.githubproject.com/user/project/master/README
You can try git archive command:
git archive --format=tar --prefix=junk/ HEAD | (cd /var/tmp/ && tar xf -)
Create a tar archive that contains the contents of the latest commit on the current branch, and extract it in the /var/tmp/junk directory.
Example git archieve command usage:
git archive --format=tar --remote=origin HEAD | tar xf -
git archive --format=tar --remote=origin HEAD <file> | tar xf -
Tip in using, git archive, Examples.

I'm not sure what about your question is, but, have you did git clone <<url of git project>> ??

Related

Linux: How can I download a git-repos without cloning it?

I need a repository from Git for a project at work. Now our proxy blocks cloning from git, but on Windows, I still can download the repos as a .zip without git cloning it.
This project however runs on Linux and I need a repos for it, but of course git clone <repos-Link> doesn't work, due to our Proxy. Is there a way to just download the files so I can use them?
I'm looking for another way than downloading on Windows, and transferring the files manually
You can get the archive of a repo with wget
wget https://github.com/username/reponame/tarball/master
Note that this is not the whole git repository, just the files.
You can then extract it with tar -xvf master

Git ignore and changing the history (on Windows)

I've already read several posts about this here (like Git ignore & changing the past, How to remove files that are listed in the .gitignore but still on the repository?, and Applying .gitignore to committed files), but they have several problems:
Commands that only work on Linux.
Incomplete commands (like the first post I've linked to).
Only for one file.
I have pretty much no experience with Git so I was hoping for some help here.
What I'm basically trying to do is rescue one of my projects history. It's currently Hg and I converted it to Git with Hg-Git (all very easy) and it includes the history (great!). However, I also added a .gitignore file and added several new files & folders that I want completely gone from the history (like the bin and obj folders, but also files from ReSharper). So I'm looking for a way to apply the .gitignore file to all of my history. The commands should work on Windows as I have no intention of installing Linux for this.
No need to add the .gitignore in the history (there is no added value to do it), just add it for your future commits.
For the remove of files and directories in your history, use bfg-repo-cleaner which is fast, easy and works very well on Windows (done in scala).
It will do the job for you!
This is working for me:
Install hg-git.
cd HgFolder
hg bookmark -r default master
mkdir ../GitFolder
cd ../GitFolder
git init --bare
cd ../HgFolder
hg push ../GitFolder
Move all files from GitFolder to a '.git' folder (in this GitFolder) and set this folder to hidden (not the subfolders and files).
cd ../GitFolder
git init
git remote add origin https://url.git
Copy all current content (including .gitignore) to GitFolder.
git add .
git commit -m "Added existing content and .gitignore".
git filter-branch --index-filter "git rm --cache d -r --ignore-unmatch 'LINES' 'FROM' 'GITIGNORE'" --prune-empty --tag-name-filter cat -- --all
git rm -r --cached .
git add .
git gc --prune=now --aggressive
git push origin master --force
There is probably an easier way to do this and it might not be perfect but this had the result I wanted.

Git clone without including top/parent folder

We have a repo in git where the project is contained in a folder called Project. We'd like to be able to release the code to a production server, by cloning the repo, without including the "Project" folder, but with everything below it. Is this possible? The destination directory name is /var/www, which is unrelated to anything in the project. Unfortunately I can't just do a symbolic link because of the nature of our hosting provider (which we'll change soon).
My answer take the assumption that you have a git repository whose content is the following:
/.gitignore
/Project
/Project/index.php
/ProjectB
/ProjectB/pom.xml
If you don't need history at all in that copy of your repository, there is the git archive command which can do what you want except its output its data in tar or zip format:
git archive [--format=<fmt>] [--list] [--prefix=<prefix>/] [<extra>]
[-o <file> | --output=<file>] [--worktree-attributes]
[--remote=<repo> [--exec=<git-upload-archive>]] <tree-ish>
[<path>…]
Like:
git archive --format=zip --remote=git#foobar.git master -- Project | unzip
However, the git clone command does not accept a repository path, and I think it's not really git like to export only a tree view of some branch. You would probably need a submodule making Project an independent git repository, or like the git archive example, get only what you want but without versioning (which can be questionable on a production server).
Instead, you can do that:
Clone your repository to whatever path, say /opt/foobar.
Create a symbolic link of /opt/foobar/Project in /var/www.
Or reference the /opt/foobar/Project in your apache configuration (to avoid the symlink) instead of plain /var/www.

Unable to create a bare git repository

My git version is 1.5.0.6.
I want to create a bare git repository called sample.git.
I tried all sorts, but it failed:
Try 1:
Gives a usage statement but no git created
git init --bare sample.git
usage: git-init [--template=<template-directory>] [--shared]
Try 2: (using git-init not git init)
$ git-init --bare sample.git
usage: git-init [--template=<template-directory>] [--shared]
Try 3: Create a directory sample and then run same command, but still same output and git repository not created.
Please help me.
Firstly, upgrade your git. Version 1.5 is positively ancient.
If you can't upgrade, I'd just create a normal git repository, then manually convert it to a bare repo. An ordinary git repo will contain a '.git' directory. This can be your "bare" repo. Rename this directory to whatever you like. (You'll probably want to mv .git ../myrepo.git).
Then you have to tell git that it is a bare repo. Do this by adding bare = true to the config file in the [core] configuration section.

Exporting files from hook in bare git repository

So, my situation is the following: I want to maintain my website using emacs-muse mode. For transferring the website (and version control), I want to use git. So I would create a hook in the remote (bare) repository that automatically copies the HTML subdir to the web directory. How can I do that from a hook? Also note that the whole directory contains a lot of files, so I can't export the whole directory.
Any help welcome.
You can use git archive, as it takes a path command. So, in your post-update hook you can do something like
git archive $SHA HTML | (cd dir/where/html/should/go && tar x)
this first creates a tarball of the subdir, and pipes that tarball to a tar command to untar it in the specified directory
(just a suggestion, as it may not be applicable in your exact configuration)
You might consider adopting this Web-Focused Git Workflow which, instead of copying, does push your repo directly on the web directory.
(From Joe MALLER)
Less complicated: Using Git to maintain your website (Daniel MIESSLER):
Daniel MIESSLER has an updated version of that same process:
Other suggestions are available at this SO question, like this post-update script mentioned in the Git FAQ.

Resources