Introduction to Git

(version control)

Dr Dorival Pedroso

Version control? What?

fade-right | It records changes to files over time; fade-right | Specific versions can later be recalled; fade-right | BADAPPROACH: create copies of files manually; fade-right | BADAPPROACH: what about differences?; fade-right | GIT to the rescue!; fade-right | PLUS: multiple co-workers!; fade-right | PLUS: nonlinear development (people, branches);

Configure Git for the first time


git config --global user.name "Your Name Comes Here"
git config --global user.email you@yourdomain.example.com
|

Learning Git

|

Commands

; | Start a new repository | git init; fade | Hidden directory (.git) is created | GHOST; fade | Check the current status | git status; fade | Tell Git to track a file, or ... | GHOST; fade | ... staging a change | git add hello.py; fade | abandon staged changes (reset staging area) | git reset; fade | Tell Git to record what you just did (with a nice message) | git commit; fade | List all changes (commits) | git log;
| Change your file hello.py using any text editor (e.g. gvim) | GHOST; fade | Check modifications (differences) |git diff; fade | Stage further modifications | git add hello.py; fade | View staged modifications | git diff --staged; fade | Commit staged changes | git commit;

fade | Suppose we want to work on another branch ... before that | GHOST; fade | If you don't want to commit just yet | git stash; fade | Ok, let's get the stash back | git stash apply;
| New development should occur within a new branch | GHOST; fade | Checkout and create a branch | git checkout -b devel; fade | Create / change file(s) in the new branch | GHOST; fade | What branch? | git branch; fade | Change to the destination branch | git checkout master; fade | Merge one file into current branch | git merge devel vector.py; fade | Merge all files into current branch | git merge devel; fade | Merge specific commits into current branch (where 4ffeb20e is the SHA) | git cherry-pick area 4ffeb20e;
|

Summary

|

Commands:

; | Initialise repository (repo) RA (1) | git init; | Type code RA (2) | echo "# TODO" GREATERTHAN main.py; | Add modifications to staging area RA (3) | git add main.py; | Commit changes (message) RA (4) | git commit; | Switch to a new branch RA (5) | git checkout -b vector; | Type code RA (6) | echo "# TODO" GREATERTHAN vector.py; | Add new file to branch RA (7) | git add vector.py; | Commit changes RA (8) | git commit; | View log of commits (sha) RA (9) | git log; | Change file RA (10) | echo "# TODO" DOUBLEGT vector.py; | Stash changes RA (11) | git stash; | Switch to master branch RA (12) | git checkout master; | Switch to a new branch RA (13) | git checkout -b area; | Add new file to branch RA (14) | echo "# TODO" GREATERTHAN area.py; | Add modifications RA (15) | git add area.py; | Commit changes RA (16) | git commit; | Go back to vector branch RA (17) | git checkout vector; | Recover stash RA (18) | git stash apply;

More on branches

Merging master into a branch

The procedure is the same as for merging a branch into master


git checkout mybranch
git merge master

Publishing your work

After creating a repository (repo) in GitHub, ...

Tell Git that you want to send your repo there:

git remote add origin https://github.com/user/repo.git

Then send your files there (push them)

git push -u origin master

The -u flag sets the repo so next time you can type git push only


Eventually someone changed some file there; thus, download the changes

git pull origin master

Conclusions

Git is Awesome!!!

And GitHub is great!!!