Skip to content

Instantly share code, notes, and snippets.

@zeroasterisk
Created December 12, 2012 05:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zeroasterisk/4265044 to your computer and use it in GitHub Desktop.
Save zeroasterisk/4265044 to your computer and use it in GitHub Desktop.
KYOSS Git Copy/Paste (intermediate)

You will be typing a fair amount into your shell to go with our slides... Here's a convenient place to copy/paste from.

Git Intermediate Presentation / Slides:

Git Introduction Presentation / Slides: (in case you need a refresher)

Branching

git checkout -b my-crazy-branch-1
echo 'yo' > howdy.txt
echo 'peace' >> goodbye.txt
git status
git diff
add howdy.txt
git commit -a -m "a few misc changes for my-crazy-branch-1"
ls
git checkout master
ls
git log --oneline

Merge

git checkout master
    # checkout where ever you want to merge on to
    # in other words, the destination branch
git merge my-crazy-branch-1
git merge my-crazy-branch-1 --squash
    # use --squash to turn several commits in source branch
    # in to one commit on destination branch
git merge my-crazy-branch-1 --no-ff
    # use --no-ff to force a commit for the merge, 
    # even if not needed

Conflicts

git checkout master
echo 'line-from-master' >> howdy.txt
git commit -a -m "added line from master"
git checkout my-crazy-branch-1
echo 'line-from-crazy' >> howdy.txt
git commit -a -m "added line from crazy"
git checkout master
git merge my-crazy-branch-1

Auto-merging howdy.txt
CONFLICT (content): Merge conflict in howdy.txt
Automatic merge failed; fix conflicts and then commit the result.

vim howdy.txt
    # edit file to suit your needs, save
    
git add howdy.txt
    # tells git we have fixed whatever was broken, add to merge

git commit
    # default message tells us about the merge and conflicts

Abort a merge if you must

git merge --abort
    # resets status, doesn't change files
git reset --hard HEAD
    # resets all files to last commit

Conflicts with a mergetool

On a GUI environment, you can use a visual mergetool to assist in resolving conflicts

http://adventuresincoding.com/2010/04/how-to-setup-git-to-use-diffmerge/

git mergetool
Merging:
howdy.txt

Normal merge conflict for 'howdy.txt':
  {local}: modified file
  {remote}: modified file
Hit return to start merge resolution tool (diffmerge): 

After you save the file and close DiffMerge, it adds the file to git for you... just have to commit!

Conflicts because of git pull

git pull origin master
git status

git pull conflicts

Conflicts git pull --rebase to the rescue

git pull --rebase origin master
git status

git rebase

Bonus Points

Branch Diffs

git diff <branch-a>..<branch-b>
git diff my-crazy-branch-1..master

useful for comparing two branches before merging.

Branch Subtraction

git log \^<branch-sub> <branch-main>
git log \^my-crazy-branch-1 master

views the log for the main/master branch, but removes all commits which exist in the subtracted branch.

useful for isolating commits within branches, research.

Branch Deletion

git branch -d <branchname>
git checkout -b tempbranch1
git branch -d tempbranch1
git branch -D tempbranch1
    # will delete even if not merged out (potentially destructive)
git push origin :tempbranch1
    # will delete branch from origin
    # strange syntax, I know...

git stash

echo 'example of stashing' >> howdy.txt
git status
git stash
git status
git stash list
git stash pop
git help stash

git stash

Branching Model

http://nvie.com/posts/a-successful-git-branching-model/

I can not recommend this highly enough...

git branching model

$ git checkout -b myfeature develop
Switched to a new branch "myfeature"

$ git checkout develop
Switched to branch 'develop'
$ git merge --no-ff myfeature
Updating ea1b82a..05e9557
(Summary of changes)
$ git branch -d myfeature
Deleted branch myfeature (was 05e9557).

$ git checkout -b release-1.2 develop
Switched to a new branch "release-1.2"
$ ./bump-version.sh 1.2
Files modified successfully, version bumped to 1.2.
$ git commit -a -m "Bumped version number to 1.2"
[release-1.2 74d9424] Bumped version number to 1.2
1 files changed, 1 insertions(+), 1 deletions(-)

$ git checkout master
Switched to branch 'master'
$ git merge --no-ff release-1.2
Merge made by recursive.
(Summary of changes)
$ git tag -a 1.2

$ git checkout develop
Switched to branch 'develop'
$ git merge --no-ff release-1.2
Merge made by recursive.
(Summary of changes)

$ git branch -d release-1.2
Deleted branch release-1.2 (was ff452fe).
$ git push origin develop

$ git checkout -b hotfix-1.2.1 master
Switched to a new branch "hotfix-1.2.1"
$ ./bump-version.sh 1.2.1
Files modified successfully, version bumped to 1.2.1.
$ git commit -a -m "Bumped version number to 1.2.1"
[hotfix-1.2.1 41e61bb] Bumped version number to 1.2.1
1 files changed, 1 insertions(+), 1 deletions(-)

$ git commit -m "Fixed severe production problem"
[hotfix-1.2.1 abbe5d6] Fixed severe production problem
5 files changed, 32 insertions(+), 17 deletions(-)

$ git checkout master
Switched to branch 'master'
$ git merge --no-ff hotfix-1.2.1
Merge made by recursive.
(Summary of changes)
$ git tag -a 1.2.1

$ git checkout develop
Switched to branch 'develop'
$ git merge --no-ff hotfix-1.2.1
Merge made by recursive.
(Summary of changes)

$ git branch -d hotfix-1.2.1
Deleted branch hotfix-1.2.1 (was abbe5d6).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment