Learn Git Progressively - Day 4
You’re reading Part Four of the “Learn Git progressively” four-part tutorial.
- Basics - Setup,
init, add, and commit
- Daily usage -
status, diff & difftool, and log
- Branching & Merging -
branch, checkout, and merge
- Collaborative coding -
remote and Github.com
This is a follow-up to my previous post, Learn Git progressively - Day 3. I suggest reading that post before continuing here.
By now you know how to use enough of Git that you’ll probably be able to manage any project you work on using Git. However, the core strength of Git lies in it’s ability to make collaborative coding—-coding with a team—-a complete breeze. Sharing code and projects using Git is very easy, and websites like GitHub and BitBucket have made it not only easy, but outright enjoyable. We’ll discuss collaborative coding using Git below.
In addition to collaborative coding, there are a few features in Git that aren’t essential, but will make your experience with Git smoother and more pleasant. We’ll discuss a few of those at the end of our final tutorial.
remotes
By now you’re familiar with the concept of a Git repository. It’s where all the details of your files—-commit histories, log notes, branches, deleted files—-are stored on your computer. What we haven’t discussed up until now is that Git is designed so that this entire repository can be very easily shared with other individuals. In this way, you not only share your code with other members of your team, but you can share your commit logs, your code diffs, and even your branches with others. Even better, you can set up a single repository that everyone can access, and in doing that you can all view each other’s changes, branches, commit logs, and other repository details, making code sharing extremely simple.
The setup I just described is a brief summary of git remote. A remote is a connection between the repository you’ve been storing your code in and another repository elsewhere which will act as a central server1. Typically, this will be on some company server or a website like GitHub (more on that later). In fact, in many cases, your first interaction with a new Git project will involve interfacing with this remote, using the git clone <url> command:
#
# Clone eykanal's "notestack" repository from github.com
#
$ git clone git@github.com:eykanal/notestack.git
Cloning into notestack...
remote: Counting objects: 857, done.
remote: Compressing objects: 100% (359/359), done.
remote: Total 857 (delta 459), reused 847 (delta 449)
Receiving objects: 100% (857/857), 1.55 MiB | 1.62 MiB/s, done.
Resolving deltas: 100% (459/459), done.
(None of that output is important; it’s just there to make your boss think you’re doing real work. It’s always better to have resolved your deltas than to not have resolved your deltas, I say.) This function tells git that it should create a local copy of the git repository on your computer. When you run this command, Git will download the entire repository on your computer, so that you have access to all the commit logs on the master branch.
Note that this only pulls the master branch. If you want to pull a When you download, or “clone”, a branch to your machine, Git will remember where the URL from where you retrieved the code originally, and it will assign it a name. This name by default is “origin”. You use this name to download an updates to the codebase,
$ git pull origin
to download additional branches,
$ git pull origin <remote branch name other than "master">
and when uploading your changes,
$ git push origin <branch you want to push>
There’s one more detail about remotes which may cause confusion from SVN veterans. When you typed git clone <url>, you created a local repository. You will be able to interact with that repository the way you interact with any other Git repository; you’ll make commits, you can roll back changes, you can create and merge branches, etc. When you’re done and you want to push your changes to the remote repository, you will be pushing all your changes—-i.e., your entire commit history—-to the remote. In other words, you download a repository with it’s history, and when you upload changes, you’re simply uploading the same repository with a bit more history attached; i.e., the changes you’ve added.
For those occasions where you didn’t originally download code from a remote, remotes are pretty simple to set up. You will be provided with a repository URL, which will often (but not always) look like a typical URL. You can add the remote repository by typing
git remote add <url> <remote name>
Once the remote is present, you can download and upload using the remote name you specified as described above.
Github.com
The ability to work with remotes makes Git an excellent tool for collaborative coding. However, Github.com2 has turned Git from a useful tool to the tool that many current open-source projects use to share their open-source projects. Between the very slick interface and the useful project management tools, you will find yourself wanting to store your projects on Github.
Note: Github.com is an awesome website, where some of the most interesting and popular Open Source projects are shared and collaboratively edited by hundreds of developers. However, a free account on Github does not come with any private repositories. This means that any code you publish to Github using a free account can be seen—-and downloaded by—-anyone on the internet. I’m demonstrating remotes using Github because of it’s immense popularity, but if you are working on a commercial project, DO NOT put it in a public GitHub repository. Note that BitBucket, another popular collaborative coding site, does offer a free account with unlimited private Git repositories.
The initial setting up of Github access takes a little bit of work, but it’s very well documented at the Git help center. You’ll want to check out the section titled “Set up SSH access”. Go ahead and do that and then come back here. Here’s the link again: Git help center.
The easiest way to move your project to Github is to follow the following three steps.
- Log in to the site.
- Click on the “New repository” link on the right hand side of the page. Alternatively, you can just follow this link. Give the project a name and a description.
- The instructions as to what to do next will be printed right in front of you. (Didn’t I tell you that Github is easy to use?) If you are starting a new project, follow the “Next steps:” instructions; if you have an existing project, follow the “Existing Git Repo?” instructions.
Congrats; you’re now a full-fledged member of the DCVS revolution.
Forking
While on Github, you’ll want to search and check out other people’s code. If you see something interesting that you want to contribute to, click on the “Fork” link. This creates a copy of his project (think git clone) that you have full ownership of. You can download the copy, you can edit the copy, compile it, upload it, and (if the license permits) ship products with it. If you make some awesome changes that you think the original developer would love to add to the original code, you can simply submit your code back to him via a “pull request”; this takes all your changes and sends them back to the original author with the note “please incorporate this into your codebase.” You can do this with bug fixes, feature requests, etc. Again, the Github.com help site has a useful page describing of how to use this feature.
Congratulations!
You’ve now familiarized yourself with one of the most capable versioning systems available today. Well done! Now go out there and code something awesome!
-
Technically, remotes don’t have to work as central repositories; you can have a network of individual computers all connected to each other via remotes, with no central server. However, this sort of setup is very uncommon in practice. ↩
-
And Bitbucket.com as well, but as most projects are housed on Github we’ll be focusing exclusively on that. Bitbucket does have many compelling features, though, including some that Github doesn’t offer, and I recommend you compare feature sets to see which is best for you. ↩