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!
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
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!As you make changes in your repo, you can tell Git how to treat those changes.
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 "First commit. Added kitten.txt to repository."
Success!
...a commit is like a snapshot of your project at a current time
$ 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 farEach of the states of Git corresponds to an area of the Git repo, so here's some vocab:
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
You still have the change in the staging area for kitten.txt
$ 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 -b checkout 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 -b checkout 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 -b checkout branch_name
: creates a new branch and hops over to itgit checkout branch_name
: switch to another branchgit branch
: lists all your branchesgit merge branch_name
: merges branch into the current branchgit branch -d branch_name
: deletes branchYou 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.