Saturday, 17 August 2019

GIT

Download
  • git-scm.com
GIT
  • Version control management tool
  • It records history all changes to a file. It is called Commit History


  • Distributed source control system
  • Massively scales
  • Open Source
  •  eg: Linux kernel project contains 15M of lines of code, with 1200 worldwide contributors with multiple branches
  • Most operations are local
  • Active community
GIT Terminology
  • Repository
    • files, history & config managed by GIT
  • 3 states
    • working dir, staging area, commit - GIT Repository(.git foler)
  • Remote Repository GITHUB
  • main/Master Branch
    • default branch provided by GIT
Installation
  • git version   --> system will show a dialog to install Xcode. Install the same
Commands to Start
  • git config --global user.name "vdesu"
  • git config --global user.email "venkat.desu@gmail.com"
  • git config --global --list
  • git config --global -e          //in editor
  • git help
  • git help add
  • git config --global diff.tool p4merge
  • git config --global difftool.p4merge.path  /Applications/p4merge.app/Contents/MacOS/p4merge
  • git config --global difftool.prompt false
  • git config --global merge.tool p4merge
  • git config --global mergetool.p4merge.path  /Applications/p4merge.app/Contents/MacOS/p4merge
  • git config --global mergetool.prompt false
 
git clone https://github.com/vdesu/PBNominations.git
git status

1. Create OS file to add to working directory
2. git add <filename> to move file to staging area
3. git commit -m "message"  to commit to local repository
4. git push origin main
git push --all origin 
  
Reinstall git in MAC
sudo xcode-select --install
Starting a Project(3 ways)
  • No Source yet -> Fresh 
  • Existing source locally
  • GitHub Project (Fork & Clone) 
Create new repository locally -> No Source yet
  • git init fresh-project
  • cd fresh-project
  • ls -all  or ls -la
  • git status
  • Save a new file to the directory
  • git status   -> one untracked file will be listed
  • git add <file name> -- to get the file tracked by git by moving the file to staging area
  • git commit -m "first git commit"
  • git logs
  • git diff  -> working area and staging differences
  • git diff --staged  -> local Repository and staging differences
  • git rm <filename> -> to delete a file
  • git pull
  • rm -rf <project> -> delete project
Create new repository locally -> Existing Source Locally
  • git init
  • cd fresh-project
  • ls -all  or ls -la
  • git status
  • Save a new file to the directory
  • git status   -> one untracked file will be listed
  • git add <file name> -- to get the file tracked by git by moving the file to staging area
  • git commit -m "first git commit"
  • git logs
  • git diff  -> working area and staging differences
  • git diff --staged  -> local Repository and staging differences
  • git rm <filename> -> to delete a file
  • git pull
  • rm -rf <project> -> delete project
Create new repository on Github
  • Create repository MISC on github
  • Create folder with MISC on your system
  • git init
  • git remote add origin "https://github.com/vdesu/misc.git"
  • git pull origin main
  • make changes
  • git add .
  • git commit
  • git push origin master
  • git push -f origin  main
Fork
  • Copy others repository into your account
Clone
  • git clone  https://github.com/vdesu/starter-web.git

Branches
  • git branch -a  -> list branches
  • git branch -> to check number of branches in side the project
  • git branch <mybranch> -> create branch
  • git checkout mybranch  -> to switch branch
  • git branch -d <branchname> -> to delete branch
  • merge the branches
    • switch to target branch
    • git merge <develop>

Revert commit

  • git reset HEAD <file>  -> move from stage to working
  • git reset HEAD~    -> undo commit
Discard changes in Working Directory
  • git checkout -- <filename>
  • File Operations
    • Rename or Move
      • git mv <file1> <file2>
      • rename at OS level
        • git add -A
    • Delete file
      • git rm <file>
History
  • git log
  • git log --oneline --graph --decorate
  • git log <commit id 1>... <commit id n>
  • git log --since="3 days ago"
  • git show <commit id>
Alias
  • git log --all --oneline --graph --decorate
  • git config --global alias.hist "log --all --oneline --graph --decorate"
  • git hist. // alias for the full command
Ignoring unwanted files and folders
  • mate .gitignore
    • provide patterns like
      • file.txt
      • *.exe
      • folder/
Merge Diff tool
  • git difftool  //working dir to stage area
  • git difftool HEAD. //working dir to git repository or last commit
  • git difftool --staged HEAD
  • git difftool HEAD HEAD^
  • git difftool <commit id 1> <commit id 2>
  • git difftool master origin/master
Branching & Merging
  • git branch -a
  • git branch <banch name>
  • git checkout <branch name>
  • git checkout -b <banch name>.        --to create and checkout branch
  • git branch -m <old branc> <new branch>   -> rename branch
  • git branch -d <branch name>
Making changes on Branch and merging
  • Checkout <branch>
  • Make changes & commit on branch
  • Switch to master
  • git diff master <branch>.   --review differences
  • git merge <branch to be merged>
  • git branch -d <branch name>   //delete branch

Existing Project to GIT
  • create repository in GitHub
  • navigate to project directory
  • git init
  • git add .
  • git commit -m "first commit"
  • git remote add origin https://github.com/vdesu/dashboard.git
  • git remote -v
  • git push -u origin master

Fork

  • Get the someone else project into our local github

Clone

  • Get repository from Github to local 
  • git clone <url>

 File History

  • git log
  • git log --oneline --graph --decorate

Rebase

  • git rebase master 
  • This is to replay or redo the changes on top of the current master branch
    • rewind the changes on child branch
    • copy master changes to child branch
    • replay the changes on child branch
  • git rebase --abort
  • git pull --rebase origin master
Stashing
  • Save your uncommit changes so that you can work on something else and revert them from working directory
  • git stash save
  • git stash apply. -> get the saved files
  • git stash list
  • git stash drop
  • git stash -u.   -> to stash untracked files
  • git stash pop -> apply and drop stash
  •  
Tagging
  • Tags are refs that points to specific points in Git history
  • It is used to capture a point in history that is used for marked version release
  • Commands
    • git tag <name>
    • git tag --list
    • git tag --delete <tagname>
  • Annotated tags -> major milestones
    • git tag -a <name>
  • Compare tags in git
    • git diff <tag1> <tag2>
  • tag specific commit
    • git tag -a <commit id>
  • git push origin <tag name>
  • git push origin master --tags
  • git push origin :<tagname>.    -> delete tag name in github

rm -rf <project> to remove the  project

Fork(from UI) - Copies the repository into our personal space


Connect to GITHUB:
  • git config --global user.name "vdesu" 
  • git config --global user.email "venkat.desu@gmail.com"




No comments:

Post a Comment