Version control is a tool that allows you to...
Create anything with other people, from academic papers to entire websites and applications.
For example, when you edit a file, version control can help you determine exactly what changed, who changed it, and why.
If something goes wrong, you can revert the changes and go back to the last version (checkpoint) that worked.
Do you have files somewhere that look like this?
Resume-September2016.docx
Resume-for-Duke-job.docx
ResumeOLD.docx
ResumeNEW.docx
ResumeREALLYREALLYNEW.docx
You invented your own version control.
There is one central server. Each client (person) checks out and merges changes to main server.
Examples:
Each client (person) has a local repository, which they can then reconcile with the main server via push and pull.
Examples:
Set up name and email in git config. Let's do it together.
$ 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@powercoders.org"
# Sets the default email for Git to use when you commit
$ git config --global init.defaultbranch "main"
# Sets the default branch to the name "main" instead of "master"
$ git config --global core.editor "code --wait"
# Sets the default editor to VSC
$ git config --list
.gitignore
.
$ cd Desktop
# create the folder
$ mkdir poco
# next go inside
$ cd poco
$ 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.
Each of the states of Git corresponds to an area of the Git repo, so here's some vocab:
index.html
git status
$ touch index.html
$ git status
Status you get when you make changes to a file or add a new file but haven't added or committed yet
1: @@ -10,6 +10,5 @@ 2: <p>This was one of my 3: multiline sample paragraphs</p> 4: 5: -<p>This text will be deleted.</p> 6: - 7: -<p>This text will be updated.</p> 8: +<p>This text will be updated 9: + with some extra content.</p> 10: 11: +<p>This one is completely new.</p> 12: + 13: <p>One more line of text.</p> 14:
@@
is the chunk
header
-m,n
= Starts at line
m
, continues for
n
lines
+o,p
= Starts at line
o
, continues for
p
lines
Space
This line is not
changed-
= This line was
deleted+
= This line was
addedgit add
plus filename.
git status
.
$ git add index.html
$ git status
Status you get when 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 index.html to repository."
Success!
git add
a new file, we
tell Git to add the file to the repository to be
tracked.
git commit
saves the
changes made to a file, not the file as
a whole. The commit will have a unique ID so we can track which
changes were committed when and by whom.
...a commit is like a snapshot of your project at a current time
$ git log
commit 91ee3768d599ab7223cbd4247c8752fc1636edad (HEAD -> main)
Author: susanne susanne.koenig@powercoders.org
Date: Fri Aug 30 16:36:09 2019 +0200
First commit. Added index.html to repository.
On Mac, type q
to exit the log.
HEAD refers to the most recent commit on the current branch.
A branch is the name for a separate line of development, with its own history.
By creating a repo, a branch called main is automatically created.
Include a short but precise message of the changes you have made, in the present tense.
$ git commit -m "Add capitalization function for header text"
Other people need to be able to read your commit history and understand what you were accomplishing at each step of the way.
Article to read: 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
farWhen files have been modified in the working directory, it might be helpful to easily view what changed to double check that they're what we want.
$ git diff
This shows the difference in the content of the files in the working directory and the ones in the staging area (also called index).
Don't worry. Git is your friend.
You made some changes to some files and realize you don't want those changes. The files have not git added or committed yet.
Open index.html in Visual Studio Code and add a new line of text. Then:
$ git restore index.html
Look at index.html in your editor: your changes are not there anymore. 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 Visual Studio Code create a new file and name it test.txt. Then:
$ git add test.txt
$ git status
$ git reset test.txt
$ git status
The file is removed from staging, but your working copy will be unchanged.
You git add
a modified or new
file, but realized you don't want it your next commit.
In Visual Studio Code create a new file and name it test2.txt. Then:
$ git add test2.txt
$ git status
$ git restore --staged test2.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 index.html and make some changes. Then:
$ git add index.html
$ git status
$ git commit -m "Make changes to index 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 index.html
$ git add index.html
$ git status
$ git commit -m "Make changes to index 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 "main" 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 * main
$ git switch -c feature
$ git branch
// you now see * feature and below main
git branch
: tells you what
branches (with commits) you have, and
*
indicates which branch you
are currently on
git switch -c branch-name
: the
-c
creates a new branch, and
switch
will hop you over to
that branch
git checkout -b branch-name
:
the -b
creates a new branch,
and checkout
will hop you over
to that branch
Add new lines to index.html
$ git add index.html
$ git commit -m "Adding changes to feature"
$ git log --oneline
$ git branch
Switch to main branch and look at the commit history
$ git switch main
$ git log --oneline
Switch to feature branch and look at the commit history
$ git switch feature
$ git log --oneline
Switch to main and merge changes
$ git switch main
$ 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
main
, you don't need the branch anymore!
$ git branch -d feature
Hint: Make sure not to be on the branch you are deleting.
git switch -c [branch_name]
: creates a new branch and hops
over to itgit switch [branch_name]
: switch to another branchgit branch
: lists all your
branchesgit merge [branch_name]
: merges branch into the current branch
git branch -d [branch_name]
: deletes branchThere could be a merge conflict.
Go back to your poco project on your desktop.
Change the first line in index.html in the
main branch
$ git add index.html
$ git commit -m "Changing index page in main"
Now change the first line in index.html in feature branch
$ git switch feature
# open index.html and change the first line
$ git add index.html
$ git commit -m "Changing index page in feature"
Merge the changes from main into the feature branch
$ git merge main
#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 main) and the main branch both had divergent histories for the same file.
init
status
add
commit
log
restore
switch
branch
merge
... and many more