In this post we learn how to create a repository on GitHub, clone the repository, get the status, add files, commit code, push code, and pull down other code changes from the repository.
Step 1: Setup a GitHub account and create a Repository
Once you are logged into GitHub. Click on the “New Repository” button. First, give the repository a name. Then select either “Public” or “Private” depending on who should be able to see this repository. Check the “Initialize with README” checkbox and click “Create Repository”. Now, on our repository home page, click on the “Clone or Download” button and copy the link to the repository.
Step 2: Command Line Git commands
1. Open up the command line and use the following command to clone the repository that was just created in step 1. The git clone command will clone everything in the repository on GitHub.com into the servers folder you run this command inside.
git clone firstname.lastname@example.org:jstolpe/github_tutorials.git
2. Add an index.php file inside of the repository we just cloned. Run git status to get the status of the repository on the localbox compared to the repository on GitHub. After running this command, the index.php should show up in red and the command line should state that index.php is an underacted file.
3. Add the file so it is ready to be committed with the git add command. This file basically marks the file as ready for commit.
git add --all
4. Our index.php file is ready to commit at this point with the git commit command. We will be adding a commit message along with this command so when we view the commit log, we can see our comment letting us know the purpose of this commit.
git commit -m "adding index.php to the repository"
5. Before the file actually goes up to the GitHub repository we have one command left to run, git push. This will sent our file up to the repository along with our commit message.
6. Lastly, if others developers have been making changes to the same repository, we want to grab those changes and make sure we have them on our local box. This way we are up to date with the latest and greatest. To do this we run the git pull command. If we already have everything, we should see an “Already up to date” message in the command line. Otherwise, we will see all the new files and updates displayed in the command line after running git pull.
That is going to do it for the very basics of creating a repository. A few tips I have would be to run git status, alot. This way you can quickly check to see if anything locally differs from the repository. Also, run git pull before you begin working. This will make sure you have the latest and greatest code in the repository before starting in on a new feature. If you do not do a git pull before starting a new feature, and there have been many other developers who have committed lots of updates, you are not starting with the most recent, up to date repository. In this case, you run the risk of many merge conflicts when you are ready to commit your code.
That is going to do it for this post! Leave any comments/questions/concerns below and thanks for stopping by the blog!