Git - AshokBhat/notes GitHub Wiki

Git

  • Notes
    • Terms
      • Remote
        • Remote repository which local repo is linked to.
      • Local repo
        • Repository that is stored locally
      • Staging
        • Intermediate place where files/changes are held before committing to local repo.
      • Working
        • Actual files on which changes are being made.
      • Staged changes
        • Changes that have been added to staging but not committed yet.
      • Unstaged changes
        • Changes that have been made to working but not added to staging area.
      • HEAD
        • Like a pointer to current branch
      • Stash
        • Temporary location where un staged changes can be kept without staging.
        • Idea is to quickly save some changes that you're not ready to commit or save, but want to come back to while you work on something else.
    • Merging process
      • When one branch is merged onto another, git merges all the commit snapshots of both branches into one tree.
      • The individual snapshots are merged on the basis of time when commits were made.
      • In addition, a final 'merged' commit snapshot will be made which contains all the merged files.
      • If merge cannot do be done automatically, git notifies conflict exists.
      • User has to resolve the conflict and add the commit the changed files.
      • At this stage, entire merge history will be created.
  • Creating Projects
    • Create git repo in current directory
      • git init
  • Getting projects
    • Clone a remote repo
      • git clone
  • Basic operations
    • Adding file to staging
      • Add a file
        • git add
      • Add all modified and removed tracked files
        • git add -u
    • Commit staging to repo
      • Provide commit message using editor
        • git commit
        • This will open up and editor where you can provide commit message.
      • Commit message with command
        • git commit -m
    • Remove file
      • From staging
        • git reset HEAD
          • Removes file from the staging area
          • File is not physically removed
          • git status will not show that file is in staging area ready to be committed.
      • From repo and local storage
        • git rm
          • Removes file from local storage
          • File will be shown as to be deleted in staging area.
    • Show difference
      • Show unstaged differences (between unstaged and repo)
        • git diff
      • Show staged differences (between staging and repo)
        • git diff --cached
      • Show all differences (unstaged and staged)
        • git diff HEAD
  • Advanced operations
    • Amend last commit
      • git commit --amend
        • Change last commit with new commit message and/or files.
    • Remove last commit
      • git reset --hard HEAD^
    • Change previous commits
      • git commit --fixup <commit_id_to_amend>
      • git rebase -i --autosquash
    • Find who modified each line of file.
      • git blame
        • Show what revision and author last modified each line of a file
  • Branches
    • List branch
      • Local
        • git branch
      • Remote tracking
        • git branch -r
      • All
        • git branch -a
      • Verbose
        • See the last commit on each branch
        • -v option
        • Some Variants
          • git commit -va
            • All branches + Verbose
          • git commit -vr
            • All remote tracking branches + Verbose
          • git commit -v
            • All local branches + verbose
    • Create branch
      • Copy of current branch
        • git branch
      • Copy of named branch
        • git branch
    • Switch to another branch
      • Existing branch
        • git checkout
      • New branch, which is copy of current branch
        • git checkout -b
      • New branch, which is copy of another branch
        • git checkout -b
    • Delete branch
      • Local branch
        • git branch -d
      • Remote branch
        • git push (remote-name) :(branchname)
    • Diff
      • Two branches
        • git diff
    • Merge
      • Merge another branch into current branch
        • No conflict
          • git merge
        • Conflict
          • git merge
          • Resolve conflict
          • git add
          • git commit
  • Working with remote repos
    • Update local repo with remote, but not merge
      • git fetch
    • Update local repo and merge
      • git pull
    • Push to remote
      • git push
      • e.g
        • git push origin master
  • Stash current work and pop it back
    • Stash - add current changes to the stack
      • git stash
    • View stashes currently on the stack
      • git stash list
    • Apply stash on top of the stack
      • git stash pop
    • Apply stash from items on stack
      • git stash apply
        • example git stash apply stash@{0}
    • Drop a stash
      • git stash drop
        • example git stash drop stash@{0}
    • Drop all stash
      • git stash clear
  • Patches
    • Create patch from last commit
      • git format-patch -n HEAD^
    • Create series of patches
      • git format-patch
        • Create a patch file for each commit that happened since the specified commit <last_commit_id>
⚠️ **GitHub.com Fallback** ⚠️