-
Notifications
You must be signed in to change notification settings - Fork 3
Git
Jeff Naecker edited this page May 31, 2019
·
7 revisions
We will be using Git to manage our code.
Git is a distributed version control system (VCS). It is used primarily for tracking and merging changes to plain text files such as code and manuscripts. Here is how to get started:
- Install Git. If you have a Mac, this is done already. Just type "git version" in your Terminal program to confirm. If you have a Windows machine, download it here.
- Make an account on GitHub. This is one of many places that you can use to sync your local git repository with one in the cloud so that your collaborators can easily see what you've done.
- Use one or more of the following tutorials to learn the basic of Git and GitHub:
- ProGit online book, chapter 1-3 and 5-7
- This git branching game was very useful.
- A very nice one-page cheat sheet.
- Tutorials from Atlassian.
- Try Git walkthrough from Github.
- Happy Git and GitHub for the useR
- StackOverflow guide to Git for beginners
- Version Control with Git from Software Carpentry
- If you would like a third-party program to help visualize what is going on with Git, try SourceTree.
- Create a folder for the project you are working on and put all the code in there.
- Navigate to that folder in your command line (Terminal if you're on a Mac).
- Run
git init
in the terminal to tell git to start tracking this folder. - Run
git add <filename(s)>
to stage the files you want to git. - Run
git commit -m "First commit"
to commit the files. - On Github, click the plus icon at the top right to add a new repository.
- Copy the URL of the new repository you just created.
- Back in your command line, run
git remote add origin <url-you-just-copied-pasted-here>
. This tells git to push all commits to this place on Github. - Run
git push -u origin master
. This tells git to push the first commit you made to the place on the web you specified in the previous step. - More details here.
- Navigate to the project folder in your command line.
- If you are working on a project in a team, run
git pull origin master
to pull down any changes that others have made since the last time you pushed. - Make changes to the code and save your work.
- Run
git add <filename(s)>
to stage your work. Note that you can usegit add .
to add everything, but be careful if using this that you don't accidentally add files that should not be tracked. - Run git
commit -m "Useful reminder here about what I just did to commit your changes"
. This only updates the local repository. - It is fine to cycle through many add/commit steps, one for each conceptual change you make to the code.
- At the end of the day/work session, run
git push origin master
to update the web repository with your new commit(s).
- Always run
git pull
when you start working, to make sure you have the latest version of a project. - Make sure your commit messages are descriptive but concise. For example, "Added OLS models to Table 1" is better than "Updated analysis".
- Commits should generally focus on one logical purpose/goal. If you make several conceptual changes to the code, this should ideally be multiple commits.
- If there are changes that have been pushed since you last pulled the repo, git will attempt to merge the changes. This can be done automatically if the changes are to different files or parts of a file. If not, git will show you the conflicting lines, and you will have to select the preferred version. More details here.
I recommend using git aliases for increase productivity. For example, instead of typing git commit -m "Message"
, on my machines I can type git cm "Message"
. How did I set this up? There are two ways:
- Edit the file
~/.gitconfig
on your machine in the following way:
[alias]
cm = commit -m
- Or you can do it via the command line with
git config --global alias.cm 'commit -m'
.
More details here as well as many examples. Most of these are fairly niche or advanced. However, I do like the one for printing pretty logs.
Tools
Checklists and Style
Project Management
Other