Set up your git install with git confing
, start by telling who you are
$ git config --global user.name "Juan Perez"
$ git config --global user.email "jperez@treschanchitos.edu"
Try it yourself (more on how to configure git here)
We will start by working on our very first project. To do so, you are required to start using Git and Github so you can share your code with your team. For now, you can keep things local and skip Github for now. For this exercise, you need to
Create an new folder with the name of your project (you can try my-first-repo
)
Initialize git with git init
command.
Create a README.md
file and write a brief description of your project. We have included an example Rmd file that you can include, if you want, here (and see the rendered output of this Readme file here).
Add the file to the tree using the git add
command, and check the status.
Make the first commit using the git commit
command adding a message, e.g.
git commit -m "My first commit ever!" $
And use git log
to see the history.
Note 1: We are assuming that you already installed git in your system.
Note 2: Need a text editor? Checkout this website link.
The following code is fully executable (copy-pastable)
# (a) Creating the folder for the project (and getting in there)
mkdir ~/my-first-repo
cd ~/my-first-repo
# (b) Initializing git, creating a file, and adding the file
git init
# (c) Creating the Readme file
echo An empty line > README.md
# (d) Adding the file to the tree
git add README.md
git status
# (e) Commiting and checkout out the history
git commit -m "My first commit ever!"
git log
Ups! It seems that I added the wrong file to the tree, you can remove files from the tree using git rm --cached
, for example, imagine that you added the file class-notes.docx
(which you are not supposed to track), then you can remove it using
git rm --cached class-notes.docx $
This will remove the file from the tree but not from your computer. You can go further and ask git to avoid adding docx files using the .gitignore file
Now that you have something to share, your team-mates are asking you to share the code with them. Since you are smart, you know you can do this using something like Gitlab or Github. So you now need to:
Create an online repository (empty) for your project using Github.
Add the remote using git remote add
, in particular
git remote add origin https://github.com/[your user name]/my-first-repo.git $
The use the commands git status
and git remote -v
to see what’s going on.
git push
like this:git push -u origin master $
You should also check the status of the project using git status
to see what git tells you about it. Origin is the tag associated with the remote repo su setted up, while master is the tag associated with the current branch of your repo.
Recommended: Complete GitHub’s Training team “Uploading your project to GitHub”
For part (b), there are a couple of solutions, first, you could try using your ssh-key (if you set it up)
# (b)
git remote add origin git@github.com:gvegayon/my-first-repo.git
git remote -v
git status
Otherwise, you can use the simple URL (this will prompt user+password) every time you want to push (and pull, if private).
# (b)
git remote add origin https://github.com/gvegayon/my-first-repo.git
git remote -v
git status
For the first git push
, you need to specify source (master) and target (origin) and set the upstream (the -u
option):
# (c)
git push -u origin master
git status
The --set-upstream
, which was invoked with -u
, will set the tracking reference for pull
and push
.
Example exctracted directly from Pro-Git (link).
# ignore all .a files *.a # but do track lib.a, even though you're ignoring .a files above !lib.a # only ignore the TODO file in the current directory, not subdir/TODO /TODO # ignore all files in any directory named build build/ # ignore doc/notes.txt, but not doc/server/arch.txt doc/*.txt # ignore all .pdf files in the doc/ directory and any of its subdirectories doc/**/*.pdf
Git’s everyday commands, type man giteveryday
in your terminal/command line. and the very nice cheatsheet.
My personal choice for nightstand book: The Pro-git book (free online) (link)
Github’s website of resources (link)
The “Happy Git with R” book (link)
Roger Peng’s Mastering Software Development Book Section 3.9 Version control and Github (link)
Git exercises by Wojciech Frącz and Jacek Dajda (link)
Checkout GitHub’s Training YouTube Channel (link)