Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Introduction to Git and Github

Introduction to Git and Github

Introduction to Git and Github - An introductory presentation to Git and Github along with basic concepts.

Senthilkumar Gopal

August 23, 2013
Tweet

More Decks by Senthilkumar Gopal

Other Decks in Programming

Transcript

  1. Git 101
    for everyone
    @sengopal

    View full-size slide

  2. Who is this for?
    ● Folks starting to use Git or foraying just now
    ● Folks who are using SmartGit :)
    ● Using Git as CVCS
    http://rogerdudler.github.io/git-guide/

    View full-size slide

  3. Clearcase Vs. Git
    Central Vs. Distributed

    View full-size slide

  4. Why command line
    No Installation
    Simple to use
    Comes bundled with Git as Git-Bash
    Consistent across OS

    View full-size slide

  5. A simple workflow
    Clone or create new repository
    Update/Add files
    Stage the changes
    Review the changes
    Commit the changes

    View full-size slide

  6. Git Setup
    $ git config --global user.name "John Doe"
    $ git config --global user.email [email protected]
    $ git config --global color.ui true

    View full-size slide

  7. Nomenclature

    View full-size slide

  8. Create a new Repository
    $ cd project/
    $ git init # initializes the repository
    $ git add . # add those 'unknown' files - ADDS FOR STAGE
    $ git commit # commit all changes, edit changelog entry
    - M
    $ git rm --cached ... # ridiculously complicated
    command to undo, in case you forgot .gitignore
    $ git reset HEAD # same as before
    $ git init project002 #shortcut for mkdir project002 &&
    cd project002 && git init

    View full-size slide

  9. Git Clone
    $ git clone git://github.com/sengopal/simplegit.git
    Initialized empty Git repository in
    /private/tmp/simplegit/.git/
    remote: Counting objects: 100, done.
    remote: Compressing objects: 100% (86/86), done.
    remote: Total 100 (delta 35), reused 0 (delta 0)
    Receiving objects: 100% (100/100), 9.51 KiB, done.
    Resolving deltas: 100% (35/35), done.
    $ cd simplegit/
    $ ls
    copy the entire history of that project so you have it locally

    View full-size slide

  10. Git status
    $ git status
    # On branch master
    #
    # Initial commit
    #
    # Changes to be committed:
    # (use "git rm --cached ..." to unstage)
    #
    # new file: README
    # new file: hello.py
    #
    # Changed but not updated:
    # (use "git add ..." to update what will be committed)
    # (use "git checkout -- ..." to discard changes in
    working directory)
    #
    # modified: README
    #

    View full-size slide

  11. Git add
    Start tracking new files and also to stage changes to already tracked files
    $ touch README.md; echo “test” > README.md
    $ git status
    $ git add .
    $ git status
    $ git diff
    Shortcut: git commit -a # the -a flag pulls in all modified files
    will commit all changed files (but not new files, those need to be added to the
    index with git-add). If you want to commit only certain files then you will need to
    stage them first with git-add

    View full-size slide

  12. Git diff
    To compare two revisions of a file, or your current file and a previous revision
    $ git diff README.md
    $ git diff --staged README.md
    $ git diff HEAD README.md
    $ git diff --stat README.md
    To compare 2 revisions of a file:
    $ git diff

    View full-size slide

  13. .gitignore
    $ git add .gitignore
    will use its rules when looking at files to commit to ignore from staging
    $ git rm --cached filename
    will not ignore a file that was already tracked before a rule was added to this file
    # to remove the tracked file - Caution: This deletes the file
    $ git config --global core.excludesfile
    ~/.gitignore_global
    file can be committed into the repository, thus sharing the rule list with any
    other users that clone the repository.

    View full-size slide

  14. Git Commit
    $ git commit -m 'my awesome changes'
    -m option not given - open a text editor for you to write your commit message.
    $ git commit -a
    automatically stage all tracked, modified files before the commit

    View full-size slide

  15. Git push
    remote branches
    are identical to local branches except that Git will not allow you to check them
    out. However, you can merge from them, diff them to other branches, run
    history logs on them, etc. You do all of that stuff locally after you synchronize.
    $ git push
    # push new commits to the on the repository
    For someone coming from CVS, the commit to the central
    repository now requires two steps.
    $ git clone
    # creates a remote called origin for push and fetch

    View full-size slide

  16. $ git pull
    # fetches code and merges it
    $ git fetch
    # fetches code without merging
    $ git pull --tag
    # pulls tags as well
    Git pull and fetch

    View full-size slide

  17. Git reset
    just a plain old git reset should unstage accidental git add
    $ git reset --soft
    undo the last commit and put the files back onto the stage
    $ git reset --hard
    undo the last commit, unstage files AND undo any changes in the working dir
    $ git-reset --hard
    Revert to a previous commit by hash
    $ git-reset --hard HEAD^
    your last commit before pull/merge

    View full-size slide

  18. Git reset
    $ git reset HEAD
    unstage file and copy from latest commit
    $ git reset --
    unstages specific files and copy files from the stage
    $ git checkout HEAD -- files
    copies files from the latest commit to both the stage and the working directory.
    $ git checkout -- files
    copies files from the stage to the working directory. Use this to throw away local
    changes.

    View full-size slide

  19. git branch
    The default branch in a git repository is called master.
    $ git branch
    To create a new branch use
    $ git branch
    To see a list of all branches in the current repository type
    $ git checkout
    If you want to switch to another branch you can use
    $ git checkout -b
    To create a new branch and switch to it in one step
    $ git branch -d # To delete a branch
    $ git stash branch # To create a branch with current
    changes

    View full-size slide

  20. git rebase
    $ git checkout experiment
    $ git rebase master
    First, rewinding head to replay your work on top of it...
    Applying: added staged command
    $ git rebase -i
    $ git rebase --interactive

    View full-size slide

  21. git merge
    If you want to merge a branch (e.g. master to release), make sure your current
    branch is the target branch you'd like to merge into (use git branch or git status to
    see your current branch).
    $ git merge experiment
    where experiment is the name of the branch you want to merge with the current
    branch
    $ git diff
    to see pending conflicts you have to resolve.
    $ git checkout -b linux-work # create a new branch
    $
    $ git commit -a
    $ git checkout master # go back to master branch
    $ git merge linux-work # merge changesets from linux-work

    View full-size slide

  22. git merge
    $ git checkout master
    $ git rebase topic
    First, rewinding head to replay your work on top of it...
    Fast-forwarded master to topic.
    This command lays the latest changes to topic right on top of the master
    branch, and preserves all of your commit history- laying them right on the end
    of the master branch’s commit history.
    $ git merge --squash topic
    This command will result in a commit log like a normal merge- meaning that all
    of the individual commit messages from the topic branch will become one
    single “merge” message.

    View full-size slide

  23. mergetool
    $ cat /usr/local/bin/extMerge
    #!/bin/sh
    /Applications/p4merge.app/Contents/MacOS/p4merge $*
    $ git config --global merge.tool extMerge
    $ git config --global mergetool.extMerge.cmd 'extMerge
    "$BASE" "$LOCAL" "$REMOTE" "$MERGED"'
    $ git config --global mergetool.trustExitCode = false
    ~/.gitconfig
    [merge]
    tool = extMerge
    [mergetool "extMerge"]
    cmd = extMerge "$BASE" "$LOCAL" "$REMOTE" "$MERGED"
    trustExitCode = false

    View full-size slide

  24. gitconfig
    $ git config --global core.editor emacs
    $ git config --global core.pager ''
    $ git config --global color.ui true
    $ git config --global diff.external extDiff
    $ git config --global core.whitespace \
    trailing-space,space-before-tab,indent-with-non-tab
    $ git config --global merge.stat true

    View full-size slide

  25. git remote
    $ git remote add origin user@server:/path/to/project.git
    adding a remote branch
    $ git remote -v
    origin [email protected]:github/git-reference.git (fetch)
    origin [email protected]:github/git-reference.git (push)
    list the remotes available
    $ git remote rm origin
    removing an existing remote alias

    View full-size slide

  26. Quick tips
    $ git log -- filename
    see the history of revisions to a file
    $ gitk
    inspect history visually, shows you how the revisions are connected
    $ git log
    this pipes a log of the current branch into your PAGER
    $ git log -p
    # same as above, but append a patch after each commit message
    $ git show HEAD
    show commit info, diffstat and patch of the tip of current branch

    View full-size slide

  27. Quick tips
    $ git filter-branch --tree-filter 'rm -f filename' HEAD
    remove all instances of a file from every commit
    $ git filter-branch --env-filter \
    "export [email protected]" HEAD
    change your email in all commits
    $ git blame
    history of user changes in a file
    $ git log --pretty=oneline --graph
    pretty log with a graph of changes done

    View full-size slide

  28. Future References
    http://gitimmersion.com
    http://git-scm.com/doc
    http://help.github.com

    View full-size slide