Use git shortcuts with a new version, overriding old - linux

I'm using git on a shared hosting plan which runs a Linux distribution. Because it's shared, I don't have access to sudo. The machine already has git version 1.7.1 installed in /usr/bin/, but this version is rather prehistoric at this point. I've already used make install to get the current version 2.14.1, which is in ~/git-2.14.1/. As detailed by this answer, I can access the correct version of git from the command line like so:
$ git --version
git version 2.14.1
which was a simple change to the $PATH variable, in the ~/.bash_profile, so I can use git just fine.
I have a list of shortcuts in my ~/.gitconfig file that make git much faster for me to use:
[alias]
co = checkout
st = status
ci = commit
... etc etc etc
When I invoke these (e.g. git st), I get the results from the wrong version of git. It goes back to 1.7.1. If I type out the full command (e.g. git status), it uses the correct version of git.
I also have these commands in my ~./bashrc:
git () {
case "$*" in
st* ) shift 1; command ~/git-2.14.1/git status "$#" ;;
* ) shift 1; command ~/git-2.14.1/git "$#" ;;
esac
}
Which I have source-ed since writing.
I have also tried:
alias git="~/git-2.14.1/git"
To no avail.
Is there a work-around that would allow me to use these shortcuts with my preferred version of git? I am imagining a way to hide the config file from the other version by redirecting the pointer to a different location, but I have no knowledge of the existence of any such pointer.
Alternatively, is there a way to totally disable the previous version of git without root access?

When I invoke these (e.g. git st), I get the results from the wrong version of git. It goes back to 1.7.1
That is not what I see in my Ubuntu session.
I have in my .gitconfig
[alias]
st = status
br = branch
v = !git version
That means I type git v, to check git version (which is the same as git --version).
If I change my PATH to a new compiled Git, I do see a different version.
vonc#VONC:~/gits$ echo $PATH
/usr/local/bin:/usr/bin:/bin
vonc#VONC:~/gits$ which git
/usr/bin/git
vonc#VONCAVN7:~/gits$ git v
git version 2.13.0
vonc#VONC:~/gits$ export PATH=~/gits/v2.14.0/bin:$PATH
vonc#VONC:~/gits$ git v
git version 2.14.0

Here's what I found. If I took out the alias section of the .gitconfig, the aforementioned shell wrapper function worked. If I removed that and restarted the ssh connection, nothing worked (as expected).
I found, with help from this answer, that there is a magical GIT_EXEC_PATH variable which tells git where to find tools like git-add, git-commit, git-status, et. al.. Uncommenting my alias section of the .gitconfig file and using
$ export GIT_EXEC_PATH=/path/to/my/new/git's/executables/
made it possible to use the aliases in the .gitconfig file. For me the path that worked was the same folder as I installed into: ~/git-2.14.1.You can check the current value of GIT_EXEC_PATH with:
$ git --exec-path
/path/to/my/new/git's/executables/
Also, echo $GIT_EXEC_PATH works as well. My understanding is that git uses its aliases not through the instance of git which called git <alias>, but rather directly through the executables in it's exec path.
Note: for me,
$ git --exec-path=/path/to/my...
did not work, but if I understand correctly, this is the equivalent command.
TL;DR: add the path to the new executables (git-add, git-status, etc.) in your .bash_profile:
export GIT_EXEC_PATH=/your/path/

Related

Git branch not displaying any results

When I run git branch -a in a folder with a .git directory, NOTHING is returned. I have been working in this directory for a few months and git branch used to return all local and remote branches.
My PyCharm IDE can still see all of the branches just fine.
I have searched for answers throughout SO and other sites to no avail.
git branch used to return all local and remote repositories.
Only git branch -a would return all local and remote tracking branches (branches, not "repositories").
Check first if, as commented, this is a pager issue. Change shell (if you are on Windows, switch between CMD, Powershell or bash, to see if the issue persists)
git --no-pager branch does work!
How do I fix git branch based on this info?
As noted in git config:
To disable pagination for all commands, set core.pager or GIT_PAGER to cat.
git config core.pager cat
But for disabling it only for git branch, you have various options as described in the similar question "How do I prevent 'git diff' from using a pager?"
An alias for git -P branch would be one of them. (-P is --no-pager)

How to use vim-fugitive with a git bare repository?

Environment
Setup
I use a git bare repository to version my config files. I can use same commands as if I were using a normal git repository just have to include some flags:
git --git-dir=/home/kunzaatko/.cfg/ --work-tree=/home/kunzaatko/ __command__
instead of
git __command__
Usage
I make use of vim-fugitive with normal git repositories mainly for making a big change and adding it in many different commits by staging partially (only a discrete set of hunks/changes) and committing them separately . I use :Gdiff for this for the nice and productive interface I can make use of.
Desire
I want to do this with my config git bare repository.
What I tried:
Renaming the repository to .cfg.git. This didn't make any change. issue that suggests this should work
I tried to change the b:git_dir internal variable of git-fugitive:
:let b:git_dir=/home/kunzaatko/.cfg/
Changing the working directory to the git directory for fugitive to recognize that it is a git repo:
:chdir /home/kunzaatko/.cfg/
What would be worthy of trying if I knew how:
I think that there may be a way to use the git submodule command to put the bare repo into scope. The problem with that is where to put the root of the git repository... issue that I base this possibility of of
Question
Is there a way to use a git bare repository with git-fugitive?
(or any other suggestion that would solve my use-case)
I agree with #okket, but there is a problem when I use that method.
When in Gstatus window, I cannot get the status of changed files. And I find the reason is that fugitive gets the core.worktree attribute from the GIT_DIR repository.
So for me, the viable way to do this is as follows:
Same as #okket says, set GIT_DIR env variable when using vim/nvim command (GIT_WORK_TREE may be omitted):
GIT_DIR=$HOME/.cfg GIT_WORK_TREE=$HOME [n]vim
For users of fish shell (like me), should use env command:
env GIT_DIR=$HOME/.cfg GIT_WORK_TREE=$HOME [n]vim
Set core.worktree for your git repository:
git --git-dir=$HOME/.cfg --work-tree=$HOME config --local core.worktree $HOME
You can make sure you get it right with the following command:
git --git-dir=$HOME/.cfg --work-tree=$HOME config --local core.worktree
The output should be your HOME path.
If you get the git repository by git clone --bare ..., you need to unset core.bare to avoid git status error warning: core.bare and core.worktree do not make sense:
git --git-dir=$HOME/.cfg --work-tree=$HOME config --unset core.bare
And there is one more thing to notice for this method. You cannot use fugitive outside of $HOME directory where you will get fugitive: working directory does not belong to a Git repository error.
PS: I'd like to comment on #okket 's answer, but due to low reputation I can only post a new answer here.
The environment variables $GIT_DIR and $GIT_WORK_TREE let you point git and fugitive to the dotfile directory when using a bare repository. This solution works with the caveat, that fugitive/git will ignore other repositories while these variables are set.
To limit this environment pollution start vim like this:
GIT_DIR=$HOME/.cfg GIT_WORK_TREE=$HOME vim [file(s)]
In case the syntax is unfamiliar: it is possible to prepend setting environment variables before calling a binary in most (all?) shells. This limits redirecting git through the environment variables to this specific vim instance.
A quality of life improvement would be to put this line into a shell alias or use a full featured dotfile manager like yadm, which is a wrapper around a git bare repository.

Changing the location of git installation on linux

I apologize if this seems basic but I'm new to linux and not really sure how to proceed. My current git version is 1.7.1 and is located in /usr/bin/git but a newer version of git (1.8) is now available in /usr/src/git/bin/git. How do I make git use this version by default as opposed to the 1.7.1 version?
You have to make sure to call the right executable. This can ben done by explicitly calling /usr/src/git/bin/git instead of git. Of course this would be annoying to type all the time, so you can either make git an alias for that path by adding the line
alias git=/usr/src/git/bin/git
to your .bashrc, or add the directory /usr/src/git/bin to your binary search path by adding the line
export PATH="/usr/src/git/bin:$PATH"
To test that the other git installation searches for the core binaries in the right place, you can check the output of git --exec-path.

What is wrong with my git 1.8.4.2-1?

I have an old Synology DS-106j server where I tried to install git using ipkg command. The installation went smoothly, but git failed to work correctly. I am currently learning how to use git, so I don't know if it is a bug from git with the version I am using or something else is wrong.
What I did was create a new local repository with a specified name, add a new file, commit it, and got an error:
NAS_SERVER> git init Test
Initialized empty Git repository in /root/Test/.git/
NAS_SERVER> ls
Packages.gz git_1.8.4.2-1_powerpc.ipk
Test
NAS_SERVER> cd Test
NAS_SERVER> git status
# On branch master
#
# Initial commit
#
nothing to commit (create/copy files and use "git add" to track)
NAS_SERVER> touch Test.cs
NAS_SERVER> ls
Test.cs
NAS_SERVER> git add *
NAS_SERVER> git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
# (use "git rm --cached <file>..." to unstage)
#
# new file: Test.cs
#
NAS_SERVER> git commit -m "Test"
fatal: 57e2b8c52efba71d84c56bf6f37581686b9061a3 is not a valid object
I thought...maybe I did something wrong, so I used git on Windows OS and try a push. Still an error. Transfer the whole repository to the server and check the status. It seems fine. Try a commit. Still the same result. What worse is that I can't update git version without having to compile it, which I don't even know how to do so. Any suggestion to what might be wrong?
If your goal is to push into a git repo located on the synology disk(s) for backup purposes I'd recommend a different approach which would avoid having to install a rather old git version on the synology box itself (which could lead to problems if/when using a newer git version on the windows machine).
Export a samba share from synology, mount it on windows and use the windows git to create the backup repo (maybe even a bare repo, eventually group shared if you plan to share work with other people). Then push from your working repo into this backup repo - all on the windows box. In this scenario the synology box doesn't need git installed, it just serves files (i.e. its original job).
I'm using such setup but with a linux machine instead of a windows one and with the bare repo on the synology disks exported via NFS instead of Samba.

Why isn't " $ git log --graph " working

I've recently tried using branches in git but while they work allright whatever I do I can't display the graph of my branches and merges. I've tried Giggle for ubuntu and gitk -all but none of them works as shown in this nettuts+ tutorial http://net.tutsplus.com/tutorials/other/easy-version-control-with-git/
I type in that
$ git log --graph #Sorry my sreenshot is missing h at the end
but instead of this...
...I get this...
I would be really pleased with a quick reply...
Here is what ' $ gitk --all ' outputs
You might be looking for git log --graph --all, likewise with gitk, I have an alias in my shell:
alias gk='gitk --all'
for just that purpose.
From git help log
--all
Pretend as if all the refs in refs/ are listed on the command line as <commit>.
So if you have branches a, b, and c, it's as if you told git to show the logs and graphs in this manner:
git log --graph a b c
Essentially, git log allows you to list any objets you can reference in what you want to see the logs for. You can even use it on a file, or multiple files files....
$ git log --oneline python-taboot.spec
c96d546 Straighten out the Makefile. Hey -- make rpm works now! Update a lot of docs. Fixes #34 - Taboot 'edit' mode should hint at the file type and give instructi
ea0d60b Version bumpskies to 0.4.0-1beta
a95cfbf Automatic commit of package [python-taboot] release [0.3.2-1].
d9e3ca6 Make python-argparse a Requires for el6 as well
ea7ed54 Automatic commit of package [python-taboot] release [0.3.1-1].
69eaea9 Add conditional Requires on python-argparse. Update README and release notes.
...
And so on...

Resources