Click here to load reader
Click here to load reader
Jul 11, 2020
● Distributed Version Control System (VCS); ● Created by Linus Torvalds, to help with Linux development;
What is git?
Why should I use a VCS?
Repositories Types of repositories:
● Private - only you and the selected contributors will see the content ● Public - everybody on the internet will be able to see the content ● But, there are also other options!
How to create a repository:
● git init ● Through an interface on the selected host (GitHub)
Work area? Staging area?
Git Add Command: git add ;
● Can be folders (f.e. git add ./folder) ● Can be multiple files (git add ) ● Or you can be lazy! (git add --all OR git add .)
Git Commit Command: git commit;
● It will open a text editor so you can insert a commit message; ● Or, you can use the “-m” flag (f.e, git commit -m “message”); ● You can sum up both previous commands:
○ git add -a -m “”;
Git Push Command: git push
● Pushes all the committed files from the staging area to the repository
Git Fetch Command: git fetch
● Retrieves the files from the repository into the Working Area
Git Pull Command: git pull
● Actually, it’s just git fetch +git merge ● Use the “--rebase” flag to rebase the commits instead of merging
Git Rebase Command: git rebase;
● Tries to order the commits, placing your commits at the top so you can push them to the remote repository.
● Sometimes some flags are required (--continue, --skip or --abort) depending on the changes made by the other contributors and in what do you want to commit.
Other commands ● Git Status (Command: git status) - shows the status of your files (added
files, files that need to be added or removed, whether you are behind in the repository or you just need to push);
● Git Reset (Command: git reset HEAD) - resets your commit into the last state of the system (you’ll typically need to add again the files);
● Git Checkout (Command: git checkout -- ) - undo the changes in the selected file, resetting it to the previous commit state;
● Git Rm (Command: git rm ) - removes the selected file from being tracked by Git;
Branches ● Branches are a great way to to split development of a project:
○ You can create a branch to clean bugs and stabilise the codebase, and another to continue the development of a functionality.
○ However, branches can lead to headaches! Problems with merging the branches, problems with bugs on one branch, etc…
● So, we recommend that you stay with a single branch, and resort to rebasing the commits instead of merging;
● Command: git branch; ○ You can list the branches existent (git branch); ○ You can create a new one with (git branch ); ○ And you can also delete a branch (git branch -d );
● Command: git checkout; ○ Use it with branches:
■ Checkout a branch (git checkout ); ■ Create a branch and switch to that one (git checkout -b );
● Now, to merge the branch… ● Command: git merge;
○ Merge another branch with the actual branch (git merge ); ● A use case scenario:
○ git checkout -d new_feature; ○ git add new_file; ○ git checkout master; ○ git merge new_feature;
Blame ● Command: git blame ; ● Allows you to show who was the author of each line of a file and in what
commit it was last placed
Grep ● Command: git grep ● This command finds all the files with string that match the given pattern ● Useful flags:
○ -n Show the line number where the string matches the pattern ○ -i Ignore case of the letters in the pattern ○ -l List all the files with strings that match the pattern
Git Tags Command: git tag ;
● There are two kinds of Tags: ○ Annotated, complete with checksum and integrity checking (f.e. git tag -a ); ○ Lightweight, just a simple pointer to a commit;
● You have to manually push the Tags (git push --tags);
Now, let’s talk about Github!
We’ve got a great help... ● Unlimited private repositories; ● Money to use on cloud services like Amazon Web Servers, Digital Ocean,
Bitnami ● Software like Unreal Engine, Atom, Travis CI
A little about project management... Github gives you two powerful tools that helps you manage your project: ● Wiki - lets you document your code and provide additional information to
anyone that needs; ● Issue Tracker - lets you manage which responsibilities are in a project, assign
them to a developer, maintain an idea of what needs to be done;
Github’s motto: Social Coding! ● Github lets you host free public repositories;
○ They want you to share your code and get help from the community!
● Open source your projects! ○ But how do I manage which commits are beneficial? ○ Pull Requests!
Share snippets of code: Gist ● Gist is the snippet tool of Github;
○ Easy to use; ○ Share it with whom you want; ○ Formats like code; ○ Can be public, or you can make it private;
Github Pages ● Serve a page about your project using Github!
○ Github pages allow you to setup a blog using Jekyll; ○ You can setup a custom URL!
● So, how do you create one? ○ Create a public repository with the name (.github.io); ○ Done!
Markdown ● What is Markdown?
○ Text-to-html conversion tool;
● Github supports Markdown in its pages (using Github-flavoured Markdown);
○ Commit messages; ○ Wikis; ○ Issues;
You can also use SSH! You can create Webhooks! ● You can also setup SSH in order to push the commits!
○ Don’t need to insert to credentials for every push; ○ Allows automation of pushes and pulls (we always use Fabric, a Python module, to push
our commits onto our repositories);
● Webhooks let you modify something when there is an event: ○ For example, send a new commit onto Travis CI (a continuous integration service);
Using github for your projects ● Ask for the student pack (https://education.github.com/pack) ● Create private repositories, you can setup one repository per project ● Add your collaborators (if you have any) and you’re ready to go
Good practices of working with git ● Make short but meaningful commits (a new function, for example); ● Associate a descriptive message so you can know what was changed; ● Don’t push code that doesn’t work, except in some special situations; ● Distribute the work with issues or other collaboration tools (e.g. Trello); ● Don’t hold your commits too long so your colleagues may know what is
happening; ● Look at your colleague’s commits, you may find there some bugs to fix or
the solution to a problem that you don’t know how to solve;
Finally, to wrap up... ● There are multiple GUI Clients:
○ SourceTree (Windows); ○ GitCola (Linux); ○ Github Desktop (Windows, Mac); ○ GitKraken;
● Other kinds of VCS: ○ SVN; ○ Subversion; ○ CVS;
● Other Hosts: ○ Gitlab; ○ Bitbucket;
Now let’s test everything we learned... ● Create a github account (if you haven’t one yet); ● Pair with a colleague, one person of the pair should create a public
repository ● Both of you should clone the created repository; ● One person should create a file and push it to the repository (remember
the steps: git add, git commit, git push); ● The other person should pull the file from the repository, change it and
push again; ● Finally, let’s simulate some conflicting changes. Both of you should change
the file and commit it. When the second person tries to push the file, both of you will have to work together to solve the conflict.
Thanks for coming!