This is the companion notes to the slides in this repo
Outline
- Install git and create a Github account
- What is Git
- Basic Bash Commands
- Using Git
- Using Github
- Advanced Workflows
- Branching and Merging
- Pull Requests
- Merge Conflicts
- Undoing local changes
- Collaborating with other poeple
- Quality of Life Hacks
- Linux: sudo apt-get install git
- Mac: http://git-scm.com/download/mac (probably already installed)
- Windows: http://git-scm.com/download/win
If you are using Windows, you will be interacting with Git via the Git Bash
application 
If you are using Mac OS or Linux, use the terminal. Ensure that you can run git --version without any errors.
Run the following commands:
git config --global user.name <your name>
git config --global user.email <your email address>
git config --global --add color.ui true
git config --listReplace <your name and your email address as appropiate.
- Github is a code hosting platform for version control and collaboration
- Share code and collaborate on coding projects
- Create a free account at www.github.com
Recommended editors (non-exhaustive)
- Sublime Text: https://www.sublimetext.com
- VSCode: https://code.visualstudio.com
Here are some common and useful bash commands, most of which deal with file and directory manipulation:
ls: Show directory contents, lists names of files.ls -la: Show directory contents (hidden files included) with extra info
mkdir: Creates a directory of the specified name.mkdir foocreates a directory called "foo".
cat: Display contents of a file.cat hello.txt: display contents ofhello.txt
cd: Change directory. Change to certain directory name if provided.cd footakes you to the directory foo.cdChanges to home directory if no directory specified.cd ..takes you up one directorycd ../..to go up tw
pwd: Displays the name of the working directory.touch: Creates a blank file with a specified name.touch file.txt: Creates blank file with namefile.txt
less: View contents of specified file, page by page.less <filenname>
head/tail: Displays the first/ last 10 lines of a file.head <filename>
rm: Removes a specified file. This action is PERMANENT. There is no recycle bin.rm <filename>: remove filerm -r <folder>: remove folder
rmdir: Removes a directory.history: Display a listing of the last commands you've run.cp: Copy specified file to a new named file. Use -r flag to copy a directory.cp file1.txt file_copy.txtcopyfile1.txtand paste asfile_copy.txt
mv: Rename a specified file or directory.mv foo.txt bar.txtrenamesfoo.txtasbar.txt
Ctrl-c: cancel everything (use this when in doubt)
To learn more: https://www.unr.edu/research-computing/the-grid/using-the-grid/bash-commands
- Repositories: Collection of all the files and history of these files
- contains all commits
- 1 Repo <=> 1 Project
- Can live on a local machine or on a remote server (e.g. Github)
- Often shortened to repo
- Commit : record of files that you have changed since last commit
- To add a file to a commit, you need to add it to the staging environment:
git add <filename>
- Staging Environment: (or index) tracks changes that you want to commit
- After using the git add command to add all the files you want, package them into a commit with the git commit command
Working in Git is all about commitments. When you save a file in your editor or create a new file, you move it to the cardboard box.
When you are feeling more confident about your changes, you move them to the the
wooden box. This happens when you run the git add <filename> command.
Once you are ready to commit, you tend move it to the safe, once in there
becomes the history of the repo. This happens when you run the git commit
command.
- HEAD: a reference to the last commit in the currently checked-out branch
- MASTER: (traditionally) the main branch in your project
- Branch: pointer to some commit
- Stage (cache): where you place files to commit to the git repo (wooden box)
- Working Directory: current local directory that you are working on (cardboard box)
To help readers follow along with the examples, the shell session is provided below.
bash-3.2$ mkdir myproject
bash-3.2$ cd myproject
bash-3.2$ touch myfile.txt
# List files in myproject folder
bash-3.2$ ls
myfile.txtAnything after the $ is a command that you should type. Any line that does not
start with $ is terminal output. Any line that starts with # is a comment
and should not entered into the terminal. It is for explaining stuff.
bash-3.2$ cd Desktop/
bash-3.2$ mkdir myproject
bash-3.2$ cd myproject/
bash-3.2$ git init
Initialized empty Git repository in /Users/raynoldng/Desktop/myproject/.git/Check the status of the repo with git status
bash-3.2$ touch raynold.txt
bash-3.2$ ls
raynold.txtCheck the status of the repo:
bash-3.2$ git status
On branch master
No commits yet
Untracked files:
(use "git add <file>..." to include in what will be committed)
raynold.txt
nothing added to commit but untracked files present (use "git add" to track)This means that raynold.txt is in the cardboard box.
bash-3.2$ git add raynold.txt
bash-3.2$ git status
On branch master
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: raynold.txt
You can also use git add . to add all files with one command
bash-3.2$ git commit -m 'This is my first commit'
[master (root-commit) 918dfdd] This is my first commit
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 raynold.txt-m is the message parameter and you supply the commit message after it. If you
were to run git commit, your terminal will launch an editor which displays a
summary of the changes in the commit and you can enter the commit message.
- Add a few more lines to your_name.txt and commit the change
- Create a new file, add some text and commit that change
- The git add command adds the file to staging, how to undo that? (Hint: study the output of git status)
What is Github?
- Largest web-based git repository hosting service. Hosts remote repositories
- Allows for code collaboration with anyone online
- Additional functionality on top of git
- UI, documentation, issues, pull requests/review and more
bash-3.2$ git remote add origin https://github.com/raynoldng/my-repo.git
bash-3.2$ git push -u origin master
# Username and Password prompt
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Writing objects: 100% (3/3), 226 bytes | 226.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://github.com/raynoldng/my-repo.git
* [new branch] master -> master
Branch 'master' set up to track remote branch 'master' from 'origin'.Refresh your Github repo and see your uploaded code!
- Remote: common repository that all team members use to exchange their changes
- Usually stored on a code hosting service like Github or an internal server
git remote –v: list the remote connections that you have to other repositoriesgit remote add <name> <url>: create a new connection to a remote repogit remote rm <name>: remove a remotegit remote rename~ <old-name> <new-name>: rename a remote
- Pulling: downloading commits that don’t exist on your machine from a remote repository
- Pushing: adding your local changes to the remote repository
- Fetch: download changes but do not merge it in yet
If you made it this far you are more or less able to effectively use git and Github for personal projects! \o/
Lets now move on to more advanced workflows.
- Branching: diverge from main line of development and continue to work without messing that main line
- master: main branch in a project
- merging: Once you are done with your line of work, you merge it back into master
A branch diverges from the orignal branch
Once feature is done, you can merge it back into master
To create a new branch: git checkout -b <branch name>
bash-3.2$ git branch | cat
* master
bash-3.2$ git checkout -b test
Switched to a new branch 'test'
bash-3.2$ git branch | cat
master
* testAfter making changes to raynold.txt
bash-3.2$ git add raynold.txt
bash-3.2$ git status
On branch test
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: raynold.txt
bash-3.2$ git commit -m 'made changes to raynold.txt'
[test 952a793] made changes to raynold.txt
1 file changed, 1 insertion(+)- Need to checkout to branch that we are going to merge the branch into
- If there is no changes on the master (current) since the branch out, we can do a fast forward merge
- To push changes to a new branch on github:
git push origin <branch name> origin: shorthand for the remote repository URL
bash-32.$ git checkout master
Switched to branch 'master'
Your branch is up to date with 'origin/master'.
bash-32.$ git merge test
Updating 918dfdd..f4b6e4f
Fast-forward
raynold.txt | 1 +
1 file changed, 1 insertion(+)git does a fast forward when you merge a branch that is ahead of your checked-out branch [1].
About Pull Requests
- To tell others about changes that you’ve pushed to a branch in a repo on Github
- After you open a PR, you can discuss and review the potential changes with collaborators and add follow-up comments before are merged into the base branch
Create a branch and commit some changes to it.
bash-3.2$ git status
On branch my-branch
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: raynold.txt
no changes added to commit (use "git add" and/or "git commit -a")
bash-3.2$ git add raynold.txt
bash-3.2$ git status
On branch my-branch
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: raynold.txt
bash-3.2$ git commit 'add more lines'
error: pathspec 'add more lines' did not match any file(s) known to git
bash-3.2$ git push origin my-branch
Total 0 (delta 0), reused 0 (delta 0)
remote:
remote: Create a pull request for 'my-branch' on GitHub by visiting:
remote: https://github.com/raynoldng/my-repo/pull/new/my-branch
remote:
To https://github.com/raynoldng/my-repo.git
* [new branch] my-branch -> my-branch
You can now see your new branch on Github:

Let's see how to create a pull request and how that leads to a merge
Click on merge pull request. Can look into the other options in your free time.
You can see that the branch has been merged in your Github repo:
Note that in this case a merge commit was created even though we could have fast forwarded. See Gtihub Docs for more info.
bash-3.2$ git checkout master
Switched to branch 'master'
Your branch is up to date with 'origin/master'.
bash-3.2$ git pull
remote: Enumerating objects: 1, done.
remote: Counting objects: 100% (1/1), done.
remote: Total 1 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (1/1), done.
From https://github.com/raynoldng/my-repo
918dfdd..182e7f7 master -> origin/master
Updating 918dfdd..182e7f7
Fast-forward
raynold.txt | 1 +
1 file changed, 1 insertion(+)- Act of switching between different versions of the target entity
git checkout <branch name/commit>git checkout -b <new branch name>: create a new branchgit checkout <branch/commit> <filename>: restore previous version of file
Merge conflicts happen when you merge branches that have competing commits, and Git needs your help to decide which changes to incorporate in the final merge
To resolve a merge conflict, you must manually edit the conflicted file to select the changes that you want to keep in the final merge.
Search your repo for conflict markers <<<<<<<.
<<<<<<< HEAD
Changes from your head branch
=======
Changes from the other branch (incoming branch)
>>>>>>> branch-a
Most IDEs will have tools that help resolve conflicts
Scope of changes (e.g. Soft: index and working dir unchanged)
- Rewinds repo to specified commit
- Left out commits are orphaned and REMOVED next time git performs garbage collection
git reset HEAD~1: go back one commitgit reset HEAD~2: go back two commits
- When things go so wrong and you don’t even want to salvage it
- Removes ALL TRACES of your changes
git reset --hard HEAD: undoes all changes since last commit
When you want to contribute to a github repo or use it as your starting point
- Fork repo
- Clone the forked repo
- Make and push changes
- Make Pull Request
- Go to https://github.com/raynoldng/my-repo
- ⭐ it
- Fork it (you will be redirected to your new repo page)
- Clone into your local machine
- Create a new branch
<your_name> - Add a new file
<your_name>.txtand add some text in it - Commit your changes and push it
- Make the PR request, I will approve it
- Profit
References [1]: https://confluence.atlassian.com/bitbucket/git-fast-forwards-and-branch-management-329977726.html










