How to push without use git's command - linux

After merging one branch into another, (like develop into master) with
git merge --no-ff develop
If you execute the git command git status in a terminal just after you will see:
On branch master
Your branch is ahead of 'origin/master' by 1 commits.
(use "git push" to publish your local commit)
nothing to commit, working tree clean
Which is not much different from that (if we look without paying attention):
On branch master
nothing to commit, working tree clean
Sometimes I do not pay much attention and I forget to push. So is there an command to know if we must push? or a way to color the Your branch is ahead of 'origin/master' by 1 commits. part in red?
or may be just a command that return 0 or 1 if we need to push, if yes I could include it with an echo routine in a git alias to make my git status more explicit.

Finally, I resolved it by putting this in my ~/.gitconfig:
[alias]
st = "!f() { git status -u; \
git status -u | grep \"Your branch is ahead\" > /dev/null \
&& echo \"\\e[31m[WARNING]\\e[91m You need to push :)\"; }; f"
Like that just by performing git st I will be warned by a red message if I need to push.

Related

How to git clone/checkout a tag hash and only pull the specified folder/files? [duplicate]

I have my Git repository which, at the root, has two sub directories:
/finisht
/static
When this was in SVN, /finisht was checked out in one place, while /static was checked out elsewhere, like so:
svn co svn+ssh://admin#domain.example/home/admin/repos/finisht/static static
Is there a way to do this with Git?
What you are trying to do is called a sparse checkout, and that feature was added in Git 1.7.0 (Feb. 2012). The steps to do a sparse clone are as follows:
mkdir <repo>
cd <repo>
git init
git remote add -f origin <url>
This creates an empty repository with your remote, and fetches all objects but doesn't check them out. Then do:
git config core.sparseCheckout true
Now you need to define which files/folders you want to actually check out. This is done by listing them in .git/info/sparse-checkout, eg:
echo "some/dir/" >> .git/info/sparse-checkout
echo "another/sub/tree" >> .git/info/sparse-checkout
Last but not least, update your empty repo with the state from the remote:
git pull origin master
You will now have files "checked out" for some/dir and another/sub/tree on your file system (with those paths still), and no other paths present.
You might want to have a look at the extended tutorial and you should probably read the official documentation for sparse checkout and read-tree.
As a function:
function git_sparse_clone() (
rurl="$1" localdir="$2" && shift 2
mkdir -p "$localdir"
cd "$localdir"
git init
git remote add -f origin "$rurl"
git config core.sparseCheckout true
# Loops over remaining args
for i; do
echo "$i" >> .git/info/sparse-checkout
done
git pull origin master
)
Usage:
git_sparse_clone "http://github.com/tj/n" "./local/location" "/bin"
Note that this will still download the whole repository from the server – only the checkout is reduced in size. At the moment it is not possible to clone only a single directory. But if you don't need the history of the repository, you can at least save on bandwidth by creating a shallow clone. See udondan's answer below for information on how to combine shallow clone and sparse checkout.
As of Git 2.25.0 (Jan 2020) an experimental sparse-checkout command is added in Git:
git sparse-checkout init
# same as:
# git config core.sparseCheckout true
git sparse-checkout set "A/B"
# same as:
# echo "A/B" >> .git/info/sparse-checkout
git sparse-checkout list
# same as:
# cat .git/info/sparse-checkout
git clone --filter + git sparse-checkout downloads only the required files
E.g., to clone only files in subdirectory small/ in this test repository: https://github.com/cirosantilli/test-git-partial-clone-big-small
git clone --depth 1 --filter=blob:none --sparse \
https://github.com/cirosantilli/test-git-partial-clone-big-small
cd test-git-partial-clone-big-small
git sparse-checkout set small
The test repository contains:
a big/ subdirectory with 10x 10MB files
a small/ subdirectory with 1000 files of size one byte
All contents are pseudo-random and therefore incompressible.
Clone times on my 36.4 Mbps internet:
full: 24s
partial: "instantaneous"
Tested on git 2.30.0 on January 2021. Might work on Git 2.25 or 2.19.
The --filter option was added together with an update to the remote protocol, and it truly prevents objects from being downloaded from the server.
The sparse-checkout part is also needed unfortunately. You can also only download certain files with the much more understandable:
git clone --depth 1 --filter=blob:none --no-checkout \
https://github.com/cirosantilli/test-git-partial-clone-big-small
cd test-git-partial-clone-big-small
git checkout master -- d1
but that method for some reason downloads files one by one very slowly, making it unusable unless you have very few files in the directory.
A more minimal test repo can be seen at: https://github.com/cirosantilli/test-git-partial-clone
TODO: files on root directory are always downloaded
For example:
git clone --depth 1 --filter=blob:none --sparse \
https://github.com/cirosantilli/test-git-partial-clone-big-small
downloads the file generate.sh, and would also contain any other files on the root directory. Subdirectories are small/ and big/ are excluded, but not the root directory. How to prevent Git from downloading files in the root directory?
Asked at: How to prevent git clone --filter=blob:none --sparse from downloading files on the root directory?
Analysis of the objects in the minimal repository
The clone command obtains only:
a single commit object with the tip of the master branch
all 4 tree objects of the repository:
toplevel directory of commit
the the three directories d1, d2, master
Then, the git sparse-checkout set command fetches only the missing blobs (files) from the server:
d1/a
d1/b
Even better, later on GitHub will likely start supporting:
--filter=blob:none \
--filter=tree:0 \
where --filter=tree:0 from Git 2.20 will prevent the unnecessary clone fetch of all tree objects, and allow it to be deferred to checkout. But on my 2020-09-18 test that fails with:
fatal: invalid filter-spec 'combine:blob:none+tree:0'
presumably because the --filter=combine: composite filter (added in Git 2.24, implied by multiple --filter) is not yet implemented.
I observed which objects were fetched with:
git verify-pack -v .git/objects/pack/*.pack
as mentioned at: How to list ALL git objects in the database? It does not give me a super clear indication of what each object is exactly, but it does say the type of each object (commit, tree, blob), and since there are so few objects in that minimal repo, I can unambiguously deduce what each object is.
git rev-list --objects --all did produce clearer output with paths for tree/blobs, but it unfortunately fetches some objects when I run it, which makes it hard to determine what was fetched when, let me know if anyone has a better command.
TODO find GitHub announcement that saying when they started supporting it. https://github.blog/2020-01-17-bring-your-monorepo-down-to-size-with-sparse-checkout/ from 2020-01-17 already mentions --filter blob:none.
git sparse-checkout
I think this command is meant to manage a settings file that says "I only care about these subtrees" so that future commands will only affect those subtrees. But it is a bit hard to be sure because the current documentation is a bit... sparse ;-)
It does not, by itself, prevent the fetching of blobs.
If this understanding is correct, then this would be a good complement to git clone --filter described above, as it would prevent unintentional fetching of more objects if you intend to do git operations in the partial cloned repo.
When I tried on Git 2.25.1:
git clone \
--depth 1 \
--filter=blob:none \
--no-checkout \
https://github.com/cirosantilli/test-git-partial-clone \
;
cd test-git-partial-clone
git sparse-checkout init
it didn't work because the init actually fetched all objects.
However, in Git 2.28 it didn't fetch the objects as desired. But then if I do:
git sparse-checkout set d1
d1 is not fetched and checked out, even though this explicitly says it should: https://github.blog/2020-01-17-bring-your-monorepo-down-to-size-with-sparse-checkout/#sparse-checkout-and-partial-clones With disclaimer:
Keep an eye out for the partial clone feature to become generally available[1].
[1]: GitHub is still evaluating this feature internally while it’s enabled on a select few repositories (including the example used in this post). As the feature stabilizes and matures, we’ll keep you updated with its progress.
So yeah, it's just too hard to be certain at the moment, thanks in part to the joys of GitHub being closed source. But let's keep an eye on it.
Command breakdown
The server should be configured with:
git config --local uploadpack.allowfilter 1
git config --local uploadpack.allowanysha1inwant 1
Command breakdown:
--filter=blob:none skips all blobs, but still fetches all tree objects
--filter=tree:0 skips the unneeded trees: https://www.spinics.net/lists/git/msg342006.html
--depth 1 already implies --single-branch, see also: How do I clone a single branch in Git?
file://$(path) is required to overcome git clone protocol shenanigans: How to shallow clone a local git repository with a relative path?
--filter=combine:FILTER1+FILTER2 is the syntax to use multiple filters at once, trying to pass --filter for some reason fails with: "multiple filter-specs cannot be combined". This was added in Git 2.24 at e987df5fe62b8b29be4cdcdeb3704681ada2b29e "list-objects-filter: implement composite filters"
Edit: on Git 2.28, I experimentally see that --filter=FILTER1 --filter FILTER2 also has the same effect, since GitHub does not implement combine: yet as of 2020-09-18 and complains fatal: invalid filter-spec 'combine:blob:none+tree:0'. TODO introduced in which version?
The format of --filter is documented on man git-rev-list.
Docs on Git tree:
https://github.com/git/git/blob/v2.19.0/Documentation/technical/partial-clone.txt
https://github.com/git/git/blob/v2.19.0/Documentation/rev-list-options.txt#L720
https://github.com/git/git/blob/v2.19.0/t/t5616-partial-clone.sh
Test it out locally
The following script reproducibly generates the https://github.com/cirosantilli/test-git-partial-clone repository locally, does a local clone, and observes what was cloned:
#!/usr/bin/env bash
set -eu
list-objects() (
git rev-list --all --objects
echo "master commit SHA: $(git log -1 --format="%H")"
echo "mybranch commit SHA: $(git log -1 --format="%H")"
git ls-tree master
git ls-tree mybranch | grep mybranch
git ls-tree master~ | grep root
)
# Reproducibility.
export GIT_COMMITTER_NAME='a'
export GIT_COMMITTER_EMAIL='a'
export GIT_AUTHOR_NAME='a'
export GIT_AUTHOR_EMAIL='a'
export GIT_COMMITTER_DATE='2000-01-01T00:00:00+0000'
export GIT_AUTHOR_DATE='2000-01-01T00:00:00+0000'
rm -rf server_repo local_repo
mkdir server_repo
cd server_repo
# Create repo.
git init --quiet
git config --local uploadpack.allowfilter 1
git config --local uploadpack.allowanysha1inwant 1
# First commit.
# Directories present in all branches.
mkdir d1 d2
printf 'd1/a' > ./d1/a
printf 'd1/b' > ./d1/b
printf 'd2/a' > ./d2/a
printf 'd2/b' > ./d2/b
# Present only in root.
mkdir 'root'
printf 'root' > ./root/root
git add .
git commit -m 'root' --quiet
# Second commit only on master.
git rm --quiet -r ./root
mkdir 'master'
printf 'master' > ./master/master
git add .
git commit -m 'master commit' --quiet
# Second commit only on mybranch.
git checkout -b mybranch --quiet master~
git rm --quiet -r ./root
mkdir 'mybranch'
printf 'mybranch' > ./mybranch/mybranch
git add .
git commit -m 'mybranch commit' --quiet
echo "# List and identify all objects"
list-objects
echo
# Restore master.
git checkout --quiet master
cd ..
# Clone. Don't checkout for now, only .git/ dir.
git clone --depth 1 --quiet --no-checkout --filter=blob:none "file://$(pwd)/server_repo" local_repo
cd local_repo
# List missing objects from master.
echo "# Missing objects after --no-checkout"
git rev-list --all --quiet --objects --missing=print
echo
echo "# Git checkout fails without internet"
mv ../server_repo ../server_repo.off
! git checkout master
echo
echo "# Git checkout fetches the missing directory from internet"
mv ../server_repo.off ../server_repo
git checkout master -- d1/
echo
echo "# Missing objects after checking out d1"
git rev-list --all --quiet --objects --missing=print
GitHub upstream.
Output in Git v2.19.0:
# List and identify all objects
c6fcdfaf2b1462f809aecdad83a186eeec00f9c1
fc5e97944480982cfc180a6d6634699921ee63ec
7251a83be9a03161acde7b71a8fda9be19f47128
62d67bce3c672fe2b9065f372726a11e57bade7e
b64bf435a3e54c5208a1b70b7bcb0fc627463a75 d1
308150e8fddde043f3dbbb8573abb6af1df96e63 d1/a
f70a17f51b7b30fec48a32e4f19ac15e261fd1a4 d1/b
84de03c312dc741d0f2a66df7b2f168d823e122a d2
0975df9b39e23c15f63db194df7f45c76528bccb d2/a
41484c13520fcbb6e7243a26fdb1fc9405c08520 d2/b
7d5230379e4652f1b1da7ed1e78e0b8253e03ba3 master
8b25206ff90e9432f6f1a8600f87a7bd695a24af master/master
ef29f15c9a7c5417944cc09711b6a9ee51b01d89
19f7a4ca4a038aff89d803f017f76d2b66063043 mybranch
1b671b190e293aa091239b8b5e8c149411d00523 mybranch/mybranch
c3760bb1a0ece87cdbaf9a563c77a45e30a4e30e
a0234da53ec608b54813b4271fbf00ba5318b99f root
93ca1422a8da0a9effc465eccbcb17e23015542d root/root
master commit SHA: fc5e97944480982cfc180a6d6634699921ee63ec
mybranch commit SHA: fc5e97944480982cfc180a6d6634699921ee63ec
040000 tree b64bf435a3e54c5208a1b70b7bcb0fc627463a75 d1
040000 tree 84de03c312dc741d0f2a66df7b2f168d823e122a d2
040000 tree 7d5230379e4652f1b1da7ed1e78e0b8253e03ba3 master
040000 tree 19f7a4ca4a038aff89d803f017f76d2b66063043 mybranch
040000 tree a0234da53ec608b54813b4271fbf00ba5318b99f root
# Missing objects after --no-checkout
?f70a17f51b7b30fec48a32e4f19ac15e261fd1a4
?8b25206ff90e9432f6f1a8600f87a7bd695a24af
?41484c13520fcbb6e7243a26fdb1fc9405c08520
?0975df9b39e23c15f63db194df7f45c76528bccb
?308150e8fddde043f3dbbb8573abb6af1df96e63
# Git checkout fails without internet
fatal: '/home/ciro/bak/git/test-git-web-interface/other-test-repos/partial-clone.tmp/server_repo' does not appear to be a git repository
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
# Git checkout fetches the missing directory from internet
remote: Enumerating objects: 1, done.
remote: Counting objects: 100% (1/1), done.
remote: Total 1 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (1/1), 45 bytes | 45.00 KiB/s, done.
remote: Enumerating objects: 1, done.
remote: Counting objects: 100% (1/1), done.
remote: Total 1 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (1/1), 45 bytes | 45.00 KiB/s, done.
# Missing objects after checking out d1
?8b25206ff90e9432f6f1a8600f87a7bd695a24af
?41484c13520fcbb6e7243a26fdb1fc9405c08520
?0975df9b39e23c15f63db194df7f45c76528bccb
Conclusions: all blobs from outside of d1/ are missing. E.g. 0975df9b39e23c15f63db194df7f45c76528bccb, which is d2/b is not there after checking out d1/a.
Note that root/root and mybranch/mybranch are also missing, but --depth 1 hides that from the list of missing files. If you remove --depth 1, then they show on the list of missing files.
I have a dream
This feature could revolutionize Git.
Imagine having all the code base of your enterprise in a single monorepo without ugly third-party tools like repo.
Imagine storing huge blobs directly in the repo without any ugly third party extensions.
Imagine if GitHub would allow per file / directory metadata like stars and permissions, so you can store all your personal stuff under a single repo.
Imagine if submodules were treated exactly like regular directories: just request a tree SHA, and a DNS-like mechanism resolves your request, first looking on your local ~/.git, then first to closer servers (your enterprise's mirror / cache) and ending up on GitHub.
I have a dream.
EDIT: As of Git 2.19, this is finally possible, as can be seen in this answer.
Consider upvoting that answer.
Note: in Git 2.19, only client-side support is implemented, server-side support is still missing, so it only works when cloning local repositories. Also note that large Git hosters, e.g. GitHub, don't actually use the Git server, they use their own implementation, so even if support shows up in the Git server, it does not automatically mean that it works on Git hosters. (OTOH, since they don't use the Git server, they could implement it faster in their own implementations before it shows up in Git server.)
No, that's not possible in Git.
Implementing something like this in Git would be a substantial effort and it would mean that the integrity of the clientside repository could no longer be guaranteed. If you are interested, search for discussions on "sparse clone" and "sparse fetch" on the git mailinglist.
In general, the consensus in the Git community is that if you have several directories that are always checked out independently, then these are really two different projects and should live in two different repositories. You can glue them back together using Git Submodules.
You can combine the sparse checkout and the shallow clone features. The shallow clone cuts off the history and the sparse checkout only pulls the files matching your patterns.
git init <repo>
cd <repo>
git remote add origin <url>
git config core.sparsecheckout true
echo "finisht/*" >> .git/info/sparse-checkout
git pull --depth=1 origin master
You'll need minimum git 1.9 for this to work. Tested it myself only with 2.2.0 and 2.2.2.
This way you'll be still able to push, which is not possible with git archive.
For other users who just want to download a file/folder from github, simply use:
svn export <repo>/trunk/<folder>
e.g.
svn export https://github.com/lodash/lodash.com/trunk/docs
(yes, that's svn here. apparently in 2016 you still need svn to simply download some github files)
Courtesy: Download a single folder or directory from a GitHub repo
Important - Make sure you update the github URL and replace /tree/master/ with '/trunk/'.
As bash script:
git-download(){
folder=${#/tree\/master/trunk}
folder=${folder/blob\/master/trunk}
svn export $folder
}
Note
This method downloads a folder, does not clone/checkout it. You can't push changes back to the repository. On the other hand - this results in smaller download compared to sparse checkout or shallow checkout.
If you never plan to interact with the repository from which you cloned, you can do a full git clone and rewrite your repository using
git filter-branch --subdirectory-filter <subdirectory>
This way, at least the history will be preserved.
This looks far simpler:
git archive --remote=<repo_url> <branch> <path> | tar xvf -
Git 1.7.0 has “sparse checkouts”. See
“core.sparseCheckout” in the git config manpage,
“Sparse checkout” in the git read-tree manpage, and
“Skip-worktree bit” in the git update-index manpage.
The interface is not as convenient as SVN’s (e.g. there is no way to make a sparse checkout at the time of an initial clone), but the base functionality upon which simpler interfaces could be built is now available.
2022 Answer
I'm not sure why there are so many complicated answers to this question. It can be done easily by doing a sparse cloning of the repo, to the folder that you want.
Navigate to the folder where you'd like to clone the subdirectory.
Open cmd and run the following commands.
git clone --filter=blob:none --sparse %your-git-repo-url%
git sparse-checkout add %subdirectory-to-be-cloned%
cd %your-subdirectory%
Voila! Now you have cloned only the subdirectory that you want!
Explanation - What are these commands doing really?
git clone --filter=blob:none --sparse %your-git-repo-url%
In the above command,
--filter=blob:none => You tell git that you only want to clone the metadata files. This way git collects the basic branch details and other meta from remote, which will ensure that your future checkouts from origin are smooth.
--sparse => Tell git that this is a sparse clone. Git will checkout only the root directory in this case.
Now git is informed with the metadata and ready to checkout any subdirectories/files that you want to work with.
git sparse-checkout add gui-workspace ==> Checkout folder
git sparse-checkout add gui-workspace/assets/logo.png ==> Checkout a file
Sparse clone is particularly useful when there is a large repo with several subdirectories and you're not always working on them all. Saves a lot of time and bandwidth when you do a sparse clone on a large repo.
Additionally,
now in this partially cloned repo you can continue to checkout and work like you normally would. All these commands work perfectly.
git switch -c %new-branch-name% origin/%parent-branch-name% (or) git checkout -b %new-branch-name% origin/%parent-branch-name%
git commit -m "Initial changes in sparse clone branch"
git push origin %new-branch-name%
It's not possible to clone subdirectory only with Git, but below are few workarounds.
Filter branch
You may want to rewrite the repository to look as if trunk/public_html/ had been its project root, and discard all other history (using filter-branch), try on already checkout branch:
git filter-branch --subdirectory-filter trunk/public_html -- --all
Notes: The -- that separates filter-branch options from revision options, and the --all to rewrite all branches and tags. All information including original commit times or merge information will be preserved. This command honors .git/info/grafts file and refs in the refs/replace/ namespace, so if you have any grafts or replacement refs defined, running this command will make them permanent.
Warning! The rewritten history will have different object names for all the objects and will not converge with the original branch. You will not be able to easily push and distribute the rewritten branch on top of the original branch. Please do not use this command if you do not know the full implications, and avoid using it anyway, if a simple single commit would suffice to fix your problem.
Sparse checkout
Here are simple steps with sparse checkout approach which will populate the working directory sparsely, so you can tell Git which folder(s) or file(s) in the working directory are worth checking out.
Clone repository as usual (--no-checkout is optional):
git clone --no-checkout git#foo/bar.git
cd bar
You may skip this step, if you've your repository already cloned.
Hint: For large repos, consider shallow clone (--depth 1) to checkout only latest revision or/and --single-branch only.
Enable sparseCheckout option:
git config core.sparseCheckout true
Specify folder(s) for sparse checkout (without space at the end):
echo "trunk/public_html/*"> .git/info/sparse-checkout
or edit .git/info/sparse-checkout.
Checkout the branch (e.g. master):
git checkout master
Now you should have selected folders in your current directory.
You may consider symbolic links if you've too many levels of directories or filtering branch instead.
I wrote a script for downloading a subdirectory from GitHub.
Usage:
python get_git_sub_dir.py path/to/sub/dir <RECURSIVE>
This will clone a specific folder and remove all history not related to it.
git clone --single-branch -b {branch} git#github.com:{user}/{repo}.git
git filter-branch --subdirectory-filter {path/to/folder} HEAD
git remote remove origin
git remote add origin git#github.com:{user}/{new-repo}.git
git push -u origin master
here is what I do
git init
git sparse-checkout init
git sparse-checkout set "YOUR_DIR_PATH"
git remote add origin https://github.com/AUTH/REPO.git
git pull --depth 1 origin <SHA1_or_BRANCH_NAME>
Simple note
sparse-checkout
git sparse-checkout init many articles will tell you to set git sparse-checkout init --cone If I add --cone will get some files that I don't want.
git sparse-checkout set "..." will set the .git\info\sparse-checkout file contents as ...
Suppose you don't want to use this command. Instead, you can open the git\info\sparse-checkout and then edit.
Example
Suppose I want to get 2 folderfull repo size>10GB↑ (include git), as below total size < 2MB
chrome/common/extensions/api
chrome/common/extensions/permissions
git init
git sparse-checkout init
// git sparse-checkout set "chrome/common/extensions/api/"
start .git\info\sparse-checkout 👈 open the "sparse-checkut" file
/* .git\info\sparse-checkout for example you can input the contents as below 👇
chrome/common/extensions/api/
!chrome/common/extensions/api/commands/ 👈 ! unwanted : https://www.git-scm.com/docs/git-sparse-checkout#_full_pattern_set
!chrome/common/extensions/api/devtools/
chrome/common/extensions/permissions/
*/
git remote add origin https://github.com/chromium/chromium.git
start .git\config
/* .git\config
[core]
repositoryformatversion = 1
filemode = false
bare = false
logallrefupdates = true
symlinks = false
ignorecase = true
[extensions]
worktreeConfig = true
[remote "origin"]
url = https://github.com/chromium/chromium.git
fetch = +refs/heads/*:refs/remotes/Github/*
partialclonefilter = blob:none // 👈 Add this line, This is important. Otherwise, your ".git" folder is still large (about 1GB)
*/
git pull --depth 1 origin 2d4a97f1ed2dd875557849b4281c599a7ffaba03
// or
// git pull --depth 1 origin master
partialclonefilter = blob:none
I know to add this line because I know from: git clone --filter=blob:none it will write this line. so I imitate it.
git version: git version 2.29.2.windows.3
Using Linux? And only want easy to access and clean working tree ? without bothering rest of code on your machine. try symlinks!
git clone https://github.com:{user}/{repo}.git ~/my-project
ln -s ~/my-project/my-subfolder ~/Desktop/my-subfolder
Test
cd ~/Desktop/my-subfolder
git status
Just to clarify some of the great answers here, the steps outlined in many of the answers assume that you already have a remote repository somewhere.
Given: an existing git repository, e.g. git#github.com:some-user/full-repo.git, with one or more directories that you wish to pull independently of the rest of the repo, e.g. directories named app1 and app2
Assuming you have a git repository as the above...
Then: you can run steps like the following to pull only specific directories from that larger repo:
mkdir app1
cd app1
git init
git remote add origin git#github.com:some-user/full-repo.git
git config core.sparsecheckout true
echo "app1/" >> .git/info/sparse-checkout
git pull origin master
I had mistakenly thought that the sparse-checkout options had to be set on the original repository, but this is not the case: you define which directories you want locally, prior to pulling from the remote. The remote repo doesn't know or care about your only wanting to track a part of the repo.
Hope this clarification helps someone else.
Here's a shell script I wrote for the use case of a single subdirectory sparse checkout
coSubDir.sh
localRepo=$1
remoteRepo=$2
subDir=$3
# Create local repository for subdirectory checkout, make it hidden to avoid having to drill down to the subfolder
mkdir ./.$localRepo
cd ./.$localRepo
git init
git remote add -f origin $remoteRepo
git config core.sparseCheckout true
# Add the subdirectory of interest to the sparse checkout.
echo $subDir >> .git/info/sparse-checkout
git pull origin master
# Create convenience symlink to the subdirectory of interest
cd ..
ln -s ./.$localRepo/$subDir $localRepo
It worked for me- (git version 2.35.1)
git init
git remote add origin <YourRepoUrl>
git config core.sparseCheckout true
git sparse-checkout set <YourSubfolderName>
git pull origin <YourBranchName>
I wrote a .gitconfig [alias] for performing a "sparse checkout". Check it out (no pun intended):
On Windows run in cmd.exe
git config --global alias.sparse-checkout "!f(){ [ $# -eq 2 ] && L=${1##*/} L=${L%.git} || L=$2; mkdir -p \"$L/.git/info\" && cd \"$L\" && git init --template= && git remote add origin \"$1\" && git config core.sparseCheckout 1; [ $# -eq 2 ] && echo \"$2\" >> .git/info/sparse-checkout || { shift 2; for i; do echo $i >> .git/info/sparse-checkout; done }; git pull --depth 1 origin master;};f"
Otherwise:
git config --global alias.sparse-checkout '!f(){ [ $# -eq 2 ] && L=${1##*/} L=${L%.git} || L=$2; mkdir -p "$L/.git/info" && cd "$L" && git init --template= && git remote add origin "$1" && git config core.sparseCheckout 1; [ $# -eq 2 ] && echo "$2" >> .git/info/sparse-checkout || { shift 2; for i; do echo $i >> .git/info/sparse-checkout; done }; git pull --depth 1 origin master;};f'
Usage:
# Makes a directory ForStackExchange with Plug checked out
git sparse-checkout https://github.com/YenForYang/ForStackExchange Plug
# To do more than 1 directory, you have to specify the local directory:
git sparse-checkout https://github.com/YenForYang/ForStackExchange ForStackExchange Plug Folder
The git config commands are 'minified' for convenience and storage, but here is the alias expanded:
# Note the --template= is for disabling templates.
# Feel free to remove it if you don't have issues with them (like I did)
# `mkdir` makes the .git/info directory ahead of time, as I've found it missing sometimes for some reason
f(){
[ "$#" -eq 2 ] && L="${1##*/}" L=${L%.git} || L=$2;
mkdir -p "$L/.git/info"
&& cd "$L"
&& git init --template=
&& git remote add origin "$1"
&& git config core.sparseCheckout 1;
[ "$#" -eq 2 ]
&& echo "$2" >> .git/info/sparse-checkout
|| {
shift 2;
for i; do
echo $i >> .git/info/sparse-checkout;
done
};
git pull --depth 1 origin master;
};
f
Lots of great responses here, but I wanted to add that using the quotations around the directory names was failing for me on Windows Sever 2016. The files simply were not being downloaded.
Instead of
"mydir/myfolder"
I had to use
mydir/myfolder
Also, if you want to simply download all sub directories just use
git sparse-checkout set *
git init <repo>
cd <repo>
git remote add origin <url>
git config core.sparsecheckout true
echo "<path you want to clone>/*" >> .git/info/sparse-checkout
git pull --depth=1 origin <branch you want to fetch>
Example for cloning only Jetsurvey Folder from this repo
git init MyFolder
cd MyFolder
git remote add origin git#github.com:android/compose-samples.git
git config core.sparsecheckout true
echo "Jetsurvey/*" >> .git/info/sparse-checkout
git pull --depth=1 origin main
#Chronial 's anwser is no longer applicable to recent versions, but it was a useful anwser as it proposed a script.
Given information I gathered and the fact that I wanted to checkout only a subdirectory of a branch, I created the following shell function. It gets a shallow copy of only the most recent version in the branch for the provided directories.
function git_sparse_clone_branch() (
rurl="$1" localdir="$2" branch="$3" && shift 3
git clone "$rurl" --branch "$branch" --no-checkout "$localdir" --depth 1 # limit history
cd "$localdir"
# git sparse-checkout init --cone # fetch only root file
# Loops over remaining args
for i; do
git sparse-checkout set "$i"
done
git checkout "$branch"
)
So example use:
git_sparse_clone_branch git#github.com:user/repo.git localpath branch-to-clone path1_to_fetch path2_to_fetch
In my case the clone was "only" 23MB versus 385MB for the full clone.
Tested with git version 2.36.1 .
If you're actually ony interested in the latest revision files of a directory, Github lets you download a repository as Zip file, which does not contain history. So downloading is very much faster.
While I hate actually having to use svn when dealing with git repos :/ I use this all the time;
function git-scp() (
URL="$1" && shift 1
svn export ${URL/blob\/master/trunk}
)
This allows you to copy out from the github url without modification. Usage;
--- /tmp » git-scp https://github.com/dgraph-io/dgraph/blob/master/contrib/config/kubernetes/helm 1 ↵
A helm
A helm/Chart.yaml
A helm/README.md
A helm/values.yaml
Exported revision 6367.
--- /tmp » ls | grep helm
Permissions Size User Date Modified Name
drwxr-xr-x - anthony 2020-01-07 15:53 helm/
Lots of good ideas and scripts above. I could not help myself and combined them into a bash script with help and error checking:
#!/bin/bash
function help {
printf "$1
Clones a specific directory from the master branch of a git repository.
Syntax:
$(basename $0) [--delrepo] repoUrl sourceDirectory [targetDirectory]
If targetDirectory is not specified it will be set to sourceDirectory.
Downloads a sourceDirectory from a Git repository into targetdirectory.
If targetDirectory is not specified, a directory named after `basename sourceDirectory`
will be created under the current directory.
If --delrepo is specified then the .git subdirectory in the clone will be removed after cloning.
Example 1:
Clone the tree/master/django/conf/app_template directory from the master branch of
git#github.com:django/django.git into ./app_template:
\$ $(basename $0) git#github.com:django/django.git django/conf/app_template
\$ ls app_template/django/conf/app_template/
__init__.py-tpl admin.py-tpl apps.py-tpl migrations models.py-tpl tests.py-tpl views.py-tpl
Example 2:
Clone the django/conf/app_template directory from the master branch of
https://github.com/django/django/tree/master/django/conf/app_template into ~/test:
\$ $(basename $0) git#github.com:django/django.git django/conf/app_template ~/test
\$ ls test/django/conf/app_template/
__init__.py-tpl admin.py-tpl apps.py-tpl migrations models.py-tpl tests.py-tpl views.py-tpl
"
exit 1
}
if [ -z "$1" ]; then help "Error: repoUrl was not specified.\n"; fi
if [ -z "$2" ]; then help "Error: sourceDirectory was not specified."; fi
if [ "$1" == --delrepo ]; then
DEL_REPO=true
shift
fi
REPO_URL="$1"
SOURCE_DIRECTORY="$2"
if [ "$3" ]; then
TARGET_DIRECTORY="$3"
else
TARGET_DIRECTORY="$(basename $2)"
fi
echo "Cloning into $TARGET_DIRECTORY"
mkdir -p "$TARGET_DIRECTORY"
cd "$TARGET_DIRECTORY"
git init
git remote add origin -f "$REPO_URL"
git config core.sparseCheckout true
echo "$SOURCE_DIRECTORY" > .git/info/sparse-checkout
git pull --depth=1 origin master
if [ "$DEL_REPO" ]; then rm -rf .git; fi
degit makes copies of git repositories. When you run degit
some-user/some-repo, it will find the latest commit on
https://github.com/some-user/some-repo and download the associated tar
file to ~/.degit/some-user/some-repo/commithash.tar.gz if it doesn't
already exist locally. (This is much quicker than using git clone,
because you're not downloading the entire git history.)
degit <https://github.com/user/repo/subdirectory> <output folder>
Find out more https://www.npmjs.com/package/degit
You can still use svn:
svn export https://admin#domain.example/home/admin/repos/finisht/static static --force
to "git clone" a subdirectory and then to "git pull" this subdirectory.
(It is not intended to commit & push.)
(extending this answer )
Cloning subdirectory in specific tag
If you want to clone a specific subdirectory of a specific tag you can follow the steps below.
I clone the distribution/src/main/release/samples/ subdirectory of cxf github repo in cxf-3.5.4 tag.
Note: if you attempt to just clone the above repo you will see that it is very big. The commands below clones only what is needed.
git clone --depth 1 --filter=blob:none --sparse https://github.com/apache/cxf
cd cxf/
git sparse-checkout set distribution/src/main/release/samples/
git fetch --depth 1 origin cxf-3.5.4
# This is the hash on which the tag points, however using the tag does not work.
git switch --detach 3ef4fde
Cloning subdirectory in specific branch
I clone the distribution/src/main/release/samples/ subdirectory of cxf github repo in 2.6.x-fixes branch.
git clone --depth 1 --filter=blob:none --sparse https://github.com/apache/cxf --branch 2.6.x-fixes
cd cxf/
git sparse-checkout set distribution/src/main/release/samples/
If you want to clone
git clone --no-checkout <REPOSITORY_URL>
cd <REPOSITORY_NAME>
Now set the specific file / directory you wish to pull into the working-directory:
git sparse-checkout set <PATH_TO_A_SPECIFIC_DIRECTORY_OR_FILE_TO_PULL>
Afterwards, you should reset hard your working-directory to the commit you wish to pull.
For example, we will reset it to the default origin/master's HEAD commit.
git reset --hard HEAD
If you want to git init and then remote add
git init
git remote add origin <REPOSITORY_URL>
Now set the specific file / directory you wish to pull into the working-directory:
git sparse-checkout set <PATH_TO_A_SPECIFIC_DIRECTORY_OR_FILE_TO_PULL>
Pull the last commit:
git pull origin master
NOTE:
If you want to add another directory/file to your working-directory, you may do it like so:
git sparse-checkout add <PATH_TO_ANOTHER_SPECIFIC_DIRECTORY_OR_FILE_TO_PULL>
If you want to add all the repository to the working-directory, do it like so:
git sparse-checkout add *
If you want to empty the working-directory, do it like so:
git sparse-checkout set empty
If you want, you can view the status of the tracked files you have specified, by running:
git status
If you want to exit the sparse mode and clone all the repository, you should run:
git sparse-checkout set *
git sparse-checkout set init
git sparse-checkout set disable
I don't know if anyone succeeded pulling specific directory, here is my experience: git clone --filter=blob:none --single-branch <repo>, cancel immediately while downloading objects, enter repo, then git checkout origin/master <dir>, ignore errors (sha1), enter dir, repeat checkout (using new dir) for every sub-directory. I managed to quickly get source files in this way
For macOS users
For zsh users (macOS users, specifically) cloning Repos with ssh, I just create a zsh command based on the answer by #Ciro Santilli:
requirement: The version of git matters. It doesn't work on 2.25.1 because of the --sparse option. Try upgrade your git to the latest version. (e.g. tested 2.36.1)
example usage:
git clone git#github.com:google-research/google-research.git etcmodel
code:
function gitclone {
readonly repo_root=${1?Usage: gitclone repo.git sub_dir}
readonly repo_sub=${2?Usage: gitclone repo.git sub_dir}
echo "-- Cloning $repo_root/$repo_sub"
git clone \
--depth 1 \
--filter=tree:0 \
--sparse \
$repo_root \
;
repo_folder=${repo_root#*/}
repo_folder=${repo_folder%.*}
cd $repo_folder
git sparse-checkout set $repo_sub
cd -
}
gitclone "$#"

Make "git pull" ask for confirmation when pulling different branch

When working with many projects and branches at the same time, I occasionally do stupid mistake like pulling into the wrong branch. For example being on branch master I did git pull origin dangerous_code and didn't notice that for quite some time. This small mistake caused a lot of mess.
Is there any way to make git ask for confirmation when I attempt to pull a branch other than branch that is currently checked out? Basically I want it to ask for confirmation if the branch name doesn't match (checked out and the one being pulled).
For now, I'll focus on how to prompt the user for confirmation before any pull is carried out.
Unfortunately, because there is no such thing as a pre-pull hook, I don't think you can get the actual pull command to directly do that for you. As I see it, you have two options:
1 - Use fetch then merge (instead of pull)
Instead of running git pull, run git fetch, then git merge or git rebase; breaking down pull into the two steps it naturally consists of will force you to double-check what you're about to merge/rebase into what.
2 - Define an alias that asks for confirmation before a pull
Define and use a pull wrapper (as a Git alias) that prompts you for confirmation if you attempt to pull from a remote branch whose name is different from the current local branch.
Write the following lines to a script file called git-cpull.sh (for confirm, then pull) in ~/bin/:
#!/bin/sh
# git-cpull.sh
if [ "$2" != "$(git symbolic-ref --short HEAD)" ]
then
while true; do
read -p "Are you sure about this pull?" yn
case "$yn" in
[Yy]*)
git pull $#;
break
;;
[Nn]*)
exit
;;
*)
printf %s\\n "Please answer yes or no."
esac
done
else
git pull $#
fi
Then define the alias:
git config --global alias.cpull '!sh git-cpull.sh'
After that, if, for example, you run
git cpull origin master
but the current branch is not master, you'll be asked for confirmation before any pull is actually carried out.
Example
$ git branch
* master
$ git cpull origin foobar
Are you sure about this pull?n
$ git cpull origin master
From https://github.com/git/git
* branch master -> FETCH_HEAD
Already up-to-date.

Git: reset and checkout in one shot with && doesn't work

Simple git question. After executing:
git reset HEAD file && git checkout -- file
The file is still under "Changes not staged for commit" section, but it shouldn't. If I execute the two operations seperately (ie. pressing enter and seeing git status between and after them), it works.
Platform: Linux amd64, git version 1.8.1.3
This is because git reset returns a non-zero exit code when the file has unstaged commits after the reset. So the && prevents the git checkout from running -- it will only run the second command if the first "succeeds" (i.e. exits with zero).
git reset HEAD file just updates the index (i.e., any staged changes to the file are lost), the changed file stays as is.
Why do it this way, if a simple git checkout file (perhaps with -f if file has been changed) acomplishes the same?

How to "attach" working dir to bare GIT repository

I have embedded Linux system that we want to store in Git. I have installed Git on the system, mount additional USB drive for storing Git data (bare repository). There is no problem with committing and pushing to the remote repository using commands like that:
cd /media/usb
git init --bare
git --work-tree=/ add -A
git --work-tree=/ commit
git --work-tree=/ push -u origin master
But when I clone bare repository to new USB drive and invoke git --work-tree=/ status I see all previously pushed files as deleted, and untracked. How to tell Git to use the work-tree?
The reason you are seeing previously committed files as deleted is that the git index (which is simply a file called index) in the first repository differs from the index in the second repository. The index in the first corresponds to the working tree, whereas the index in the second is uninitialized and therefore has no entries. The output from git status is the result of two comparisons:
between HEAD and the index (to determine staged changes to be committed)
between the index and the working tree (to determine unstaged changes which will not be committed)
In your case, HEAD in the second repository points to a commit which contains all the files you committed from your root filesystem, but the index is empty. So when git performs the first comparison, it thinks that each of these files has been staged for deletion on the next commit.
When git performs the second comparison, it finds that the working tree contains all the same files as the commit, but the index is of course still empty, so it sees these files as "new" untracked files. That is why you see all the files as both deleted and untracked.
The solution is very simple: initialize the second index so that it matches master:
git --work-tree=/ reset
While I'm here, I should point out some other issues with the commands you posted:
Firstly, your git add -U is adding all the git repository meta-data files to the repository. In other words, the repository is tracking itself. This is happening as a consequence of the way you use --work-tree, and is very bad. You should ensure that the repository files are ignored by adding them to info/exclude or .gitignore.
Secondly, you don't really want a bare repository here, just a detached working tree. You could have achieved this via git config core.bare false and export GIT_DIR=/media/usb; then you could run git commands from outside (i.e. above /media/usb), and you wouldn't have to continually include --work-tree=/ as a global option in each command.
Here's a complete test case which encapsulates everything I just covered except for the second bullet point:
#!/bin/sh
root=fakeroot
mkdir -p $root/media/usb{1,2} $root/{bin,etc}
echo a > $root/bin/sh
echo b > $root/etc/hosts
cd $root/media/usb1
git init --bare
# We don't want our git repository meta-data being tracked.
echo '/media/usb*/' >> info/exclude
git --work-tree=../.. add -A ../..
git --work-tree=../.. commit -m '1st commit'
echo c >> ../../etc/hosts
git --work-tree=../.. add -A ../..
git --work-tree=../.. commit -m '2nd commit'
git remote add origin ../usb2
git --git-dir=../usb2 init --bare
git push origin master
cd ../usb2
echo '/media/usb*/' >> info/exclude
echo "========================================="
echo "index in usb2 is not yet initialized:"
git --work-tree=../.. status
echo "========================================="
echo "initialize index to master (HEAD)"
git --work-tree=../.. reset
echo "========================================="
echo "now we have a clean working tree:"
git --work-tree=../.. status

How to apply a git patch on a non-checked out branch?

I am wondering if it is possible to apply a git patch on a non-checked out branch?
I would like to do this because I don't want this patch on my working branch, but on a dedicated branch that I don't use right now. This is a large branch, and checking it out will:
make my exuberant tags useless or broken. It takes long to rebuild them.
also, my kernel will force rebuild if I checkout this branch and roll-back to my working branch...
It's extremely indirect, but possible. You can do it by applying it only to the index.
The easy way:
$ git read-tree <branch>
$ git apply --cached <patch>
$ git update-ref refs/heads/<branch> \
-m "<optional reflog message>" \
$(git commit-tree $(git write-tree) -m "<message>" -p <branch>)
If you want everything to be "cleaner" (i.e. have the reflog look normal for the commit), then here's a longer way, considerably more verbose:
$ git checkout -b temp # Create a temp branch
$ git reset --mixed <branch> # Reset the index to the branch you want
$ git apply --cached <patch> # Apply the patch to the index
$ git commit # Create a commit object
# Move the branch's reference to the new commit
$ git update-ref refs/heads/<branch> refs/heads/temp
# Clean up
$ git checkout --force <original_branch>
$ git branch -d temp
Can you clone from your current repository to another directory, and check out the branch you want to work on there? AFAIK, you can only apply a patch, or cherry pick commits, etc. to the files in your working directory.
For some of the projects I work on, I have multiple copies of a repository on my system, with different branches checked out on each.
I don't think this is possible mainly because of this reason:
How will you deal with a patch failure?

Resources