Since I've started contributing to open source projects in the last few months, I've learned that I could benefit from a deeper understanding of Git, and GitHub flow. So, this week I'm starting a series of posts about the things I'm learning on my quest to become a Git and GitHub pro.
Today I'm covering the basics of creating a git repository, branches, commits, push, pull, and merge.
Before we begin, if you don't have Git installed on your computer you can click the link to find information about Git Installation. And if you haven't already, you can create an account at GitHub.com.
There are a two different situations in which you would want to create a git repository. Let's go over how to do this in the command line for each situation.
Existing Local Directory:
cd path/to/directory git init
First you change directories to the one you want to create a git repository for and then you enter
git init. This creates the git repository for the directory that you are in.
Cloning an Existing Repository
cd path/to/directory git clone <url/to/remote/repository>
Cloning is used when you want to copy an existing repository to a directory on your local machine. Again you want to switch to the directory where you want to copy the repository. Then you enter
git clone followed by the URL of the remote repository you want to clone.
In the case of
git clone, your directory is automatically connected to to remote that you copied it from. But what about
git init? In that case you need to manually add a remote.
git remote add origin <url/to/remote/repository>
git remote add accepts two arguments, whatever you want to name the remote, and the URL to the remote repository. Origin is a typically used for the name but you could choose anything. The remote is an online copy of your repository, hence the URL. IF you don't know how to create repository on GitHub and find the associated URL, click this here.
It's generally considered bad form to work directly off you master branch, so the first thing you want to do is create a feature branch.
git branch [branch-name] git checkout [branch-name]
git checkout -b [branch-name]
Option 1 tells Git to create a branch called branch-name, then
git checkout tells Git to switch to the branch called branch-name. Option 2 does both of those things in one command.
So you have your repository initialized, you're connected to a remote repository, and you're on a new branch. Now you're ready to make changes and commit those changes.
git add . git commit -m "commit message"
Before you can commit your changes you need to tell Git what you want to be in the commit. The command
git add . stages all the files that have been changed since your last commit and tells Git they should be added to the next commit. The
git commit command takes a snapshot of the code as it stands at that moment. It's important to add a descriptive commit message so other developers can get an idea of what was updated in that commit, which you do with the
To sync the remote with the local repository we use the
git push command.
If it's your first push:
git push -u [remote-name] [branch-name]
Any other push:
git push [remote-name] [branch-name]
git push accepts two arguments, the name of the remote you want to push to, and the branch you want to push. The first time you push to your remote repository, you should use the
-u option with tells git to add upstream tracking references. The
git push command will add the local commits to the remote copy of the branch.
What if our working with someone else on the same repository and you need to update your local copy with changes that someone else added to the remote copy. You can do this with
git pull [remote-name]
This will update your local directory with all of the new commits that are on the remote repository.
Once you have some new feature or change made on your branch, it's time to merge those changes into master.
git checkout master git merge [feature-branch-name]
First you want to switch to the branch you want to merge your feature branch into, in this case 'master'. Then you pass the
git merge the name of the branch you want to merge into the current branch.
And that's it! Well not quite. That's the tip of the iceberg, but it's what you need to know to get started. In the weeks to come I'll be going into more depth on these topics and as well as more advanced Git commands. But for now, let's review:
git init: Creates the git repository for the directory that you're in.
git clone <url/to/remote/repository>: Copies a remote git repository to the current directory on your local machine.
git remote add [remote-name] <url/to/remote/repository>: Connects the git repository of the directory your currently into the remote at the URL given. It also assigns this remote repository the name given([remote-name]).
git branch [branch-name]: Creates a new branch with the name given.
git checkout [branch-name]: Switches to the branch that matches the name given.
git checkout -b [branch-name]: Create a new branch with the name given and switches to that branch.
git add .: Prepares all changed files on this branch to be added to the next commit.
git commit -m "commit message": Creates a commit and adds a descriptive message to that commit. A commit is a snapshot of the code as it currently stands.
git push -u [remote-name] [branch-name]: Adds new local commits to the remote repository. The
-uoption adds remote tracking on your first push, though it can be omitted on all pushes after that.
git pull [remote-name]: Updates your local directory with all of the new commits that are on the remote repository.
git merge [branch-name]: Merges the commits from the branch listed into the branch that you are currently on.
Setting Up a Repository - atlassian.com
Git Clone - atlassian.com
Git Clone - git docs
Adding a Remote - GitHub.com
Git Cheatsheet - GitHub.com
Saving Changes - atlassian.com
Git Commit - atlassian.com
Git Push - atlassian.com
Git Pull - atlassian.com
Git Merge - atlassian.com