A Google engineer, speaking to an audience of Google engineers, once described the newly invented Git as:
"a version control system which is expressly designed to make you feel less intelligent" (source)
... but it gets easier with practice!
Nearly all commands follow a common pattern with 3 main parts.
$ ls -l ~
cd: change directoryls: list all the filesmkdir: make directoryrmdir: remove/delete directorytouch: create a filerm: remove a filepwd: find out the file path of current directory you are in, from the rootterminology note: directory === folder
A little further explanation...
cd folder_name: moves down into the foldercd path_name: moves down into the last folder listedcd ..: moves up one folder levelcd ../../../: moves up three folderlevelscd ~: moves to your home directory, no matter where you areGoal: Create a folder named kittens_project on my desktop
$ cd Desktop
$ mkdir kittens_project
$ cd kittens_project
$ pwd
Version control is a tool that allows you to...
Create anything with other people, from academic papers to entire websites and applications.
Mistakes happen. Wouldn't it be nice if you could see the changes that have been made and go back in time to fix something that went wrong?
Do you have files somewhere that look like this?
Resume-September2016.docx
Resume-for-Duke-job.docx
ResumeOLD.docx
ResumeNEW.docx
ResumeREALLYREALLYNEW.docx
One central server, each client (person) checks out and merges changes to main server
Examples: CVS, Subversion (SVN), Perforce
Each client (person) has a local repository, which they can then reconcile with the main server.
Examples: Git, Mercurial
Set up name and email in gitconfig
$ git config --global user.name "Your Name Here"
# Sets the default name for Git to use when you commit
$ git config --global user.email "your_email@example.com"
# Sets the default email for Git to use when you commit
$ git config --list
Essentially, a Git version of project folder.
Git will track any changes inside of a repository.
$ pwd
$ git status
# should show an error because
# we haven't made it a repository yet!
$ git init
$ git status
git init will transform any folder into a Git repostiory.git status returns no errors, it means your folder has successfully been Git-ified!Each of the states of Git corresponds to an area of the Git repo, so here's some vocab:
kitten.txt
$ touch kitten.txt
git status
$ git status
When you make changes to a file or add a new file but haven't added or committed yet
$ git add kitten.txt
git status
$ git status
When you use git add to let Git know that these are the files you want to 'stage' or prepare for committing.
git status. Make sure that the changes listed represent exactly what you want to commit.
$ git status
$ git commit -m "Add kitten.txt to repository"
Success!
...a commit is like a snapshot of your project at a current time
Git’s default text editor is vim, and you may sometimes find yourself in there accidentally.
Here are some basic commands:
i - puts you in editor mode, so you can type the content of your file:wq - from normal mode, saves and exits:q! - from normal mode, exits without saving$ git log
commit 6853adc0b6bc35f1a8ca0a6aa5e59c978148819b
Author: Your name <you@your-email.com>
Date: Tues May 23 16:01:22 2017 -0700
First commit. Added kitten.txt to repository.
:
Type q to exit the log.
Include a descriptive but succinct message of the changes you have made, in the present tense
$ git commit -m "Add capitalization function for header text"
Main point is: other people need to be able to read your commit history and understand what you were accomplishing at each step of the way
Article: Art of the commitgit init: turns a folder into a Git repositorygit status: checks the status of your filesgit add file_name: adds file to the staging areagit commit -m "your commit message": commits your changesgit log: see your commits so far
Don't worry. Git is your friend.
You made some changes to some files (have not git added or committed yet), and realize you don't want those changes!
Open kitten.txt and make some changes or add something new. Then:
$ git checkout kitten.txt
Look at kitten.txt in your editor: your changes are gone (you've gone back to the previous commit state).
You git add a modified or new file, but realized you don't want it your next commit!
In your text editor, create a new file, and name it possum.txt. Then:
$ git add possum.txt
$ git status
$ git reset possum.txt
$ git status
The file is removed from staging, but your working copy will be unchanged.
You made a commit, but then realize that a piece of code doesn't work, so you just want to uncommit!
Open kitten.txt and make some changes. Then:
$ git add kitten.txt
$ git status
$ git commit -m "Make changes to kitten text file"
$ git reset --soft HEAD~1
$ git status
Your most recent commit is called the HEAD.
Passing git reset the options of --soft HEAD~1 essentially asks to move the HEAD back by one commit (essentially uncommitting your most recent commit).
--soft means you won't lose your changes—they'll just move to staging.
You realize you don't want any of the code in your previous commit, so just getting rid of that commit completely
$ git add kitten.txt
$ git status
$ git commit -m "Make changes to kitten text file"
$ git reset --hard HEAD~1
$ git status
passing git reset the options of --hard HEAD~1 will delete the last specified commit and all the work related to it.
Heads up—there are many, many different ways to undo changes. That's what's powerful about Git. Learn more at Atlassian tutorial
A branch is essentially another copy of your repo that will allow you to isolate changes and leave the original copy untouched. You can later choose to combine these changes in whole or part with the "master" copy, or not.
Branches are good for features!
Can be something as big as adding a new section to a site or an app, to a small functionality (a carousel on the homepage)
Branches are cheap!
So, you want to develop a new feature:
Create a new branch called feature
$ git branch
// you should see only * master
$ git checkout -b feature
$ git branch
// you should see * feature and master
git branch: tells you what branches you have, and * indicates which branch you are currently ongit checkout -b branch-name: the -b creates a new branch, and checkout will hop you over to that branchAdd new lines to kitten.txt
$ git add kitten.txt
$ git commit -m "Adding changes to feature"
$ git log --oneline
$ git branch
Switch to master branch and look at the commit history
$ git checkout master
$ git log --oneline
Switch to feature branch and look at the commit history
$ git checkout feature
$ git log --oneline
Switch to master and merge changes
$ git checkout master
$ git merge feature
$ git log --oneline
When you merge, you create a new commit on the branch you just merged into
Since your code from your feature branch is merged into master, you don't need the branch anymore!
$ git branch -d feature-branch
Hint: Make sure not to be on the branch you are deleting.
git branch : lists all your branchesgit branch branch_name: creates a new branchgit checkout branch_name: switch to another branchgit checkout -b branch_name: creates a new branch and hops over to itgit merge branch_name: merges branch into the current branchgit branch -d branch_name: deletes branch
You will see this in the affected file your text editor
Here are lines that are either unchanged from the common ancestor,
or cleanly resolved because only one side changed.
<<<<<<< yours:sample.txt
Your changes are reflected here in this section.
=======
Their changes are here in this section, in conflict with yours.
>>>>>>> theirs:sample.txt
And here is another line that is cleanly resolved or unmodified.
Go back to your kittens_project on your desktop
Change the first line in kitten.txt in master branch
$ git add kitten.txt
$ git commit -m "Changing kitten in master"
Now change the first line in kitten.txt in feature branch
$ git checkout feature
# open kitten.txt and change the first line
$ git add kitten.txt
$ git commit -m "Changing kitten in feature"
Merge the changes from master into the feature branch
$ git merge master #remember, you are on the feature branch here
You will be notified of a conflict. Go to the file in your editor and fix the problem. Then add and commit your edits.
The merge conflict occurred because the feature branch (which is based off of master) both had divergent histories for the same file.
GitHub is a Web-based service for storing, sharing, and collaborating on projects managed with Git
Like a social media profile for your code
You can add information and change settings for:
The header of a GitHub repository page includes features that GitHub adds on to Git.
The file listing displays information common to all Git repositories.
Each row contains:
GitHub treats some files as special based on their filenames.
These files are designed to help with collaboration.
Explore the contents of the GDI Dayton website repository
Click the ➕ icon in the upper right corner, and select New repository.
Give the repo a name and a description.
Private repositories require a paid account.
GitHub can create some files for you.
In the button bar above the list of files, select Create new file.
Enter a filename, and your file contents.
When you're finished, write a commit message and commit the new file.
We'll talk about committing directly or creating a new branch later.
You can also upload files that aren't text, like images or videos. (These are called binary files.)
In the button bar above the list of files, select Create new file.
Drag or select one or more files, and commit the changes as you would for creating a file.
Navigate to the file you want to change, and select the pencil icon at the top of the file.
The editor view will open, where you can edit the text and commit your changes.
Markdown is a simple markup language that many tools use to convert to styled text.
Different symbols translate to different HTML elements.
GitHub displays all markdown files as styled HTML. Select the Preview changes tab to see how your file will look.
When you create or edit a file, its path is displayed next to the filename.
At the beginning of the filename field, type the name of the folder followed by /.
The folder will be added to the path.
You can do this multiple times, and you can delete folders from the path, too.
Binary files like images can't be edited in GitHub, so you can't change their paths!
Instead, you'll have to:
When you try to make changes in a repo you don't control, GitHub will make a fork.
You can also make a fork by selecting the Fork button at the top of the page.
Forking makes a copy of the repo in your account, where you can make changes to your copy however you like. A fork remembers where it came from.
However, new changes in the original repo (usually called the "upstream") will not affect your fork, and changes in your fork will not affect the upstream repo.
You can propose that changes on your fork be applied to the upstream repo by opening a pull request.
If you try to edit a file in the upstream repo, it will give you this option automatically.
Before you make a pull request, GitHub will compare the changes you're proposing (on the 'head') against the repo you want to change (the 'base').
The comparison looks at a specific branch in each repo. (More on those later.)
The comparison also includes a detailed view of all of the changes, called a diff.
When you create a pull request (or PR), you add a comment, explaining your change to the maintainers.
You can work in a branch without affecting the rest of the repo. For example, the "patch-1" branch has changes that the "master" branch does not.
To start making a group of changes together, create a new branch.
The new branch will include all of the changes from the branch you were on when you created it. Usually you will create the branch from "master," or the main branch.
With your new branch selected, you can make changes as usual. When you commit new changes, you'll have the option to commit to the new branch.
When you start committing to the new branch, GitHub will show a prompt to start a new pull request. You can ignore this until you've made all of your changes.
When you create the pull request from your new branch, the diff will show all commits, to all files.
Create a fork of https://github.com/gdidayton/memehub
To merge a pull request, click the Merge pull request button near the bottom of the Conversation tab of the PR.
The changes will be added to the master branch.
Merge conflicts happen when two commits try to change the same thing, and Git isn't sure which change to use.
This often happens when two people PR changes to the same file, and one gets merged first.
When resolving merge conflicts, Git shows the file with both conflicting changes included, bound in special characters.
Edit the file to incorporate the changes as you like, delete the special characters, and mark the file resolved. You'll commit the revised file.
No more merge conflicts! The pull request is ready to merge.
GitHub issues are where collaborators track bugs, propose features, and discuss changes. They are for conversation, and do not actually change code.
Anyone can open or comment on an issue.
Reading a repo's issues is a great way to find out where you can contribute.
GitHub has several options for searching and filtering issues, like finding all issues with a "newbies-only" label.
If you find an issue you'd like to work on, avoid duplicating work by commenting to say you'll volunteer.
Feel free to ask questions if you need clarification before starting!
Make your first open source contribution to the GDI Dayton resources repo!
Some tools for finding newbie-friendly projects and issues:
Some friendly repos for making contributions with little or no code:
Other resources for learning about Git, GitHub, and open source:
Go forth and contribute!