LinuxMoz

Linux Stuff && Coffee

Git Tutorial: How to Use Git

| Comments

I have recently migrated my blog from Wordpress to Octopress during this migration I cloned a Git repository and setup my own, forcing me to learn some basic git commands.

Host your own Git repository (skip this if you are using Github)

Github offer free public repositories, however if you want a private repo you have have to pay $7 a month or host your own. I opted to host my own repo on CentOS, you can do the same with Mac OSX or even Winodws (I think), I chose to use my CentOS server as it’s accessable online so I can push pull my code from anywhere.

Enter the followinf as your user (not root) on your git server:

1
2
3
4
5
ssh [email protected]
mkdir -p git/your-repo-name.git
cd git/your-repo-name.git
git init --bare
pwd

Make note of your git repo above, it’s required for the next step.

Make sure you are in your Git dir and enter the following on your local machine (note, you only need to rename origin if the repo you are working from was originally cloned):

1
2
3
4
5
# If you are working from a cloned git repo you need to change the origin name to something else 
git remote rename origin stargate
git remote add origin ssh://[email protected]:/home/user/git/your-repo-name.git/
# set the new origin as the default branch 
git config branch.master.remote origin

I would recommend setting up SSH key based auth if you are not already using it, or you will have to enter your passphrase each time you push / pull with git.

How to use Git – basic Git commands

Now that we have the remote git repo setup, you can move to the next stage of the git tutorial and actually learn how to use git commands.

This guide assumes you have a some code in your local git dir and your remote repo is bare.

To push all your code to the remote repo you must first add the files & dir’s, cd into your code dir andenter the following to add all files & dir’s in the current directory:

1
git add .

You can now view the files & dir’s we just added with:

1
git status

If you are happy that all the files & dir’s are correct you can then commit with:

1
git commit

Finally push your code with:

1
git push origin master

Your code should then be pushed the bare git repo we created at the start of the article.

Git Pull, How To pull code with git

How to pull code with git:

1
git pull

That’s all the commands you need to get started with git, for more functionality refer to the git man page with man git.

Comments