Tips

shallow git submodules

git submodule add --depth 1 -- repository path
git submodule update --depth -- [<path>...]

--depth option is valid for add and update commands. Create a ‘shallow’ clone with a history truncated to the specified number of revisions.

For Git 2.9.0 and up, you can do this: git clone --depth=1 --recursive --shallow-submodules repo_url

And git 2.10 Q3 2016 allows to record that with git config -f .gitmodules submodule.<name>.shallow true.

Ref: How to make shallow git submodules?

error: Server does not allow request for unadvertised object

git submodule update --force --recursive --init --remote

Why does git fail to fetch specific valid submodule for a given commit and how to fix it?

Another solution is, fetching enough history from the server, so the commit is included in this history.

There are some options in git fetch can help you do this:

  • --deepen=<depth>
  • --shallow-since=<date>
  • --shallow-exclude=<revision>

Shallow clone a branch

git clone --depth 1 https://path/to/repo/foo.git -b bar

Ref: How do I shallow clone a repo on a specific branch?

Shallow fetch a tag

git fetch --update-shallow --depth=1 origin 1.2.1

list remote heads

if git branch -r doesn’t work, try:

git ls-remote --heads <remote-name>

Source: How do I list all remote branches in Git 1.7+?

delete a remote branch

git push <repo_name> :<branch_name>

exclude files from git grep

git grep XXX -- './*' ':!*/test/*'

How to exclude certain directories/files from git grep search

If you use Git for Windows in cmd.exe, ‘"’ should be used as the quote

git grep xxx -- "./*" ":!*/test/*"

You can put the above in a script named git-grep1, with the following content:

#!/usr/bin/bash

git grep -n "$@" -- './*' ':!*/test/*'

Put the script some where in PATH, so that git can find it. Therefore you can use git grep1 xxx.

The script works even for Git for Windows.

update all git repo in one directory

in shell, execute:

for d in */; do pushd $d; echo $d; git pull; popd; done

clone without lfs

Two alternatives:

#1) Using the GIT_LFS_SKIP_SMUDGE variable:

GIT_LFS_SKIP_SMUDGE=1 git clone SERVER-REPOSITORY

# 2) Configuring the git-lfs smudge:

git config --global filter.lfs.smudge "git-lfs smudge --skip"
git clone SERVER-REPOSITORY

# To undo this configuration, execute:

git config --global filter.lfs.smudge "git-lfs smudge -- %f"

How to clone/pull a git repository, ignoring LFS?

sparse-checkout

turn on sparse-checkout: git config core.sparseCheckout true

What to check out is controlled by .git/info/sparse-checkout.

/*
!**/test/**

The above example excludes all test directories from the checkout.

ref: Is it possible to do a sparse checkout without checking out the whole repository first?, also gitglossary pathspec

git orphan branch

git checkout --orphan YourBranchName

[How to create a new (and empty!) “root” branch?])(https://stackoverflow.com/questions/15034390/how-to-create-a-new-and-empty-root-branch)

git for windows

  • Line ending problem git config --global core.autocrlf false, source

make a fold case sensitive in Windows 10

Window 10 introduces support for case sensitive folder, so that in such a folder “Hello.txt” and “hello.txt” are too different files.

You will need an admin cmd.exe and run fsutil.exe file setCaseSensitiveInfo, if your Winwodws doesn’t support it yet, it will show setCaseSensitiveInfo is an invalid parameter..

Otherwise, use fsutil.exe file setCaseSensitiveInfo "full path to your folder" enable to turn of case sensitivity on a folder.

note that case sensitivity can not be inherited by sub folders.

ref: Enable Case Sensitive Mode for Folders in Windows 10

How to get the changes on a branch in Git

git diff HEAD...branch
git cherry branch [newbranch]
git diff --name-status branch [newbranch]
git diff `git merge-base feature-branch master` feature-branch

other refs:

rewrite history

Example

  • git filter-repo --invert-paths --path '.DS_Store' --use-base-name

rewriting history may go wrong, should check the resulting history to confirm the effect.

Git Client

  • tig. Text interface for Git repositories

Git for Windows: HTTP Basic: Access denied and fatal Authentication

HTTP Basic: Access denied

GitLab remote: HTTP Basic: Access denied and fatal Authentication

git show latest commits

git log -10 --all --date-order

For last 10 commits in all branches, you can do:

git log --graph --all --format=format:'%h - (%ai) %s — %cn %d' --abbrev-commit --date=relative -10

How to find the latest commits in one git repository?

git-lfs exclude sub-folders

*.asset filter=lfs diff=lfs merge=lfs -text
Assets/Resources/*.asset -filter=lfs -diff=lfs -merge=lfs -text

How to make git LFS not apply to a subdirectory

Git LFS track folder recursively

pageant

  • Use klink from Kitty, instead of plink from Putty. Klink has option “-auto-store-sshkey”
  • Use GIT_SSH_COMMAND instead of GIT_SSH

GIT_SSH_COMMAND: C:\\path\\to\\klink.exe -auto-store-sshkey

or maybe

GIT_SSH_COMMAND: C:/path/to/klink.exe -auto-store-sshkey

.netrc

.netrc can be used to provide HTTP basic asscess to git when accessing repo through http.

example .netrc:

machina gitlab.com/username/project login username password password_generated_by_gitlab_for_the_project

noproxy

When a http_proxy is set, and you would like to by pass the proxy for some hosts:

no_proxy=localhost,127.0.0.0,127.0.1.1,127.0.1.1,local.home

see: Setting up proxy to ignore all local addresses [duplicate]

github access token

otherwise errors fatal: this operation must be run in a work tree

This could happen in a bare repo, for example, created with git init --bare repo

The remedy would be to set bare to false for the repo.

gitdir

.git doesn’t have to be a directory containing the repo, it could be a file pointing to another directory in which the repo holds.

All you need to do is identify the path (absolute or relative to current path) in the .git file like:

gitdir: path/to/directory

Move branch pointer to different commit without checkout

基本上有三种方法

git reset –hard new-tip-commit

git branch –force []

git update-ref -m “reset: Reset to

(末尾)