Techcookies

Git cheat sheet

Git | Fri Nov 29 2019 | 3 min read

What is git and how changes tracked

  • Distributed change management system
  • Every user works in their own repository rather than working from one central repository
  • Changes are tracked as "change sets" or patches not as versions
  • SVN tracks versions
  • Later change sets can be exchanged/pushed between repositories.
  • No single master repository; just many working copies and each repositories with their own combination of change sets
  • Advantages:
    • faster as no need to communicate with central server
    • no network access required
    • no single failure point
    • change sets can be submitted for inclusion or exclusion from repository later after work and each developer can work independently
  • not great for tracking non-text files
    https://git-scm.com/docs/git-update-index#_using_assume_unchanged_bit

Install config

- Install
- git help <command>

Create

    Init
    Clone

Local changes

Commit changes

    - make changes
    - add changes
    - commit changes
    - push
    - commands
    ```
        git pull
        git add
        git status
        git commit
        git Push
    ```

view commit log

    - git log
      - -n <number> last number of commit logs
      - -p <filename> changes for specific file overtime
      - -p or --patch, which shows the difference (the patch output) introduced in each commit.
      - -stat
      - --pretty=oneline
      - git log --pretty=format:"%h - %an, %ar : %s"
      - --graph
    - git blame -ew -L 1,10 <code filename>

diff: unstaged changes between your index and working directory

    - git diff <filename>
    - git diff <filename1> <filename2>
    - git diff HEAD: Show difference between working directory and last commit

update/delete

    - Rm
    - Reset
    - Stash
    - Stash pop
    - Stash drop

Config

    - Config
      - git config <--global|--system|--local> <user.name|user.email|core.editor>
      - git config --global --edit
    - Gitignore

Git branching

    - Git branch -av: list all local and remote branches, v here is for verbose
    - git checkout <branch name>: switch branch
    - git checkout -b <branch name>
    - git branch <new branch name>: create a new branch
    - git branch -d <branch name>
    - git tag <tag name>
    - git push --tags
    - git remote -v
    - git pull <origin><branch>: get changes and merge into HEAD
    - git push <origin><branch>: publish local changes on a remote
    - git branch -dr <remote/branch>

Undo

  • git reset: reset staging area
  • git reset --hard: reset staging area and working directory
  • git reset <commit id>
  • git reset <--hard|--soft> HEAD~1
    • --hard: won't keep local changes
    • --soft: will keep local changes

Merge & Rebase

  • git merge <branch name>: merge <branch> into current branch
  • git rebase <branch name>: Rebase your current HEAD onto <branch>
  • git rebase --continue: continue rebase after conflict resolution