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 directory
ls: list all the files
mkdir: make directory
rmdir: remove/delete directory
touch: create a file
rm: remove a file
pwd: find out the file path of current directory you are in, from the root
terminology note: directory === folder
A little further explanation...
cd folder_name: moves down into the folder
cd path_name: moves down into the last folder listed
cd ..: moves up one folder level
cd ../../../: moves up three folderlevels
cd ~: moves to your home directory, no matter where you are
Goal: Create a folder named
kittens_project on my desktop
$ cd Desktop
$ mkdir kittens_project
$ cd kittens_project
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 "email@example.com" # Sets the default email for Git to use when you commit
$ git config --list
By default Git is set up to use Vim as the text editor.
(esc + :q or :q! to get out of Vim)
$ git config --global core.editor "nano" # Sets the default text editor to Nano
You can find commands for other text editors in these instructions.
Install Git and set your user name and email.
BONUS LEVEL: If you want to, change your default text editor.
Essentially, a Git version of project folder.
Git will track any changes inside of a repository.
$ git status # should show an error because # we haven't made it a repository yet! $ git init $ git status
git initwill transform any folder into a Git repostiory.
git statusreturns 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.
$ touch kitten.txt
$ 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
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."
...a commit is like a snapshot of your project at a current time
$ git log
commit 6853adc0b6bc35f1a8ca0a6aa5e59c978148819b Author: Your name <firstname.lastname@example.org> Date: Tues May 23 16:01:22 2017 -0700 First commit. Added kitten.txt to repository. :
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 wayArticle: Art of the commit
git init: turns a folder into a Git repository
git status: checks the status of your files
git add file_name: adds file to the staging area
git commit -m "your commit message": commits your changes
git log: see your commits so far
Each 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).
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.
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
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 on
git checkout -b branch-name: the
-bcreates a new branch, and
checkoutwill hop you over to that branch
Add 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 checkout -b branch_name: creates a new branch and hops over to it/li>
git checkout branch_name: switch to another branch
git branch: lists all your branches
git merge branch_name: merges branch into the current branch
git 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.