SVN Style
$ svn checkout svn+ssh://svn@example.com/svn/trunk
$ svn log | less
$ svn add <file>
$ svn rm <file>
$ svn commit -m 'message'
GIT Style
$ git clone ssh://git@example.com/path/to/git-repo.git
$ git log
$ git add <file>
$ git rm <file>
$ git commit -m 'message'
$ git push
Wait a minute...
This looks a lot like SVN!!
sort of...
$ svn add <file | directory | etc>
$ svn commit -m 'message'
schedules files, directories, or symbolic links in your working copy for addition to the repository
send your changes from the working copy to the repository
$ git add <file | directory | etc>
$ git commit -m 'message'
$ git push
updates the [staging area] using the current content found in the working tree, to prepare the content staged for the next commit
stores the current contents of the [staging area] in a new commit ... describing the changes
Updates remote refs using local refs, while sending objects necessary to complete the given refs.
GIT has an extra step, why would we use this garbage?
$ cd path/to/where/you/want/to/start
$ git init
$ git remote add origin https://example.com/path/to/git/repo.git
OPTIONAL
$ git clone ssh://git@example.com/path/to/git-repo.git
Every git project has a hidden folder called .git containing everything git needs to track your project
$ git status
Git status is your go-to command to see the state of the files in your project
$ git add
When you git add files, they're added to a special place called the staging area
touch README.md
git status
git status
git add README.md
git status
PROTIP:
// stage all files for commit
git add .
OR
git add -A
$ git commit
git commit takes a snapshot of the state of your files
git commit -m 'added README file to the project'
git status
COMMIT PROTIP:
// use the -am flags to stage and commit all TRACKED files
git commit -am 'message'
EXAMPLE:
but first....
$ git log
Use log to see the full commit history of the current branch
$ git commit --amend
$ git log
$ git log
$ git status
$ git push
Use push to send your changes upstream to the remote
// push the branch to the remote
git push <remote> <branch-name>
// set the upstream link so you can run `git pull` with no arguments
git branch -u <remote>/<branch-name>
PROTIP:
// push to the remote and set the upstream branch in one command
git push -u <remote> <branch-name>
DO THE PUSH:
git push -u origin master
Usage
$ git rm
Use rm to stage the deletion and termination of tracking for the specified file
$ git rm <file>
$ EDITOR .gitignore
When do you branch?
A lightweight, movable pointer to a specific commit hash
Local to your machine until you push them somewhere
A way to develop changes outside your main code flow
master is the default branch of every git project
(unless a narcissist deletes it)
$ git branch
$ git branch -a
$ git branch <branch-name>
$ git checkout -b <branch-name>
$ git branch -d <branch-name>
$ git branch -D <branch-name>
$ git push origin :<branch-name>
Branches point to the latest commit
The latest commit is called the tip or HEAD of the branch
Branching + Committing often lets developers defer synchronizing upstream until they’re at a convenient break point
$ git checkout <filename>
$ git checkout <branch-name>
AKA DETACHED HEAD STATE
$ git checkout <commit-hash>
$ git checkout -b <some-new-branch-name>
Save a quick reference to this commit as a branch
the process of moving a branch to a new base commit
$ git rebase <base>
<base> CAN BE ANY KIND OF COMMIT REFERENCE
(ID, BRANCH NAME, TAG, RELATIVE <HEAD> REFERENCE)
- Garrett Nay
Rebasing a branch locally that is being tracked remotely leads to an interesting problem
Notice that the message here tells you to pull
$ git pull
$ git pull
TL;DR: Collaboration Pushes vs History Rewrites
$ git push --force origin <branch-name>
$ git push --force-with-lease origin <branch-name>
Never rebase commits or branches that other people have consumed/are consuming
$ git add -A
$ git rebase --continue
FORCE PUSH!!
Conflicts are unavoidable, but they don't have to be scary
- confucius
(probably)
// revert the latest commit of the current branch
$ git revert HEAD
// revert a specific commit from the history
$git revert <commit-hash>
$ git log
$ ls
$ git revert 592ad5fd8b7ae73d72907743a02eab0b92a2679b
$ git log
$ ls
Use revert to get undo commits already consumed by the rest of the team
$ git reset <file>
$ git reset
$ git reset --hard
$ git reset <commit | HEAD>
$ git reset --hard <commit | HEAD>
Tags in git are lightweight references that point to [the] SHA hash of a commit. Unlike branches, they are not mutable and once created should not be deleted
// tag the latest commit
$ git tag <tag-name>
// tag a specific commit
$ git tag <tag-name> <commit-hash>
// tag the latest commit
$ git tag -a <tag-name> -m 'message'
// tag a specific commit
$ git tag -a <tag-name> <commit-hash> -m 'message'
$ git push --tags
$ git fetch --tags || git pull
$ git tag -l
$ git tag -d <tag-name>
$ git push origin :refs/tags/<tag-name>
$ git checkout <tag-name>
Tags are easily referenced bookmarks to specific commits