Jun
08
2017




How to Install and Configure Basic Git Server on CentOS 6

Git has changed my life.  I’m not exaggerating.  I work with a lot of ‘one off’ scripts (BASH, PHP, and Python).  They are spread out among systems and developed from my workstation and(sometimes) laptop.  I spent 15 years keeping multiple versions and copies of the same scripts spread between systems.  Looking back it seems Crazy!

While I’ve known about Git for years, but I had no idea how badly I needed it until I interviewed for a contract and the interviewer repeatedly mentioned Git as a crucial part of their development process.  I admitted to him that I didn’t use it, but I would set it up myself and answer his questions in the next interview.  I installed it and set out to learn the centralized Git workflow(similar to SVN; one master setup).  There are other workflows and I recommend taking a look at this post if you are curious: https://www.atlassian.com/git/tutorials/comparing-workflows  

Goals of this Post:

  • Install  and configure basic Git for centralized workflow

 

[root@git ~]# yum install git

Add a user called ‘git'(or whatever) to handle Git request.  Make a tough password.

[root@git ~]# system-config-users

Next we need to configure some default git options.  These could be set globally on the server ‘/etc/git/gitconfig’ and in a users home directory, ‘~/.gitconfig’.

[root@git ~]# git config –global user.name “John Doe”
[root@git ~]# git config –global user.email johndoe@example.com
[root@git ~]# git config –global core.editor nano

Now create a directory to house our code.

[root@git ~]# mkdir /home/git/code

Finally let’s make a soft link to the / directory for easy access.  To the client code will stored/retrieved with the string, ‘git@10.1.10.10:/git/myproject’.  Rather than ‘git@10.1.10.10:/home/git/code/myproject’.

[root@git ~]# ln -s /home/git/code /git

Remember anything created by root user needs to change permissions back to the Git user.

[root@git ~]# chown git:git /home/git/code

Lets test it!  Create a Git project directory to clone from.

[root@git ~]# cd /git
[root@git git]# mkdir mycode.git
[root@git git]# touch mycode.git/README
[root@git git]#

Issue the command to initiate the Git repo.

[root@git ~]# git init –bare /git/mycode.git
Initialized empty Git repository in /home/git/code/mycode.git/
[root@git ~]#

Change owner & group to ‘git’.

[root@git code]# chown -R git:git mycode.git/

From your remote machine clone the Git repo.

[matt@mattcom1 git]$ git clone git@10.1.10.10:/git/mycode.git
Cloning into ‘mycode’…
git@10.1.10.10’s password:
warning: You appear to have cloned an empty repository.
Checking connectivity… done.
[matt@mattcom1 git]$ cd mycode/

Create some test code.

[matt@mattcom1 mycode]$ nano hello.py
#!/bin/python3

print(“hello”)

[matt@mattcom1 mycode]$ git add hello.py
[matt@mattcom1 mycode]$ git commit
Add hello.py

# Please enter the commit message for your changes. Lines starting
# with ‘#’ will be ignored, and an empty message aborts the commit.
# On branch master
#
# Initial commit
#
# Changes to be committed:
# new file: hello.py
#

[master (root-commit) 0a7d6fc] Add hello.py
1 file changed, 3 insertions(+)
create mode 100755 hello.py
[matt@mattcom1 mycode]$

Now push your commit to the of the new code to the upstream git server.

[matt@mattcom1 mycode]$ git push git@10.1.10.10:/git/mycode.git
git@10.1.10.10’s password:
Counting objects: 3, done.
Writing objects: 100% (3/3), 248 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@10.1.10.10:/git/mycode.git
* [new branch] master -> master
[matt@mattcom1 mycode]$

Test your Git Repository  delete  the mycode directory and clone it again.  You should see the file you just created and pushed.

[matt@mattcom1 mycode]$ cd ..
[matt@mattcom1 git]$ ls
fabric mycode nflrank puppet python swnpc voiceip
[matt@mattcom1 git]$ rm -rf mycode/
[matt@mattcom1 git]$ git clone git@10.1.10.10:/git/mycode.git
Cloning into ‘mycode’…
git@10.1.10.10’s password:
remote: Counting objects: 3, done.
Receiving objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Checking connectivity… done.
[matt@mattcom1 git]$ ls mycode/
hello.py
[matt@mattcom1 git]$

Thank you for reading!  Please comment if you have questions.

If you like this article tell Google!!









Written by mattb in: Automation,Linux | Tags: , , , , ,

No Comments »

RSS feed for comments on this post. TrackBack URL

Leave a comment