Git: Difference between revisions

m editing "settings.user" gets an error; intended property is "config.user"
CNap (talk | contribs)
m Changed `config.user` to correctly match `settings.user`
 
(3 intermediate revisions by 2 users not shown)
Line 1: Line 1:
[https://en.wikipedia.org/wiki/Git_(software) Git] is the version control system (VCS) developed by Junio C Hamano and designed by Linus Torvalds (Creator of the Linux kernel). Git is used to maintain NixOS packages, as well as many other projects, including sources for the Linux kernel.  
[https://en.wikipedia.org/wiki/Git_(software) Git] is the [[wikipedia:Version_control|version control system (VCS)]] developed by Junio C Hamano and designed by [[wikipedia:Linus_Torvalds|Linus Torvalds]] (Creator of the [[Linux kernel]]). Git is used to maintain NixOS packages, as well as many other projects, including sources for the Linux kernel.  


== Installing and configuring Git ==
== Installing and configuring Git ==


On NixOS, Git can be installed and configured at either the system level or the user level with [[Home Manager]].
On NixOS, Git can be installed and configured at either the system level with the NixOS module or the user level with [[Home Manager]].


=== System-wide installation ===
=== System-wide installation ===
Line 10: Line 10:


{{file|/etc/nixos/configuration.nix|nix|<nowiki>
{{file|/etc/nixos/configuration.nix|nix|<nowiki>
  environment.systemPackages = with pkgs; [  
environment.systemPackages = with pkgs; [  
    git  
  git  
  ];
];
</nowiki>}}
</nowiki>}}


Line 18: Line 18:


{{file|/etc/nixos/configuration.nix|nix|<nowiki>
{{file|/etc/nixos/configuration.nix|nix|<nowiki>
  programs.git.enable = true;
programs.git.enable = true;
</nowiki>}}
</nowiki>}}


Line 30: Line 30:


<syntaxhighlight lang="nix">
<syntaxhighlight lang="nix">
  programs.git = {
programs.git = {
    enable = true;
  enable = true;
    config.user = {
  settings.user = {
        name  = "John Doe";
    name  = "John Doe";
        email = "johndoe@example.com";
    email = "johndoe@example.com";
    };
   };
   };
};
</syntaxhighlight>
</syntaxhighlight>


Aliases can be added with:
Aliases can be added with:


<syntaxhighlight lang="nix">
<syntaxhighlight lang="nix">programs.git = {
  programs.git = {
  enable = true;
    enable = true;
  settings.alias = {
    settings.alias = {
    ci = "commit";
      ci = "commit";
    co = "checkout";
      co = "checkout";
    s = "status";
      s = "status";
    };
   };
   };
</syntaxhighlight>
};</syntaxhighlight>


Git [https://git-lfs.com/ LFS] can be enabled with:
Git [https://git-lfs.com/ LFS] can be enabled with:


<syntaxhighlight lang="nix">
<syntaxhighlight lang="nix">
  programs.git = {
programs.git = {
    enable = true;
  enable = true;
    lfs.enable = true;
  lfs.enable = true;
  };
};
</syntaxhighlight>
</syntaxhighlight>
Configure git-credential-helper with libsecret:
Configure git-credential-helper with <code>libsecret</code>:


<syntaxhighlight lang="nix">{ pkgs, ... }:
<syntaxhighlight lang="nix">{ pkgs, ... }:
Line 76: Line 74:
To add additional configuration you can specify options in an attribute set, so to add something like this:
To add additional configuration you can specify options in an attribute set, so to add something like this:


<syntaxhighlight lang="ini">
<syntaxhighlight lang="ini">[push]
[push]
         autoSetupRemote = true</syntaxhighlight>
         autoSetupRemote = true
</syntaxhighlight>


To your <code>~/.config/git/config</code>, you can add the below to <code>settings</code>
To your <code>~/.config/git/config</code>, you can add the below to <code>settings</code>
Line 137: Line 133:


<syntaxhighlight lang="nix">
<syntaxhighlight lang="nix">
  programs.git = {
programs.git = {
    enable = true;
  enable = true;
    package = pkgs.gitFull;
  package = pkgs.gitFull;
  };
};
</syntaxhighlight>
</syntaxhighlight>


== Management of the <code>nixpkgs</code> git repository ==
== Management of the <code>nixpkgs</code> git repository ==


<code>nixpkgs</code> has become a git repository of quite substantial size with > 889 000 commits (as of late 2025). This brings many unoptimized tools to their limits, leading to long waiting times on certain operations. Here we’ll collect useful info on how to manage that.
<code>nixpkgs</code> has become a git repository of quite substantial size with > 1M commits<ref>https://github.com/NixOS/nixpkgs</ref> (as of 2026). This brings many unoptimized tools to their limits, leading to long waiting times on certain operations. Here we’ll collect useful info on how to manage that.


=== Garbage collecting ===
=== Garbage collecting ===


Normal <code>git gc</code> should work as usual, but you should force a full garbage collect every half a year or so. <code>git gc --aggressive</code> is the command for that. For the author it did not work on the first try, since their laptop’s memory was too small and it went out of memory. According to [https://stackoverflow.com/a/4829883/1382925|this StackOverflow] answer it suffices to set some local repository config variables.
Normal <code>git gc</code> should work as usual, but you should force a full garbage collect every half a year or so. <code>git gc --aggressive</code> is the command for that. For the author it did not work on the first try, since their laptop’s memory was too small, and it went out of memory. According to [https://stackoverflow.com/a/4829883/1382925|this StackOverflow] answer it suffices to set some local repository config variables.


<syntaxhighlight lang=console>
<syntaxhighlight lang=console>
Line 156: Line 152:
</syntaxhighlight>
</syntaxhighlight>


worked well on a machine with about 6–8 GB of free RAM and two processor threads, and reduced the size of the <code>nixpkgs</code> checkout from ~1.3 GB to ~0.95 GB.
This worked well on a machine with about 6–8 GB of free RAM and two processor threads, and reduced the size of the <code>nixpkgs</code> checkout from ~1.3 GB to ~0.95 GB.


= Serve Git repos via SSH =
= Serve Git repos via SSH =
Line 201: Line 197:
1. Run this on the server to create repo <code>myproject</code> accessible by user <code>git</code>
1. Run this on the server to create repo <code>myproject</code> accessible by user <code>git</code>


<syntaxhighlight lang="bash">
<syntaxhighlight lang="console">
sudo -u git bash -c "git init --bare ~/myproject.git"
$ sudo -u git bash -c "git init --bare ~/myproject.git"
</syntaxhighlight>
</syntaxhighlight>
(<code>~</code> here is the home of the user <code>git</code>, which is <code>/var/lib/git-server</code>)
(<code>~</code> here is the home of the user <code>git</code>, which is <code>/var/lib/git-server</code>)
Line 208: Line 204:
2. Push to the server repo from another system  
2. Push to the server repo from another system  


<syntaxhighlight lang="bash">
<syntaxhighlight lang="console">
mkdir myproject
$ mkdir myproject
cd myproject
$ cd myproject
echo hello > a
$ echo "hello" > a
git init
$ git init
git add .
$ git add .
git commit -m init
$ git commit -m "init"
git remote add origin git@myserver:myproject.git
$ git remote add origin git@myserver:myproject.git
git push origin master
$ git push origin master
</syntaxhighlight>
</syntaxhighlight>


3. Clone and edit the server repo from another system
3. Clone and edit the server repo from another system


<syntaxhighlight lang="bash">
<syntaxhighlight lang="console">
git clone git@myserver:myproject.git
$ git clone git@myserver:myproject.git
cd myproject
$ cd myproject
cat a
$ cat a
echo world >> a
$ echo "world" >> a
git commit -am hello
$ git commit -am "hello"
git push origin master
$ git push origin master
</syntaxhighlight>
</syntaxhighlight>