Monday, July 3, 2017

git - small tutorial

[admin@localhost ~]$ sudo yum install git -y
[admin@localhost ~]$ git config --global user.name "tarasov.dmitriy"
[admin@localhost ~]$ git config --global user.name "tarasov.dmitriy@gmail.com"

Make git repository in needed directory (I'll use git_test dir):
[admin@localhost ~]$ mkdir git_test
[admin@localhost ~]$ cd git_test
[admin@localhost ~]$ git init
Initialized empty Git repository in /home/admin/git_test/.git/
[admin@localhost git_test]$ cat > git_test.cfg
this is test file used to understand git
[admin@localhost git_test]$ git status
# On branch master
#
# Initial commit
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#    git_test.cfg
nothing added to commit but untracked files present (use "git add" to track)

If you have many files in directory and want to track only limited number of files, first setup gitignore file as follows (we also can add allfiles in directory via `git add .`):
[admin@localhost git_test]$ echo "*" > .gitignore
[admin@localhost git_test]$ git status
# On branch master
#
# Initial commit
#
nothing to commit (create/copy files and use "git add" to track)
[admin@localhost git_test]$ ls -1
git_test.cfg

Now we'll add needed files (git_test.cfg in our example):
[admin@localhost git_test]$ git add git_test.cfg
The following paths are ignored by one of your .gitignore files:
git_test.cfg
Use -f if you really want to add them.
fatal: no files added
Aha..
[admin@localhost git_test]$ git add -f git_test.cfg
[admin@localhost git_test]$ git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
#   (use "git rm --cached <file>..." to unstage)
#
#    new file:   git_test.cfg
#

After adding all needed files, we must commit:
[admin@localhost git_test]$ git commit -m "initial configuration commit"
[master (root-commit) 30d2109] initial configuration commit
 Committer: tarasov.dmitriy@gmail.com <admin@localhost.localdomain>
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly:

    git config --global user.name "Your Name"
    git config --global user.email you@example.com

After doing this, you may fix the identity used for this commit with:

    git commit --amend --reset-author

 1 file changed, 1 insertion(+)
 create mode 100644 git_test.cfg

Now we can make symbolic link to that file (git_test.cfg) and place this link to the needed directory so that we can use actual version of this file:
[admin@localhost git_test]$ ln -s ~/git_test/git_test.cfg ~/Desktop/

I prefer to use cp -s because it's more verbose, i.e.:
cp -s git_test.cfg /home/admin/Desktop/
cp: ‘/home/admin/Desktop/git_test.cfg’: can make relative symbolic links only in current directory

Now if we'll check the status of our git repository:
[admin@localhost git_test]$ git status
# On branch master
nothing to commit, working directory clean

To list all files in a repo, use this command:
[admin@localhost git_test]$ git ls-files
git_test.cfg


see:
1) http://www.zyxware.com/articles/2515/track-server-configuration-file-changes-using-git-versioning-system
2) http://www.saintsjd.com/2011/01/what-is-a-bare-git-repository/
3) https://developer.atlassian.com/blog/2016/02/best-way-to-store-dotfiles-git-bare-repo/