On Github alimac / git-for-you-and-me
char • o • jay • ka
- Developer and system administrator based in Chicago - Doing Drupal for ~4 years, active in the community for ~2 years - Using git for ~2 yearshistory
undo
experiment
- "Everybody's doing it." - 3 reasons: 1. write project history 1. undo changes (thanks to history) 1. experiment and track changessingle user
collaborative
- single-user: local, or local+remote - collaborative: local+remote - code, websites, books, slidesUpdate to the latest version (2.5.0)
- Download for Windows, comes with OS X, Linux - Tip: update for latest featuresgit config --global user.name "Alina Mackenzie" git config --global user.email hello@alim.ac
git config --global color.ui true git config --global core.excludesfile /path/to/home/directory/.gitignore git config --global core.whitespace trailing-space- have git use colors in output - ignore certain files globally: .DS_Store, *.swp - show trailing whitespace
# Packing List Things to bring.
$ cd packing-list $ git init Initialized empty Git repository in /Users/alimac/packing-list/.git/
Untracked
Tracked
Staged
Committed
Pushed
- Untracked: Git sees file, but doesn't know about it - Tracked: Git knows about file and tracks changes - Staged: Changes are ready to be commited - Committed: Changes to the file are saved to project history - Pushed: Changes are pushed to remote repositoryWhere am I?
What is going on?
What just happened?
- The "three sidekicks" are 3 useful commands for orienting yourself in a project.$ git status On branch master Initial commit Untracked files: (use "git add <file>..." to include in what will be committed) README.md nothing added to commit but untracked files present (use "git add" to track)- tells us which branch are we on - Initial commit means there is no history written yet - shows untracked files - gives hints
$ git add README.md $ git status On branch master Initial commit Changes to be committed: (use "git rm --cached <file>..." to unstage) new file: README.md- git add has 2 uses: - start tracking a file - stage a file that is ready to be committed
# Packing List Things to bring. laptop skirt with pockets toothbrush- adding a few items to the list
$ git status On branch master Initial commit Changes to be committed: (use "git reset HEAD <file>..." to unstage) modified: README.md Changes not staged for commit: (use "git add <file>.." to update what will be committed) (use "git checkout -- <file>..." to discard changes in working directory) modified: README.md- Still initial commit - Some changes are staged, some are not - Git keeps track of sets of changes within the same file
$ git diff diff --git a/README.md b/README.md index daa8d60..da0a4b2 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,7 @@ # Packing List Things to bring. + +laptop +skirt with pockets +toothbrush- git diff shows changes between two versions of a file, changes that have not been staged yet - added lines are prefixed with a + - removed lines are prefixed with a -
$ git commit -m "Initial commit." [master (root-commit) 1c50eb4] Initial commit. 1 file changed, 3 insertions(+) create mode 100644 README.md- To commit is to write something down as history - git commit -m is a shortcut to do the commit in one line - git commit will open a text editor to write the commit message
$ git log commit b9fd7596e44051a99b683a19982c592fdcbca5a5 Author: Alina Mackenzie <hello@alim.ac> Date: Wed Mar 8 12:10:25 2015 -0500 Initial commit.- git log shows commit hash, author, date and message
A set of related and reversible changes that function together as a unit of work. Alina Mackenzie alimac
- up to each developer to decide - when on a team, agree on conventions and document them - beware of unrelated changes, changes outside of the scope - base commits on ticketsgit add -p- git will ask which changes to stage - But it will only ask about changes to tracked files - Use git status to find any new, untracked files
# Packing List Things to bring. ## Technology laptop ## Clothes skirt with pockets ## Toiletries toothbrush- Adding headings to separate items into categories
diff --git a/README.md b/README.md index da0a4b2..9e775a7 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,11 @@ Things to bring. +## Technology laptop + +## Clothes skirt with pockets + +## Toiletries toothbrush Stage this hunk [y,n,q,a,d,/,s,e,?]?- y: yes, n: no, q: quit - a: stage this hunk and all later hunks - /: search for a hunk matching the given regex - d: do not stage this hunk or any of the later hunks in the file - s: split, e: edit, ?: help
Stage this hunk [y,n,q,a,d,/,s,e,?]? s Split into 3 hunks. @@ -2,4 +2,5 @@ Things to bring. +## Technology laptop Stage this hunk [y,n,q,a,d,/,j,J,g,e,?]?- changes are split into smaller chunks, or hunks - git add -p: Stage only the changes you want. - git reset -p: does the same, but for unstaging.
git config --global core.editor sublimeChange default editor to Sublime Text, Atom
Add short descriptive subject line. Longer description in the form of paragraphs that include reasons for the change, reference tickets or other commits.- Imagine saying "This commit will..." before writing the message - Short subject, blank line, longer message - Prefix with a ticket number - Be consistent
git add remote origin git@bitbucket.org:alimac/packing-list.git- Add a remote repository - "origin" is a convention, use any meaningful name
git push -u origin master
$ git reset HEAD README.md $ git reset -p
$ git checkout -- README.md
$ git commit --amend
$ git reset --soft HEAD~1 $ git reset HEAD~1- You can always amend the last commit - Undo a commit and keep files staged - Undo a commit and unstage files
$ git reset --soft HEAD~3 $ git reset HEAD~3
$ git revert 617ff153- Undo last 3 commits - Undo last 3 commits and unstage files - Revert a specific commit. This will create a new commit.
Don't do it.
- If you pushed to remote repository, use revert to undo. - If you are the only contributor, git push -f <remote> <branch> will force an updateUndoing changes in Git Emma Jane Hogbin Westby emmajane
$ git checkout master $ git merge test- git merge is one way we can integrate changes from one branch to another - When we do a merge, a couple of things could happen
$ git checkout test $ git rebase master
Things to bring.- What if this line were modified in different ways, by two different contributors?
Things to bring on a trip.
Things to bring on an expedition.- Both contributors modify the same line, to add different words. - When the second person tries to merge or rebase, git will tell them there is a conflict.
<<<<<<< HEAD Things to bring on a trip. ======= Things to bring on an expedition. >>>>>>> expedition-branch- 7 less-thans, 7 greater thans, separated by 7 equals signs - Replace text between markers with your resolution - Delete markers
Things to bring on a trip.
git add README.md