On Github jdduarte / git-training
Muggles still use this everyday
Good
Standard solution for many years and still used by many
Good
git config --global user.name "John Doe" git config --global user.email johndoe@example.com
git config --global core.editor vim
gif config --global merge.tool vimdiff
git config --list # show all config values, last ones have priority git config user.name # show config value
git help <verb> git <verb> --help man git-<verb>
Example:
git help config
git init
git clone <repo>
$ git status # On branch master nothing to commit (working directory clean)
$ touch README $ git status # On branch master # Untracked files: # (use "git add <file>..." to include in what will be committed) # # README # nothing added to commit but untracked files present (use "git add" to # track)
$ git add README
$ git status # On branch master # Changes to be committed: # (use "git reset HEAD <file>..." to unstage) # # new file: README #
$ vim index.html (& make some changes)
$ git status # On branch master # Changes to be committed: # (use "git reset HEAD <file>..." to unstage) # # new file: README # # Changes not staged for commit: # (use "git add <file>..." to update what will be committed) # # modified: index.html #
$ git add index.html
$ git status # On branch master # Changes to be committed: # (use "git reset HEAD <file>..." to unstage) # # new file: README # modified: index.html #
$ git diff // changes still unstaged $ git diff --cached // changes staged to commit
$ git commit -m "Learning git is fun!"
$ git commit -a -m "fix stuff"
$ git commit --ammend
$ git rm file.txt // removes from repo & working directory $ git rm --cached file.txt // removes from repo
$ git mv file_from file_to
$ mv README.txt README $ git rm README.txt $ git add README
$ EDITOR .gitignore
IDE files, local settings, etc...
$ git log
$ git reset <filename>
$ git checkout <filename>
$ git remote -v
$ git remote add <name> <url>
$ git fetch <remote>
$ git pull <remote> <branch>
$ git push <origin> <branch>
A commit consists of
$ git branch <branch_name>
$ git branch -d <branch_name>
$ git checkout <branch_name>
$ git checkout -b <branch_name>
$ git branch
You should branch everytime you do something new.
Branch for:
$ git checkout -b iss53 Switched to a new branch "iss53"
$ vim index.html $ git commit -a -m 'added a new footer [issue 53]'
$ git checkout master Switched to branch "master"
$ git checkout -b hotfix Switched to a new branch "hotfix" $ vim index.html $ git commit -a -m 'fixed the broken email address' [hotfix]: created 3a0874c: "fixed the broken email address" 1 files changed, 0 insertions(+), 1 deletions(-)
$ git checkout master $ git merge hotfix Updating f42c576..3a0874c Fast forward index.html | 1 - 1 files changed, 0 insertions(+), 1 deletions(-)
$ git checkout iss53 Switched to branch "iss53" $ vim index.html $ git commit -a -m 'finished the new footer [issue 53]' [iss53]: created ad82d7a: "finished the new footer [issue 53]" 1 files changed, 1 insertions(+), 0 deletions(-)
$ git checkout master $ git merge iss53 Merge made by recursive. index.html | 1 + 1 files changed, 1 insertions(+), 0 deletions(-)
$ git merge iss53 Auto-merging index.html CONFLICT (content): Merge conflict in index.html Automatic merge failed; fix conflicts and then commit the result.
$ git status index.html: needs merge # On branch master # 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) # # unmerged: index.html #
<<<<<<< HEAD:index.html <div id="container">Awesome!</div> ======= <div id="container"> Wunderbar! </div> >>>>>>> my-other-branch:index.html
Remember: HEAD is was what you had checked out when you ran your merge command
$ git add <filename>
$ git mergetool
$ git commit
Reapplying a diverging branch onto another
git checkout <diverging_branch> git rebase master
After rebasing, a merge of master with diverging_branch will fast-forward master
$ git checkout experiment $ git rebase master First, rewinding head to replay your work on top of it... Applying: added staged command
$ git checkout master $ git merge experiment
Rebasing replays changes from one line of work onto another in the order they were introduced, whereas merging takes the endpoints and merges them together.
The only difference between merging and rebasing is the resulting history
$ git commit -m "Fancy message" // Many of these $ git pull origin develop // This merges. Simpler $ git push origin develop // Push commits