manage_config_with_git
Table of Contents
Manage Configuration Files with Git
ex)
| username | pctr |
|---|---|
| mail address | pctresearch@pctresearch.com |
Case1: user home directory
$ cd ~/ # /home/pctr $ cat .gitconfig [user] name = pctr email = pctresearch@pctresearch.com [core] editor = code [merge] tool = meld [mergetool "meld"] cmd = meld "$LOCAL" "$MERGED" "$REMOTE" --output "$MERGED" [mergetool "meld"] trustExitCode = false [difftool] Prompt = false [difftool "meld"] cmd = meld "$LOCAL" "$REMOTE"
$ cat .gitignore # Ignore all * # Unignore all start with dot files #!.* # Unignore specific start with dot dirs !.ssh/ .ssh/* # Unignore specific files !.bash_logout !.bashrc !.gitconfig !.gitignore !.profile !.vimrc !.ssh/config
Initializes a new Git repository in the current directory.
$ git init
Shows the current state of your repository
$ git status
You can view tracked files in specific directories with:
ex) $ git status .ssh/*
stage the modified and new files.
$ git add -A
Creates a new commit with the changes in the staging area and specifies the commit message inline.
$ git commit -m “<message>”
done.
To list all files currently being tracked under the branch master, use ls-tree:
$ git ls-tree -r master --name-only .bash_logout .bashrc .gitconfig .gitignore .motd_shown .profile .ssh/config .sudo_as_admin_successful .vimrc
Backup configure files in home directory
If you want to make backup in /backup/home:
$ sudo mkdir -p /backup/home $ sudo chown pctr:pctr /backup/home $ cd /backup/home $ git clone /home/pctr
And you can update backup with:
$ cd /backup/home/pctr $ git remote -v # For confirmation origin /home/pctr (fetch) origin /home/pctr (push) $ git fetch $ git pull /home/pctr master From /home/pctr * branch master -> FETCH_HEAD Already up to date.
Case2: under certain root owner directories (E.g. /etc
This time we are going to make git repository in “/etc”.
You can do this in other directories like “/usr”.
fundamentally, same as Case1
.gitconfig is same.
$ cat /.gitconfig [user] name = pctr email = pctresearch@pctresearch.com [core] editor = code [merge] tool = meld [mergetool "meld"] cmd = meld "$LOCAL" "$MERGED" "$REMOTE" --output "$MERGED" [mergetool "meld"] trustExitCode = false [difftool] Prompt = false [difftool "meld"] cmd = meld "$LOCAL" "$REMOTE"
If you have apache2, ssh and ufw installed and configured, the .gitignore will look like this.
$ cat /.gitignore # .gitignore for /etc # Ignore all * # ssh !ssh/ ssh/* # ufw !ufw/ ufw/* !ufw/applications.d/ ufw/applications.d/* !ufw/applications.d/apache2/ # apache2 !apache2/ apache2/* !apache2/sites-available/ apache2/sites-available/* !apache2/sites-enabled/ apache2/sites-enabled/* !apache2/mods-enabled/ apache2/mods-enabled/* !apache2/conf-available/ apache2/conf-available/* # apache2 config !apache2/sites-available/wweb.conf !apache2/sites-enabled/wweb_ssl.conf !apache2/apache2.conf !apache2/envvars !apache2/mods-enabled/dir.conf !apache2/conf-available/serve-cgi-bin.conf !apache2/sites-available/default-ssl.conf !apache2/sites-available/wweb_ssl.conf # Unignore specific files !.gitignore !.gitconfig # ssh config !ssh/ssh_config # ufw config !ufw/ufw.conf !ufw/applications.d/apache2-utils.ufw.profile !ufw/applications.d/openssh-server
Initializes a new Git repository in the current directory.
$ cd /etc $ sudo git init
Shows the current state of your repository
$ git status On branch master No commits yet Untracked files: (use "git add <file>..." to include in what will be committed) .gitconfig .gitignore apache2/ ssh/ ufw/
You can view tracked files in specific directories with:
ex) $ git status apache2/* On branch master No commits yet Untracked files: (use "git add <file>..." to include in what will be committed) apache2/apache2.conf apache2/conf-available/ apache2/envvars apache2/mods-enabled/ apache2/sites-available/ apache2/sites-enabled/ nothing added to commit but untracked files present (use "git add" to track) $ git status apache2/conf-available/* On branch master No commits yet Untracked files: (use "git add <file>..." to include in what will be committed) apache2/conf-available/serve-cgi-bin.conf nothing added to commit but untracked files present (use "git add" to track)
stage the modified and new files.
$ sudo git add -A
Check git status.
$ git status On branch master No commits yet Changes to be committed: (use "git rm --cached <file>..." to unstage) new file: .gitconfig new file: .gitignore new file: apache2/apache2.conf new file: apache2/conf-available/serve-cgi-bin.conf new file: apache2/envvars new file: apache2/mods-enabled/dir.conf new file: apache2/sites-available/default-ssl.conf new file: apache2/sites-available/wweb.conf new file: apache2/sites-available/wweb_ssl.conf new file: apache2/sites-enabled/wweb_ssl.conf new file: ssh/ssh_config new file: ufw/applications.d/apache2-utils.ufw.profile new file: ufw/applications.d/openssh-server new file: ufw/ufw.conf
Creates a new commit with the changes in the staging area and specifies the commit message inline.
$ sudo git commit -m "apache2 ssh ufw" $ sudo git commit -m "apache2 ssh ufw" [master (root-commit) 44f6c7b] apache2 ssh ufw .... 14 files changed, 656 insertions(+) create mode 100644 .gitconfig create mode 100644 .gitignore create mode 100644 apache2/apache2.conf create mode 100644 apache2/conf-available/serve-cgi-bin.conf create mode 100644 apache2/envvars create mode 120000 apache2/mods-enabled/dir.conf create mode 100644 apache2/sites-available/default-ssl.conf create mode 100644 apache2/sites-available/wweb.conf create mode 100644 apache2/sites-available/wweb_ssl.conf create mode 120000 apache2/sites-enabled/wweb_ssl.conf create mode 100644 ssh/ssh_config create mode 100644 ufw/applications.d/apache2-utils.ufw.profile create mode 100644 ufw/applications.d/openssh-server create mode 100644 ufw/ufw.conf
done.
To list all files currently being tracked under the branch master, use ls-tree:
$ git ls-tree -r master --name-only .gitconfig .gitignore apache2/apache2.conf apache2/conf-available/serve-cgi-bin.conf apache2/envvars apache2/mods-enabled/dir.conf apache2/sites-available/default-ssl.conf apache2/sites-available/wweb.conf apache2/sites-available/wweb_ssl.conf apache2/sites-enabled/wweb_ssl.conf ssh/ssh_config ufw/applications.d/apache2-utils.ufw.profile ufw/applications.d/openssh-server ufw/ufw.conf
Backup configure files in /etc directory
If you want to make backup in /backup/etc :
$ sudo mkdir -p /backup $ cd /backup $ sudo git clone /etc
This will allow you to run “git status” without sudo:
$ git config --global --add safe.directory /backup/etc $ git status On branch master Your branch is up to date with 'origin/master'. nothing to commit, working tree clean
You can update backup with:
$ cd /backup/etc $ git remote -v # For confirmation origin /etc (fetch) origin /etc (push) $ sudo git fetch $ git pull /etc master From /etc * branch master -> FETCH_HEAD Already up to date.
For reference
Sample of /usr/.gitignore
$ cat /usr/.gitignore # .gitignore file for /usr # Ignore all * # Unignore specific dirs !local/ local/* !local/bin/ local/bin/* # ssl certificate !/usr/share/ /usr/share/* !/usr/share/ssl-cert/ /usr/share/ssl-cert/* # ssl certificate config !/usr/share/ssl-cert/ssleay.cnf !/usr/share/ssl-cert/ssleay_wweb.cnf # /usr/loval/bin # your original shell !local/bin/ialert.sh !local/bin/inwatch.sh # Unignore specific files !.gitignore !.gitconfig
Another sample of /etc/.gitignore
$ cat /etc/.gitignore # Ignore all * # Unignore all start with dot files !.* # ignore all start with dot dirs #.*/ # Unignore specific dirs !apache2/ !ssh/ !ufw/ !samba/ !gdm3/ !timeshift/ !postfix/ !dovecot/ !cron.d/ !pam.d/ # Unignore .json files !*.json # Unignore .conf files !*.conf # Unignore specific files !authorized_keys !id_rsa.pub !known_hosts !profile !hostname !hosts !hosts.allow !hosts.deny !aliases # pam.d !pam.d/atd !pam.d/common-session !pam.d/gdm-launch-environment !pam.d/newusers !pam.d/smtp !pam.d/chfn !pam.d/common-session-noninteractive !pam.d/gdm-password !pam.d/other !pam.d/sshd !pam.d/chpasswd !pam.d/cron !pam.d/gdm-smartcard !pam.d/passwd !pam.d/su !pam.d/chsh !pam.d/cups !pam.d/gdm-smartcard-pkcs11-exclusive !pam.d/ppp !pam.d/sudo !pam.d/common-account !pam.d/dovecot !pam.d/gdm-smartcard-sssd-exclusive !pam.d/runuser !pam.d/sudo-i !pam.d/common-auth !pam.d/gdm-autologin !pam.d/gdm-smartcard-sssd-or-password !pam.d/runuser-l !pam.d/sudo.org !pam.d/common-password !pam.d/gdm-fingerprint !pam.d/login !pam.d/samba !pam.d/su-l # apache2 !apache2/envvars !apache2/conf-available !apache2/conf-enabled !apache2/sites-available !apache2/sites-enabled # ssh !ssh/ssh_config !ssh/sshd_config !ssh/ssh_host_rsa_key.pub # ufw !ufw/ufw.conf !ufw/applications.d/ ufw/applications.d/* !ufw/applications.d/apache2-utils.ufw.profile !ufw/applications.d/cups !ufw/applications.d/dovecot-imapd !ufw/applications.d/dovecot-pop3d !ufw/applications.d/openssh-server !ufw/applications.d/postfix !ufw/applications.d/samba # postfix postfix/* !postfix/main.cf # dovecot dovecot/* !dovecot/conf.d/ dovecot/conf.d/* !dovecot/dovecot.conf !dovecot/conf.d/10-ssl.conf # cron.d !cron.d/anacron !cron.d/clamscan !cron.d/dokuwiki !cron.d/e2scrub_all !cron.d/php !cron.d/.placeholder !cron.d/sysstat !cron.d/timeshift-hourly # ignore .tmp .org files *.tmp *.org .bash_history .lesshst # definisions hereunder are not necessary. #ignore .git/ .git/ # Prerequisites #*.d # Compiled Object files *.slo *.lo *.o *.obj # Precompiled Headers *.gch *.pch # Compiled Dynamic libraries *.so *.dylib *.dll # Fortran module files *.mod *.smod # Compiled Static libraries *.lai *.la *.a *.lib # Executables *.exe *.out *.app
Usefull Command
Restore an Unintentionally Modified Config File
$ git status On branch master Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git restore <file>..." to discard changes in working directory) modified: <file name> $ git restore <file name> or $ sudo git restore <file name>
Check Modified History
$ git log commit d66c386b161b6c6447aa8f6a2845cdfe23d94f45 (HEAD -> master) Author: pctr <pctresearch@pctresearch.com> Date: Thu Feb 27 08:09:26 2025 +0900 .gitignore commit e22036cf2557d4ab99095ede6e1c293787f9d152 Author: pctr <pctresearch@pctresearch.com> Date: Thu Feb 20 04:28:24 2025 +0900 add ssh setting commit d474b84927877651e3b8725c6c7d1d3780c4b6c4 Author: pctr <pctresearch@pctresearch.com> Date: Wed Feb 19 20:02:35 2025 +0900 Customize some configuration files commit 8175285fb6420ddd2bd23df18e7f8c9c74e9e830 Author: pctr <pctresearch@pctresearch.com> Date: Wed Feb 19 19:54:01 2025 +0900 initial status
$ cgit diff 8175285fb6420ddd2bd23df18e7f8c9c74e9e830..HEAD .bashrc diff --git a/.bashrc b/.bashrc index b488fcc..a369f62 100644 --- a/.bashrc +++ b/.bashrc @@ -8,6 +8,16 @@ case $- in *) return;; esac +blk='\[\033[01;30m\]' # Black +red='\[\033[01;31m\]' # Red +grn='\[\033[01;32m\]' # Green +ylw='\[\033[01;33m\]' # Yellow +blu='\[\033[01;34m\]' # Blue +pur='\[\033[01;35m\]' # Purple +cyn='\[\033[01;36m\]' # Cyan +wht='\[\033[01;37m\]' # White +clr='\[\033[00m\]' # Reset + # don't put duplicate lines or lines starting with space in the history. # See bash(1) for more options HISTCONTROL=ignoreboth @@ -57,7 +67,8 @@ if [ -n "$force_color_prompt" ]; then fi if [ "$color_prompt" = yes ]; then - PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ ' +# PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ ' + PS1='${debian_chroot:+($debian_chroot)}'${grn}'\u@\h'${clr}':'${blu}'\w\n'${clr} else PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ ' fi @@ -115,3 +126,23 @@ if ! shopt -oq posix; then . /etc/bash_completion fi fi +alias sudo='sudo ' +alias cpu10='ps -L aux | sort -nr -k 3 | head -10' +alias mem10='ps -L aux | sort -nr -k 4 | head -10' +alias lsmount='mount |column -t' +alias sshk='ssh -p 65342 kan@win10kan2' +alias sshuk='ssh -p 65342 kan@ubuntukan' +alias vimn='vim.nox -u ~/.vimrc ' +alias ggit='export GIT_EXTERNAL_DIFF=git-meld-diff && git ' +alias cgit='unset GIT_EXTERNAL_DIFF && git ' + +function git_branch() { + if [ -d .git ] ; then + printf "%s" "($(git branch 2> /dev/null | awk '/\*/{print $2}'))"; + fi +} + +PS_GIT='$(git_branch)' +#PS1=${cyn}${PS_GIT}${clr}${PS1}${pur}'\n'${clr}'\$ ' +PS1=${cyn}${PS_GIT}${clr}${PS1}${pur}''${clr}'\$ ' + (END)
Restore a Specific Historical Revision of a Specific Config File
$ git restore --source d474b84927877651e3b8725c6c7d1d3780c4b6c4 .gitignore
$ git restore <file name>
is equal to :
$ git restore --source HEAD <file name>
manage_config_with_git.txt · Last modified: 2025/03/12 08:27 by jianwu
