Table of Contents
First create the project in the git repository. Then copy the url. (possibly from https://git-scm.com/book/en/v2/Git-on-the-Server-Setting-Up-the-Server and https://git-scm.com/docs/git-shell)
This is a good method of limiting git to a non-interactive shell. Copy keys to the git user authorized_keys2 file in /l/git/.ssh on lycoming.
On the client:
git init testapp cd testapp/ git config --global user.name "Stephen R Edwards" git config --global user.email "steve.edwards@vt.edu" touch README.md git add README.md git add . git commit -m 'First commit' git remote add origin git@git.it.vt.edu:stedwar1/testapp.git git push -u origin master
On git host server, initialize the repo:
git init --bare stedwar1/testapp chown -R git:git stedwar1/
On the client:
git remote add origin ssh://git@lycoming.aoe.vt.edu:32722/l/git/stedwar1/testapp git push -u origin master
markdown
Headlines
##
Blockquote
>
Blockquote in a blockquote
>>
Lists
+ or -
4 spaces for code indent
back tics for code inline
test `code` inline
tab
Use delimited back tics to render a back quote
test \'code\'
HTML
<span class="special">
clone with all branches
git clone --bare https://github.com/planetoftheweb/angular.git .git git config --bool core.bare false git reset --hard git branch git status git stash && git checkout 01_03
git essentials (lynda.com)
Configuration
- System
git config --system /etc/gitconfig Program Files\Git\etc\gitconfig
- User
git config --global ~/.gitconfig $HOME\.gitconfig git config --global user.name "Steve Edwards" git config --global user.email "steve.edwards@vt.edu" git config --list git config user.name git config user.email cd ~ ls -la cat .gitconfig git config --global core.editor "mate -wl1" #(wait on line one) git config --global color.ui true cat .gitconfig git config --global core.excludesfile ~/.gitignore_global
- Project
git config my_project/.git/config
git-completion
$ curl -OL https://github.com/git/git/raw/master/contrib/completion/git-completion.bash $ mv ~/git-completion.bash ~/.git-completion $ cat >> .bash_profile if [ -f ~/.git-completion.bash ]; then source ~/.git-completion.bash fi ^d
git help
git help git help log man git-log
3. Getting Started
git initialize project
First create a new folder then cd into that folder
cd ~/Documents mkdir first_git_project cd !$ git init
creates the ./.git folder that tracks all the changes
ls -la .git
Only the config file should be edited, but even git commands can edit this file.
git keeps all changes here, unlike svn that creates an svn file(?) in every directory.
Delete the .git and the changes are no longer being tracked.
git tracking changes
create a file, first_file.txt, in the project directory
touch first_file.txt git add . git commit -m "Initial commit"
- make changes
- add the changes
- commit changes to the repository with a message
writing commit messages
- short single-line summary (less than 50 characters)
- optionally followed by a blank line and a more complete descrtiption
- keep each line to less than 72 charactes
- write commit messages in the present tense, not past tense
- “fix bug” or “fixes bug”, not “fixed bug”
- bullet points are usually asterisks or hyphens
- can add “ticket tracking numbers” from bugs or support requests
- can develop shorthand for your organization
- “[css,js]”
- “bugfix: ”
- “#38405 - ”
- Be clear and descriptive
- Bad: “Fix typo”
- Good: “Add missing > in project section of HTML”
- Bad: “Update login code”
- Good: “Change user authentication to use Blowfish
- Bad: “Updates member report, we should discuss if this is right next week”
t23094 - Fixes bug in admin logout When an admin logge out of the admin ares, they could not log in to the memebers area because their session[:user_id] was still set to the admin ID. This patch fixes the bug by setting session[:user_id] to nil when any user logs out of any area.
Viewing commit log
git log
limit number of commit messages
git log -n 5
time periods
git log --since=2012-06-14 git log --until=2012-06-15 git log --author="Kevin" git log --grep="Init" (global reg ex search)
4. Git Concepts and Architecture
Three tree architecture
- Two tree
- working
- commit
- repository
- checkout
- Three tree
- working
- git add file.txt
- staging index
- git commit file.txt
- repository
The third tree allows a way to get changes ready for the commit and still allow modifications to the working directory until time to commit.
Git workflow
- New file creation is in the working directory.
touch file.txt
- git add places the file in the staging directory
git add file.txt
- git commit saves the changes to the repository
git commit …
- edited file is modified in the working directory
cat » file.txt
some changes ^d * add the changes to the staging index git add file.txt * commit changes git commit ...
Using has values (SHA-1)
- Git generates a checksum for each change set
- checksum algorithms convert data into a simple number
- sam data always equals same checksum
- data integrity is fundamental
- changing data would change checksum
- Git uses SHA-1 hash algorithm to create checksums
- 40-character hexadecimal string (0-9,a-f)
git log
Each commit has the parent hash as part of the commit to ensure data integrity between commits.
Working with the HEAD pointer
HEAD
- reference pointer to “tip” of current branch in repository
- last state of repository; what was last checked out
- points to parent of next commit
- where writing commits takes place
kind of like the record head of a tape player
cd .git ls -la cat HEAD cd refs ls -la cd heads cat master git log (to review what the SHA-1 refers to) git log HEAD (is the same as above)
5. Making Changes to Files
Adding Files
git status git add second_file.txt git status git commit -m "Add second file to project" git status git log git add third_file.txt git status git commit -m "Add third file to project" git status git log
Editing Files
Same procedure as adding files.
Make changes with text editor
git status git add first_file.txt git log
files can be in staging directory and not in staging and “status” will show the differences.
Viewing Changes with diff
view differences between repository version and the working directory
git diff
less started with -S will chop long lines. -R will show raw and wrap lines
Inside less, use - shift S to toggle back and forth between wrap and chop lines.
Viewing only staged changes
view differences between repository verison and the staging index version
git add first_file.txt git diff --staged
This used to be called –cached which was confusing. Both return the same information.
Deleting files
git add * git commit -m "files to delete"
Two ways to delete files. One it to delete file_to_delete1.txt from the OS which can put it in the trash, then:
git rm file_to_delete1.txt git commit -m "deleted file_to_delete1.txt" git status git log
The other way is to just do the whole remove from git, which will bypass the trash.
git rm file_to_delete2.txt git commit -m "Removed second file"
Moving and renaming files
Two ways: from the OS, then tell git, or just let git do it.
(rename a file) git status (then git shows a deleted file and a new untracked file.) git add primary_file.txt git rm first_file.txt git status (then shows the file to be renamed even with up to 50% changes.)
git mv second_file.txt secondary_file.txt git status will then show the file renamed
Moving is similar
git mv third_file.txt first_directory/third_file.txt
git commit -m "reorganized file structure by moving files"
6. Using Git with a Real Project
Editing the support Phone Number
TextMate allows files to be edited as a project by dragging the folder onto TextMate.
Make desired changes. Then use git status and git diff to see the changes
git status
git diff contact.html
Shows the lines with +/- designating changed lines.
- Uses the Less pager.
- “f” and “b” can be used to move forward and back pages of text.
- Start “less” with -r to fold lines (set in global config.)
- or, hit the minus key and Shift-S + return
To highlight changes specifically (not on a line by line level):
git diff --color-words contact.html
commit the changes
git add contacts.html
or
git add .
or
git commit -am "changed phone number."
Note: -a includes all changed files, but not new files or deleted files.
Editing the backpak file name and links
Changing file names within git is a good option.
git mv tours/tour_detail_backpack.html tours/tour_detail_backpack_cal.html
git status
will show that the file has been renamed.
Make changes to the html by using a global search and change the references.
git add tours.html git add /tours git status
Note: git reports just the rename for the file that was renamed.
A good practice is to make commits one change at a time. If after renaming files, a context change is made, then go ahead and commit the name change, then add the changed file(s) for another commit.
7. Undoing Changes
Undoing working directory changes
Show files modified.
git status
Show specific changes
git diff
Could be ambiguous:
git checkout index.html git checkout resources
This will retrieve the named thing (file, directory or branch), (index.html or resources) and make my directory look like that.
Better practice:
git checkout -- index.html (-- Means stay on the current branch.)
These checkout(s) will blow away the contents of the present directory.
Unstaging files
To get a hint:
git status
git reset HEAD <file>...
Amending Commits
Changing prior commits to git cannot really be made because of the hash integrity. The last commit is editable however.
With a change that needs to be commited
git add resources.html git commit -m "Rearrange the items to bring on a trip" git log
Make another change
git status git add resources.html git commit --amend -m "Rearrange the items to bring on a trip"
Or just the message can be updated which will change the hash of the last commit.
Remember, once another commit is made, that commit cannot be changed.
Retrieving old versions
git status git log
copy the first few letters of the hash, say 10 or so.
git checkout 2907d12603a34 -- resources.html git status (shows it is moved into the staging index git diff --staged git commit -m "This commit reverts 2907d12603a34" (adding the hash is a good idea)
Undo the checkout and commit:
git status git reset HEAD resources.html (puts it back in the working directory.) git checkout -- resources.html
Reverting a commit
Takes all the changes and go back to that version
git log git revert eb2f28564fd3cf
git -n (does not automatically do the commit.)
A merge is performed if the changes cannot be easily undone with a mirror image commit.
Using reset to undo many commits
git reset
Allows us to specify where the HEAD pointer points to.
git reset is analogous to rewinding a cassette and recording over it.
- -soft
- does not change the staging index or working directory
- -mixed (default)
- Changes staging index to match repository
- does not change working directory
- -hard
- changes staging index and working directory to match repository
- USE WITH CAUTION as it removes from the working directory any changes since the commit
Demonstrating a soft reset
git log
If we want to undo, we could “revert” or we can rewind and record over.
Open up a new text file and paste the output of the “git log” because once we rewind, we will not be able to see the log of changes since.
$ git log
commit 78687188cca4cbd9374da062952eaa2db3a0ba7f
Author: Steve Edwards <steve.edwards@vt.edu>
Date: Tue Dec 20 15:41:36 2016 -0500
moved items in list.
commit eb2f28564fd3cfe164be4329a78b3eac5c80ddd1
Author: Steve Edwards <steve.edwards@vt.edu>
Date: Tue Dec 20 14:54:00 2016 -0500
Initial-07_02
commit 2907d12603a340c02a4c846e3709991a7d0c21b9
Author: Kevin Skoglund <someone@nowhere.com>
Date: Tue Jun 19 16:28:37 2012 -0400
Removed contractions from contact page text
commit cf78db87140d6470393177c7dc634d706422acfa
Author: Kevin Skoglund <someone@nowhere.com>
Date: Tue Jun 19 16:28:21 2012 -0400
Renamed Backpack Cal file for clarity
commit 1506576375b15992e3d1f0a3d8965550efb89fa2
Author: Kevin Skoglund <someone@nowhere.com>
Date: Tue Jun 19 16:02:29 2012 -0400
Changed 24 hour support number to 4314
commit c4b913ef2da10a5d72c1ab515ada11ffb9c343e0
Author: Kevin Skoglund <someone@nowhere.com>
Date: Tue Jun 19 15:04:41 2012 -0400
Initial commit
$ cat .git/HEAD ref: refs/heads/master $ cat .git/refs/heads/master eb2f28564fd3cfe164be4329a78b3eac5c80ddd1
git reset --soft eb2f28564fd3
git status
Shows the reverted file.
git diff
show no changes
git diff --staged
shows changes
All it does is move the head pointer.
A re-commit could be made:
git commit -m "Doing the commit over again."
or just change the head pointer back.
git reset --soft 78687188cca4cb
Demonstrating a mixed reset
Same as soft except it makes the staging index look the same as the repository
Make a copy of the log so we have the commit hashes:
$ git log
commit 78687188cca4cbd9374da062952eaa2db3a0ba7f
Author: Steve Edwards <steve.edwards@vt.edu>
Date: Tue Dec 20 15:41:36 2016 -0500
moved items in list.
commit eb2f28564fd3cfe164be4329a78b3eac5c80ddd1
Author: Steve Edwards <steve.edwards@vt.edu>
Date: Tue Dec 20 14:54:00 2016 -0500
Initial-07_02
commit 2907d12603a340c02a4c846e3709991a7d0c21b9
Author: Kevin Skoglund <someone@nowhere.com>
Date: Tue Jun 19 16:28:37 2012 -0400
Removed contractions from contact page text
commit cf78db87140d6470393177c7dc634d706422acfa
Author: Kevin Skoglund <someone@nowhere.com>
Date: Tue Jun 19 16:28:21 2012 -0400
Renamed Backpack Cal file for clarity
commit 1506576375b15992e3d1f0a3d8965550efb89fa2
Author: Kevin Skoglund <someone@nowhere.com>
Date: Tue Jun 19 16:02:29 2012 -0400
Changed 24 hour support number to 4314
commit c4b913ef2da10a5d72c1ab515ada11ffb9c343e0
Author: Kevin Skoglund <someone@nowhere.com>
Date: Tue Jun 19 15:04:41 2012 -0400
Initial commit
git reset --mixed eb2f28564fd3c Unstaged changes after reset: M resources.html
So, `–mixed` will change the staging index to what is in the repository. Now, a
git diff
will show changes, and
git diff --staged
will not indicate changes.
We could restage and re-commit.
git add . git status
Demonstrating a hard reset
This will destuctively rewrite files in the current directory.
$ git log
commit 78687188cca4cbd9374da062952eaa2db3a0ba7f
Author: Steve Edwards <steve.edwards@vt.edu>
Date: Tue Dec 20 15:41:36 2016 -0500
moved items in list.
commit eb2f28564fd3cfe164be4329a78b3eac5c80ddd1
Author: Steve Edwards <steve.edwards@vt.edu>
Date: Tue Dec 20 14:54:00 2016 -0500
Initial-07_02
commit 2907d12603a340c02a4c846e3709991a7d0c21b9
Author: Kevin Skoglund <someone@nowhere.com>
Date: Tue Jun 19 16:28:37 2012 -0400
Removed contractions from contact page text
commit cf78db87140d6470393177c7dc634d706422acfa
Author: Kevin Skoglund <someone@nowhere.com>
Date: Tue Jun 19 16:28:21 2012 -0400
Renamed Backpack Cal file for clarity
commit 1506576375b15992e3d1f0a3d8965550efb89fa2
Author: Kevin Skoglund <someone@nowhere.com>
Date: Tue Jun 19 16:02:29 2012 -0400
Changed 24 hour support number to 4314
commit c4b913ef2da10a5d72c1ab515ada11ffb9c343e0
Author: Kevin Skoglund <someone@nowhere.com>
Date: Tue Jun 19 15:04:41 2012 -0400
Initial commit
git reset --hard eb2f28564fd3cfe164be git log git status
files have been changed and reverted.
We can move back to the latest commit if we know the hash, but at some point the commit would be garbage collected.
git reset --hard 78687188cca4cbd93
Or make another commit.
git reset --hard eb2f28564fd3cfe164be git log git status
are both clean and the files are rewound.
…make a change
git status git commit -am "moved sunglasses above hat." git log
The overwritten commits are abandoned and will eventually get cleaned up.
Remove untracked files
Destructivly deletes files from the pwd.
git clean
if some unneeded files, say text files, are in the directory, then git can purge them.
cat > junk1.txt cat > junk2.txt cat > junk3.txt
Test run:
git clean -n
git add junk1.txt
Force:
git clean -f
removes junk2 and 3
git status git reset HEAD junk1.txt
removes junk1.txt from staging index
git clean -f
removes junk1.txt
8. Ignoring Files
Using .gitignore files
Create a file in the root of the project directory
project/.gitignore
Can have very basic regular expressions
`* ? [aeiou] [0-9]
negate expressions with !
`*.php` !index.php
ignore all files in a directory with trailing slash
assets/videos/
comment lines begin with #, blank lines are skipped
cat >.gitignore
tempfile.txt .DS_Store *.zip *.gz log/*.log log.*.log.[0-9] assets/photoshop/ assets/videos # But don't ignore: !assets/videos/tour_*.mp4
Understanding what to ignore
- compiled source code
- packages and compressed files
- logs and databases
- operating system generated files
- user-uploaded assets (images, PDF's, videos)
Ignoring files globally
- ignore files in all repositories
- setting not tracked in repository
- user-specific instead of repository-specific
Create the .gitignore_global in base of home directory
cat > ~/.gitignore_global .DS_Store .Trashes .Spotlight-V100
git config --global core.excludesfile ~/.gitignore_global
Ignoring tracked files
Tracked files are not ignored even if the file is in .gitignore
- Add file to .gitignore
Then
git rm --cached tempfile2.txt
Tracking Empty Directories
Empty directories are not tracked.
mkdir assets git status
Add:
touch .gitignore
or
touch .gitkeep
git status git add assets/pdfs/ git status
reveals a tracked file
git commit -m "Add 'empty' directory with .gitkeep file in it"
9. navigating the Commit Tree
Referencing commits
tree-ish is something that references part of a tree.
- references a commit (sha-1 hash)
- sha-1 hash
- short sha-1 hash
- at least 4 characters for a small project
- unambiguous (8-10 characters)
- HEAD pointer
- always points to the commit of tip of the currently checked out branch
- branch reference, or tag reference
- ancestry
- parent commit
- `-HEAD^`, `acf87504^`, `master^`
- `-HEAD~1`, `HEAD~`
- grandparent commit
- `HEAD^^`, `acf87504^^`, `master^^`
- `-HEAD~1`
- great-grandparent commit
- `-HEAD^^^`, `acf87504^^^`, `master^^^`
- `HEAD~3`
Exploring tree listings
git ls-tree ... git help ls-tree git ls-tree HEAD
A tree is a directory
Getting more from the commit log
git help log git log --oneline git log --oneline -3 git log --since="2012-06-20" git log --after="2012-06-20" (same as previous) git log --until="2012-06-20" git log --before="2012-06-20" (same as previous) git log --since="2 weeks ago" --until="3 days ago" git log --since=2.weeks --until=3.days git log --author="Kevin Skoglund" git log --grep="temp" git log --online git log 2907d12..acf8750 git log 2907d12..acf8750 --oneline git log c4b913.. index.html git log -p c4b913.. index.html git log --stat c4b913.. index.html git log --summary c4b913.. index.html git log --stat --summary c4b913.. index.html git log --format=oneline (returns full sha1) git log --format=short git log --format=meduim (default) git log --format=full git log --format=fuller git log --format=email git log --format=raw git log --graph (shows branches) git log --oneline --graph --all --decorate
Viewing commits
git log --oneline git show cdb913e git show --format=online cdb913e git show --format=online HEAD git show --format=online HEAD^ git show --format=online HEAD^^ git show --format=online HEAD~3 git help show git ls-tree master git show 6fa98b (Tree-ish object "tree" instead of "blob", which is a directory) git show 497db6 (Tree-ish object "blob" instead of "tree", which is a file) git show index.html (is not the same thing as a tree-sh and will not work.)
Comparing commits
Show changes between working directory and Staging index
git diff
Show changes between staging index and the repository or the HEAD (where the HEAD pointer is pointing)
git diff --staged git diff --cached (same thing)
git log --oneline
Passing in one sha1 will show the difference between that commit and the pwd.
git diff cdae0ed git diff cdae0ed tours.html git diff cdae0ed..cdae0ed git diff cdae0ed..cdae0ed tours.html git diff cdae0ed..HEAD git diff cdae0ed..HEAD^^
git log --oneline git diff --stat --summary 1506576..HEAD git diff -b 1506576..HEAD git diff --ignore-space-change 1506576..HEAD (same as -b) git diff --ignore-all-space 1506576..HEAD (same a w) git diff -w 1506576..HEAD (ignore all space changes)
10. Branching
Branching overview
Branches of the master can be created and merged back into the master or discarded. It is a good way to test ideas in code that might not make it, rather than undoing a bunch of commits.
The HEAD pointer will point to the same commit until changes on the created branch are made and committed.
Viewing and creating branches
To show the branches before creating:
git branch
The * indicates the current branch
cat .git/HEAD ls -la .git/refs/heads cat .gits/refs/heads/master
To create the branch:
git branch new_feature ls -la .git/refs/heads cat .git/refs/heads/new_feature (shows hash of master since nothing has been committed yet.) cat .git/HEAD (still points to master)
Switching branches
pwd git branch cat .git/HEAD
git checkout new_feature git branch cat .git/HEAD
Make a change
git status git commit -am "Modified title on index.html" git log --oneline git checkout master git branch git log --oneline
Creating and switching branches
git branch
To create a new branch and checkout at the same time
git checkout -b shorten_title git status (note the checkout help option to discard changes) git add index.html git commit -m "Shorten title in index.html" git log --oneline git checkout new_feature git log --oneline git checkout master git log --oneline git log --graph --oneline --decorate --all
Switching branches with uncommitted changes
Working directory must be ” mostly clean“. Untracked files will not be deleted, as they will just show up as untracked files in any branch switched to.
git status
git checkout shorten_title
make a change to index.html
git status (shows unstaged change) git checkout master (will fail with an error.)
Two options. One is to discard changes.
git status git checkout -- <filename>
Second is to commit the changes
git commit -am "Swap out dash for colon in index.html title."
Third is to stash the changes. Described later.
Comparing branches
git branch git diff master..new_feature git diff --color-words new_feature..shorten_title git diff --color-words new_feature..shorten_title^
To check if a branch has been completely included in a branch.
git branch --merged
Renaming branches
pwd git branch git diff master..new_feature git branch -m new_feature seo_title git branch --move new_feature seo_title (same as previous)
Deleting branches
git branch git checkout master git branch branch_to_delete git branch -d branch_to_delete
git branch branch_to_delete git checkout branch_to_delete git branch -d branch_to_delete (will fail to delete)
make a change
git commit -am "Changed Title" git log --oneline git checkout master git branch git branch -d branch_to_delete (will fail to delete) git branch
Configuring the command prompt to show the branch
Make sure .git-completion.bash and git-prompt.sh are installed and .git-completion is loaded from .bash_profile
$ cat ~/.bash_profile if [ -f ~/.git-completion.bash ]; then source ~/.git-completion.bash fi if [ -f ~/git-prompt.sh ]; then echo $PS1 source ~/git-prompt.sh PS1='[\u@\h \W$(__git_ps1 " (%s)")]\$ ' # Default seems to be '\h:\W \u\$' fi
export PS1='>>>>>' export PS1='$(_git_ps1 "(%s)") > ' export PS1='\W$(_git_ps1 "(%s)") > '
11. Merging Branches
Merging code
Start with a clean working directory with everything committed.
Checkout the branch the is receiving the changes.
git checkout master git merge seo_title
Using fast-forward merge vs. true merge
A fast forward merge can occur when no changes have been made on the originating branch. It can simply move the commit to the timeline and point the HEAD at it.
To take a look:
git log seo_title --oneline -3 git log master --oneline -3
Force a commit while merging so a commit message can be logged.
git merge --no-ff branch
Merge only if ff merge can be performed.
git merge --ff-only branch
True merge
git checkout master git branch
make a change to contact.html
git status git add contact.html git commit -m "edit contact.html title" git log --oneline -3 git log shorten_title --oneline -3 git merge shorten_title (text editor will be involked to add a commit message.)
Merging Conflicts
If the same line is changed on the same file in different branches, then a conflict will arrise
git checkout -b 'text_edits'
Make changes to mission.html on lines 65, 66, 67 and 68.
git status git commit -am "Text edits on mission.html page" git checkout master
Make changes to mission.html on lines 67
git commit -am "Replace quotes on mission.html page" git log --oneline git log text_edits --oneline git merge text_edits
the conflicting files get lines inserted with the conflicts that will need to be fixed manually.
Resolving merge conflicts
If PS1 is set, the prompt will show (master|MERGING)
Three choices
- abort merge
- resolve the conflicts manually
- use a merge tool (not recommended until fully understanding merging manually)
Abort
git merge --abort
Resolve manually
Search for ”««“ in the conflicting file
git log --oneline -3 git show 432e8c6 (This will show differences.)
Find the proper changes and remove all the «« and ===== and extra parts.
git add mission.html git commit (a message is not necessary.)
git log --graph --oneline --all --decorate
git mergetool --tool= git mergetool
Exploring strategies to reduce merge conflicts
- keep lines short
- keep commits small and focused (don't make changes “while in the file”.)
- beware stray edits to whitespace
- spaces, tabs, line returns
- merge often
- track changes to master (merge master into branch as changes get made.)
12. Stashing Changes
Saving changes in the stash
git branch git checkout shorten_title git branch --merged
modify mission.html
git status git checkout master (will not work since changes to the shorten_title branch have been made!)
so stash them instead
git stash save "changed mission.html page title"
A stash is not a commit, it can be a little sloppier.
git log --oneline -3
runs get reset hard HEAD and made what is in the repo our pwd
Untracked files can be included with the git stash “include untracked” option.
Viewing stashed changes
list changes
git stash list
show files changed
git stash show stash@{0}
show files changed with diff
git stash show -p stash@{0}
Retrieving stashed changes
Stashed changes can be brought (merged) into whatever branch you are on like merging.
git stash pop (removes code from stash. Used most of the time.) git stash apply (leaves a copy in stash)
By default, it will pop the first stash
A specific stash can be popped.
git stash pop stash@{2}
Deleting stashed changes
git stash list
git stash drop stash@{0}
make a change tours.html
git status git stash save "changed mission and tours page title" git stash list git status
Make a change to explorers.html
Delete everything in stash all at once
git stash clear
13. Remotes
Using local and Remote repositories
Remotes are the central repository online. GitHub is one as such is BitBucket. Changes are sent to the server with a “push” command. This creates a branch called origin/master that matches the changes pushed to the repository on our local machine. This origin allows changes to be made by others while a local copy is also being modified. The changes have to be merged together to combine the changes. Origin/master just tries to stay in sync with the version on the remote.
push (send local commits to remote server and creates orgin/master and tries to stay in sync with server master.) fetch (brings remote changes into origin/master. A merge needs to be done to move changes to local master. merge (fast forward merge)
Do commits locally, fetch changes from server, then merge locally, and then push up to the server.
Setting up a GitHub account
Create an account. Free ones are public only.
Create a new repository (repo)
Initialize with README will add a README file. .gitignore is also available.
The setup page show commands to add the remote server origin branch
Adding a remote repository
git remote
Create a origin brach by convention for the main
git remote add origin https://... git remote
To list remotes verbosely and show the url
git remote -v
To remove a remote alias
git remote rm orgin (or whatever alias name used)
Creating a remote branch
Push (-u track branch change) (alias for remote repository) (branch)
git push -u origin master
cat .git/config
master references origin, and origin references a url.
ls -la .git/refs/remotes/ ls -la .git/refs/remotes/origin/ cat .git/refs/remotes/origin/remotes
stores branch hashes
git branch -r git branch -a
Cloning a remote repository
Find the link on github path to the repository and use it following:
git clone https://github.com/kevinskoglund/explore_california.git
To put it in a different named folder
git clone https://github.com/kevinskoglund/explore_california.git lynda_version git branch
Only the master branch by default (which can be changed) will be cloned unless -b specifies a different branch.
Tracking remote branches
The dash u option tracks changes on a remote server.
cat .git/config
...
[branch "master"]
remote = origin
merge = refs/heads/master
Create a branch that does not track don't use the `-u` option
git branch non_tracking git push origin non_tracking
Does not create a branch paragraph in the config file
To manually create:
git config branch.non_tracking.remote origin git config branch.non_tracking.merge refs/heasd/master
with git 1.7:
git branch --set-upstream non_tracking origin/non_tracking
Pushing Changes to a remote repository
Make some changes to tours.html
git commit -am "some changes made to tours.html" git log --oneline git log --oneline origin/master (my copy of remote branch) git diff origin/master..master
git push origin master
Since it is a tracking branch, just:
git push
git log --oneline
Fetching changes from a remote repository
pwd git log --oneline -5 git log --oneline -5 orgin/master git branch git branch -r (to show remote branches)
git does not automatically sync changes, it just looks at the local copy.
git fetch origin git fetch (if only one repository is specified)
- Always fetch before you work.
- Fetch before you push.
- Fetch often
Merging in fetched changes
pwd git branch -a git diff origin/master..master
git merge origin/master git log --oneline -3 master
git pull = git fetch + git merge
Checking out remote branches
pwd git branch git branch -r
A non-tracking branch can be retrieved and set to track:
git branch non_tracking origin/non_tracking git branch cat .git/config (shows tracked branch)
To delete the local copy of the branch:
git branch -d non_tracking git branch -r
create a branch and check it out:
git checkout -b non_tracking origin/non_tracking git branch (shows the active branch is non_tracking.) git checkout master (to go back to the master branch.)
Pushing to an updated remote branch
If commits have been made to the remote server since the local fetch, git will not push.
git fetch git merge origin/master git push
Deleting a remote branch
pwd git branch -a
old way:
git push origin :non_tracking git branch -r (it's gone!)
The colon is shorthand for from:to and if nothing is on the left side, it assumes nothing goes to the right and deletes it.
The shorthand can also be used to push from one to another
git push origin non_tracking:non_tracking
Newer, more intuitive way:
First push the local copy of the branch back up
git push origin non_tracking git branch -r git push origin --delete non_tracking
Enabling collaboration
From the github project homepage, select admin and add collaborators.
For an open source project, create a fork.
A collaboration workflow
git checkout master git fetch git merge origin/master
Create a branch to work on changes
git checkout -b feedback_form
Create a new file called feedback.html
git add feedback.html git commit -m "Add customer feedback form" git fetch git push -u origin feedback_form
Co-workers point of view:
git clone ??? (if not already cloned) git checkout master git merge origin/master
Now grab the remote version of feedback_form
git checkout -b feedback_form origin/feedback_form git log git show 84b6adf0
or
git show feedback_form (if it is the last commit made.)
make a change to the feedback_form
git commit -am "Adde tour selector to feedback form" git fetch git push
My p.o.v.
git fetch git log -p feedback_form..origin/feedback_form
if changes are acceptable, them merge them with the local copy
git merge origin/feedback_form
Go back to master and update it for other remote changes.
git checkout master git fetch git merge origin/master
Master is now up-to-date. Now merge in the feedback_form
git merge feedback_form git push
14. Tools and Next Steps
Setting up aliases for common commands
pwd
Set aliases in the global git config file so that they work for all projects
ls -la ~/
The file is .gitconfig
git config --global alias.(abbreviated command) (actual command) git config --global alias.st status cat ~/.gitconfig git config --global alias.co checkout git config --global alias.ci commit git config --global alias. git config --global alias.br branch git config --global alias.df diff git config --global alias.dfs "diff --staged" git config --global alias.dfc "diff --cached" git config --global alias.logg "log --graph --decorate --oneline --abbrev-commit --all"
Using SSH keys for remote login
https://help.github.com/articles/set-up-git
https://help.github.com/articles/caching-your-github-password-in-git/
https://help.github.com/articles/connecting-to-github-with-ssh/
Exploring integrated development environments
Textmate > Bundles > Git
- Vim
- Emacs
- TextMate
- Eclipse
- Netbeans
- Visual Studio
- IntelliJ
- RubyMine
- PHPStorm
Exploring graphical user interfaces
Graphical User Interfaces for Mac
- GitX http://gitx.org
- GitHub http://mac.github.com
- SourceTree http://sourcetreeapp.com
- Tower http://git-tower.com
- SmartGit http://syntevo.com/smartgit
- Gitbox http://gitboxapp.com
Graphical User Interfaces for Windows
- TortiseGit http://code.google.com/p/tortoisegit
- GitHub http://windows.github.com
- Git Extensions http://code.google.com/p/gitextensions
- SmartGit http://syntevo.com/smartgit
List of others: https://git.wiki.kernel.org/index.php/InterfacesFrontendsAndTools
Understand Git hosting
A git server needs to be set up.
- GitHub http://github.com
- Bitbucket http://bitbucker.org
- Gitorious http://gitorious.org
Git Self-Hosting
- Gitosis http://github.com/tv42/gitosis
- development stopped a few years ago
- Gitolite http://github.com/sitaramc/gitolite
- based on gitosis