Skip to content

Latest commit

 

History

History
368 lines (269 loc) · 8.23 KB

Session#6.md

File metadata and controls

368 lines (269 loc) · 8.23 KB

Session#6

Git Version Control

A Free Open Source DVCS ”distributed version control system ”

How it Works!

  • Snapshots, Not Differences

    • Compares SHA1 sum of modified files.
    • Saves the new file as a Whole "easier to retrive lost data"
    • Each Snapshot contains references to the files "to save space uses ref. only"
  • Stores new versions of files only

    N|Solid

  • Each snapshot contains each file

    N|Solid

  • Everything is local “distributed”

    • Each Computer has all the history of the project
    • Makes it faster and easeir to contribute
    • Safer beacuase the data are not onlt on one repo
  • Git has Integrity

    • Everything in Git is CHECKSUMMED "fast ,reliable"
    • Uses SHA-1 hash "a 40 hexadecimal char string"
    • Easy to recognize corrupted files.
  • Git Generally Only Adds Data "Everything is almost reversible"

Files’ Three States

  • Modified
  • Staged
  • Committed

Installtion

$ sudo apt update
$ sudo apt intsall git

First Time Configurations

git config --global user.name "John Doe"
git config --global user.email [email protected]
git config --global core.editor nano
git config --global merge.tool meld 

Git Help

git has a really good command manual for each command

$ git help <command>
$ man git <command>
$ git <command> -h #summarized

Important Terms

  • Repository
  • Index “Staging area”
  • working tree “Project tree”
  • Commit
  • Branch
  • Tag
  • Master
  • Head

Git Basics

Create a new repository

    $ git init # Intialize an empty/new repository
    $ git clone <URL># to Clone/Copy exsiting repository

<URL>: can be git, shh, http, https, file , etc

Doing changes in the repository

  • Remove files or directories

    git rm <file name| wild card>
  • Move or rename files or directories

    git mv <file directory | file name | wild card> <new directory | new file name>

Recording Changes to the Repository

N|Solid

  • Checking status of your files

    $ git status
    $ git status -s # Summarized
    • ?? “ untracked ”
    • A “ staged ”
    • M “ Modified staged “
    • M “ Modified untracked “

    Example:

    $ git status
    $ echo 'I' > README #creates a new file
    $ git status
  • Staging Modified Files

    Stage your files after adding not before

    $ git add <files|WildCards> 

    Example:

    $ git add README
    $ git status
    $ echo “USE” >> README
    $ git status
    $ git add README
    $ git status
  • Ignoring Files

    A file listing patterns to match them with files in your repositories.

    File name “.gitignore”

  • Viewing staged and unstaged

    $ git status
    $ git diff #diffreneces between commited  and modified/untracked
    $ git diff --staged #diffreneces between commited and modified/tracked/staged

    Example 1

    $ git commit -am “cleaning the stage”
    $ echo “Git” >> README
    $ echo  “Git” > Cont.md
    $ git add README Cont.md
    $ git diff # 
    $ echo “OSC” > Cont.md
    $ git status
    $ git diff

    Example 2

    $ git commit -am “cleaning the stage”
    $ echo “1” > file
    $ git add file && git commit -m “new file”
    $ echo “2” > file
    $ git add file
    $ echo “3” >file
    $ git diff
    $ git diff --staged
  • Committing Changes

    $ git commit
    $ git commit -m "short Message"
    $ git commit -am “Your Message” #Stage all untracked files and commit all files in staged area

    Example

    $ git commit -am “cleaning the stage”
    $ echo “1” > newfile
    $ git commit -am “new file”
    $ git status
    $ git add newfile && git commit -m “new file”
    # Change “newfile” and try using commit -am “changed new file”
  • Commit History

    $ git log
    $ git log -p
    $ Git log -<number>
    $ git log --since <date>
    $ git log --until <date>

    <data>or <number> EX : “ 2008-01-15” or EX : "2 years 1 day 3 minutes ago"

Undoing Things

Undo add (Unstaging a Staged File)

$ git reset <files>

Amend

$ git commit --amend

Used to change your latest log message and to make modifications to the most recent commit

Undo Commit

    $ git revert <commit> # merge a previous commit with current commit into a new commit
    $ git checkout <commit> # move the Head to the specified commit and change the working set to match the commit
    $ git checkout <file> # change the file to match the commited version of the file "the Head commit"
    $ git reset --soft <commit> # the Head and the branch pointer will move to the specified commit
    $ git reset <commit> # the Head and the branch pointer will move to the specified commit and will clear the index
    $ git reset --hard <commit> # the Head and the branch pointer will move to the specified commit and will clear the index and reset the working directory to match it
- git reset:
- --soft: uncommit changes, changes are left staged (index).
- --mixed: (default): uncommit + unstage changes, changes are left in working tree.
- --Hard: uncommit + unstage + delete changes, nothing left.
- <commit>:
    - SHA1
    - Head^,Head@{number}

Remote Repositories

  • Clone a Repository

$ git clone <URL>
- <URL>:
 - https://github.com/
     - git://github.com/koke/grit.git
     - [email protected]:mojombo/grit.git
     - /srv/git/project.git
     - file:///srv/git/project.git
  • Showing Your Remotes

        $ git remote
        $ git remote -v
  • Add Remotes

        $ git remote add <Name> <URL>
  • Fetching and Pulling Repositories

        $ git fetch <remote>
        $ git pull <remote> <branch>
  • Pushing into Repositories

        $ git push <remote> <branch>

Branching

  • List branches

     $ git branch
     $ git branch -a
  • Create new branch

    • git branch testing

    N|Solid

    • switching Branch

            - “git checkout testing”
      

    N|Solid

    • Commiting in a New Branch

    N|Solid

    • Return to master Branch

    N|Solid

    • Commiting again Branch

    N|Solid

  • Switch branches

     $ git Checkout <branch name>
  • Delete Branches

     $ git branch -d <branches>
  • Merging Branching

    • Fast-forward
    • Recursive “Three Way merge”
    • git mergetool
    • meld
  • Combine Commits and Modify History of a Branch

    • git rebase

Remote Branches

N|Solid N|Solid N|Solid

Local Git Repositories

- git daemon:
 - base-path=<path> 
 - export-all 
 - reuseaddr 
 - informative-errors 
 - verbose
 - enable=receive-pack
 - You can use aliases

git aliases

- git  config --global alias.<name> ‘commands’
$ git config --global alias.serve '!git daemon --base-path=. --export-all --areuseaddr --informative-errors --verbose' 
$ git serve

Resources