Intro to git workshopThu 21 February 2019
A 4 hours introduction to git workshop for my PhD fellas. Based on an old git crash course blog post.
- Keep your projects organized.
- Collaborate with others.
- Get involved with open source.
Introduction to the command line. From now on, the rest is done there.
Configuring git for the first time
git config --global user.name "Your name here" git config --global user.email "[email protected]" git config --global core.editor nano
When git manages a directory on your computer we call this directory a local git repository.
There are two ways to start to work in such a repository:
- git clone https://github.com/some_user/some_repo.git to download a project into a new directory some_repo.
- git init to make the current directory a git repository.
Unstaged ➜ staged ➜ committed
Remember! you can always see the current state and the staging / unstaging commands with git status, so don't try to memorize them.
When you are satisfied with the changes commit them:
git commit -m "an informative message describing your change"
- Create a directory called jokes.
- Make it a local git repository.
- Create a file called jokes.txt in that directory.
- Write some funny stuff.
- Add and commit. Try to have an informative commit message.
- Repeat steps 4-5 as many times as you want 🙃.
Explore your repo
git log # see the history git diff # see the unstaged changes git diff --staged # see the staged changes git show <COMMIT_HASH> # see the changes in a commit
Collaborating through GitHub
GitHub is a place to share and collaborate on git repositories. Go head and create an account!
Your local git repository can be "linked" to remote repositories. To see them run git remote (or with extra info using git remote -v). If you cloned an existing repository you should see one remote, called origin, in the list. Otherwise, create a new GitHub repository and add it as a remote with:
git remote add origin https://github.com/you/your_repo.git
Naming a remote origin is just a convention. It has no special meaning.
To get the latest changes (commits) from your remote run:
git pull origin master
What's master? The name of the default branch. More on that later.
If you have permissions to push code to the remote repository you can update the remote with your changes (commits) by running:
git push origin master
Remember! Always pull before you push to avoid unnecessary conflicts.
Assuming that you already have a local repository with a remote (called origin) that you can push code to, a simple but complete workflow might look like that:
git pull origin master # to get the latest changes # work work work... git status # to see all of the changes you did git diff # optional but handy git add FILE_WITH_CHANGES # repeat as necessary git commit -m "your message" # commit the changes to the repository git push origin master # to upload your changes
- Create a new GitHub repo, called jokes.
- Add it as a remote for your local jokes repo.
- Push your jokes to GitHub.
Forks and pull requests
If you don't have permissions to push code to a remote repository.
- Fork the repository. It will copy the remote repository to your account.
- Add your fork as a remote.
git remote add mine https://github.com/you/your_repo.git
- Work on your fork.
- Send your commits to the owner(s) of the projects using a pull request.
- Fork this repository.
- Clone your fork so you'll have a local git repository on your computer.
- Answer some questions in one of the files in mat-trivia/trivia/.
- Stage the changes, commit, and push to your remote repo.
- Submit a pull request