If you have a programming project that you are working on, chances are your workflow would be improved by adding version control. Version control makes it easier for you to back up your work, keep a history of changes you have made, work collaboratively, and share your code. Git has emerged as by far the most popular version control option, so that is what we will learn. We will be using GitHub Desktop as it makes it easy to get started.
To follow this lesson you will need to download GitHub Desktop and sign up with an account at github.com.
Let's add a project to version control. This tells git that we will want to track some of the files under this local directory.
Version control only works if you have files to track, so let's make some files.
# In a bash terminal cd <path>/Pirate nano pirate.py # In pirate.py import random print "arr, matey" # Save the file
The status tells you what changes you've made that are not in git yet.
Look at the status by clicking on the tab that should say 1 Uncommitted Change. Notice that clicking on a file shows you the changes from that file.
When you commit, you are telling git to keep track of all of the changes you have made. You create a checkpoint that you can go back to at anytime, and view differences.
Commit changes:
This history let you see all the commits you have made over time. Let's check the history now.
Click on History at the top. It just has a single commit now, but soon it will be more populated.
What will typically happen is that you make a few small changes, and then commit as you go. Make sure you put a detailed commit message for each commit so that you know what you did when (and why). This makes it easy to figure out where a bug came from or what your thought process was.
We'll make some more changes:
# Delete a line We are not using the random module. Delete it from the file. Commit "Removed unnecessary random module" Check History #Change a line Capitalize 'Arr, matey' Commit "Correct capitalization" Check History # Add some lines for i in range(100): print str(i) + " bottles of rum" Commit "Walk paces 1-100" Check History
Sometimes you make a commit and shortly afterwards realize something went wrong, you spelled something wrong, the code doesn't do exactly what you wanted, etc. We can undo our most recent commit.
Undo a commit:
We can track more than one file at a time (otherwise version control would be pretty useless).
Add a file:
# Add a new text file 'treasure.txt' Rubies Diamonds Crown Jewels Parrot # Update pirate.py to read file and print out data with open('treasure.txt') as fh: for line in fh: print line
We can tell git that we don't want to track some files. This is useful for output, configuration files, files with sensitive data (like passwords), or compiled files .
# Have pirate.py write a file with open('map.txt', 'w') as fh: for i in range(10): fh.write("Walk " + str(i) + " paces left.") # Run our program python pirate.py
Now we want to commit, but we don't want to commit map.txt (now or ever).
Right now all your files are on your computer, if you want to send them to the cloud so that you can share your code and have another backup of all your changes you create a remote. GitHub Desktop publishes your code to GitHub (though you can configure a different remote if you want).
Once you have a repo publicly you should add a readme to help people who find your project understand what it is for and begin using it.
You have committed your changes locally, but they are not on the remote repository (GitHub) yet. To get you changes to GitHub, you need to push your changes.
You can make changes directly to the remote repo, or someone else can make their own changes and push them to remote. You will need to pull those changes back to your local repo.
If you have ever accidently created a commit and can't undo, you can revert your commit.
Add a bash command to your pirate.by file echo "Polly wants a cracker"
Branching allows you to make changes to your code while maintaining a stable core. A good model to follow is to make a branch for every new feature or bug fix you want to make and then to merge it back into master when you are done.
Gold doubloons Goldfish Gold nuggets
Once you have finished what you were working on in your branch, you can merge your code back into master.
Sometimes someone git cannot figure out how to merge a file because the same line has been changed in both branches. This is called a merge conflict and you will have to tell git which lines you want to keep.
We are going to create a merge conflict by changing the same line in both branches. Git will require you to pick what you want to keep.
You can download someone else's git repo if they have made it public. This is called cloning. Be aware that you will only be able to make changes locally to a cloned repo unless the owner gives you permission. If you want to be able to push changes, you will learn that next section with forking.
Your clone list will be prepopulated with your own repos and those from organizations you belong to. To get one from somewhere else on GitHub:
You can also make a copy of a repo to your own remote section; this is called forking a repository. Only do this if you plan to make changes to the code and push your changes remotely.
If you have a project that you have already started and want to add version control, or you have cloned a repo through other means besides GitHub Desktop you can add it.
We have been using a GUI for performing all of our git actions, but you can do everything from the command line too. Using git from the command line give you access to more commands and options than are available with the desktop version, but with great power comes great complexity.
If you would like to learn command line git I recommend starting with Try Git.