Set Up A Private Git Server

On Red Hat/CentOS/Fedora/ systems, you can install it using yum command.

# yum install git

Now that GIT is installed, let’s set it up. In your home directory, there will be a file called “~/.gitconfig“. This holds all of your repository info. Let’s give it your name and your email:

# git config –-global “Your Name”
# git config –-global

Now we are going to create our first repository. You can make any directory a GIT repository. cd to one that has some source files and do the following:

# cd /home/vrk/project1
# git init  — creates .git — hidden folder

Add all your files

when you are working on a GIT project, you are going to be adding all of the files in the directory. You can do so like this:

#git add .

#git commit -m “initial commit”

How to Upload Project to GitHub Repository
We are now ready to push your project up to GitHub. You will need the login information that you made when you created your account. We are going to take this information and pass it to GIT so it knows where to go. Obviously, you’ll want to replace ‘user’ and ‘repo.git’ with the proper values.

# git remote set-url origin
Now, it is time to push, ie copy from your repository to the remote repository. The git push command takes two arguments: the “remotename” and the “branchname”. These two names are usually origin and master, respectively:

#git push origin master

For Private repository creation

# mkdir /git-repos

#cd /git-repos

#mkdir project1

#cd project1

#git init –bare ————–creates new empty repository.

How to upload to git private repository

#git remote add origin ssh://IP-Address/git-repos/project1

#git push origin master:refs/heads/master

Additional commands

#git log

it will display all commits information with dates

#git diff

it will display differences before commit

#git status

it will display changes made after commit.

#git show

it will display what is committed in last commit.

#git show <commit number>  — it will display what is committed in that commit

#git checkout <commit number> <file name>

it will pull old file from that checkout.


Install and Configure Git

Install git.

# sudo apt-get install git
Set up a “git” group. Members of this group will be able to push to the repo. Everyone else will be able to fetch only.

# sudo groupadd git
Then add your user to that group so that you can test. Change “ted” to your userid on the server.

# sudo gpasswd -a ted git
Now we need to create the /srv/git directory and set it up properly. (Be sure to replace “ted” with your server userid. “root” is probably ok too.)

# sudo mkdir -p /srv/git
# cd /srv
# sudo chown ted:git git
# chmod 775 git
# chmod g+s git
Tell git that we want to do sharing with the git group. This should preserve the “git” group on all files along with the setgid bit on all directories. We’ll double-check later just in case.

# sudo git config –global core.sharedRepository group
Now you can clone your first repo into the /srv/git directory. One way is to clone from a repo in another directory. In this example, we assume the repo is in a directory called “source-repo-dir”. Replace “projectname” with an appropriate name for the git project.

# git clone –bare source-repo-dir /srv/git/projectname.git
Setting core.sharedRepository to “group” should have ensured that the group and setgid bits were handled properly when we did the clone. Check to make sure this is the case. Go through the new repo and make sure all files and directories belong to the “git” group and that the setgid bit is on for all directories. If this isn’t the case, fix up the permissions with chown and chmod:

# cd /srv/git
# chown -R ted:git projectname.git
# chmod -R 775 projectname.git
# find projectname.git -type d -exec chmod g+s ‘{}’ \;
Now we can test git from another machine on the network. Clone the repo and see if it works. Replace “user” with your server userid, “host” with the IP or hostname of the server, and “port” with the random sshd port we selected. Also replace “projectname” with the name of the project.

#git clone ssh://user@host:port/srv/git/projectname.git projectname

If you made changes, and did a git add, only to realize it was in error use git reset from working directory of repo.

#git reset

And if you have committed changes, but wish to revert it, you can use revert.

#git revert

fetch updates from master

#git fetch -p origin