Git (/ɡɪt/) is a version control system primarily used for source code management for tracking changes in computer files and coordinating work on those files among multiple people. Git is comparable to CVSSubversion (SVN), and Mercurial.

Some benefits of using git over other existing version control systems e.g. SVN:

  • Easy Branching and Gracefully Merge for Distributed Development (feature oriented)
  • Work with versioning locally / offline (commit locally, local history)
  • Easy Context Switching of Branches and Commits by seconds (“Quick Time Machine”)
  • Commit to local before push to server (error prevention of committing code)

To work with git we can use git CLI using our favorit terminal / console like bash or CMD (for who ever who likes to do with commands or scripts), or we can use git visual UI software like GitKraken, Git Tower, or Sourcetree to help us managing versions using graph visualization and user friendly interface.

Daily Use Syntax for git CLI (Git Cheatsheet)

  • Checkout a branch
    • git checkout -q <grouper-folder>/<name-of-the-branch>
  • Get latest code from the same branch (get latest changes within branch)
    • git pull
  • Revert changes (undo file change)
    • Unstage first (if already in staging)
      • git reset -q HEAD path/to/filename.ext
    • To revert changes to last commit state
      • git checkout -q — path/to/filename.ext
    • To clean throw away / delete new file
      • git clean -f -q — path/to/filename.ext
  • Add new files to Git (stage file to Git)
    • git add -A — path/to/filename.ext
  • Add all files to Git (state all modified / added files to Git)
    • git add .
  • Commiting changes locally (commit only)
    • Add new files and modified files to Git (stage files)
      • git add -A — path/to/filename.ext
    • See files haven’t been pushed (track changes before commit)
      • git status
    • Commit stages files locally
      • git commit -m “Implement xyz
  • Commiting changes to server (commit and push)
    • Firstly committing changes locally using “git commit”
    • Pushing commit to server
      • git push
  • Basically compare local files with the server files (origin)
    • git diff –name-only <name of origin branch>
      • e.g. git diff –name-only origin/develop
    • or:
    • git diff –stat –cached <name of origin branch>
      • e.g. git diff –stat –cached origin/JIR-1234-vehicle-accessories
  • Create new branch from a checked out branch (new module / fix from develop branch or any other branch)
    • git checkout -q -b <grouper-folder>/<name-of-the-branch>
  • Publish new created branch to server (make a new branch available in server / origin)
    • git push -u origin <branch>
  • Get latest code from another branch (take update regularly from main branch (develop / master))
    • Make sure you are in the branch you want to update
      • git merge <another-branch>
  • Undo merge of hasn’t been pushed (undoing messed up merge, test merge then revert)
    • Cancel commit by merge
      • git reset HEAD~
    • Clean modified files
      • git checkout -q .
    • Clean untrack files
      • git clean -fd
  • Ignore files or folder to be included and tracked by Git (.gitignore)
    • put files/folder path in .gitignore

Advanced but Dangerous Operations:

  • Overwrite a branch by another branch (replace deprecated main branch with updated branch)
  • Clean synchronized deleted branches from remote which are still in local
    • git remote prune origin
  • Reset branch which already pushed to the server / origin / remote (e.g. to push to previous commit)
    • git push -f origin <sha1 of target commit>:<remote branch>
  • Delete branch
    • Locally
      • git tag –delete <tagname>
    • Remote
      • git push origin :<tagname>
      • or:
      • git push –delete origin <tagname>

Tools for Git

Git Repository

Looking for git repository? Here are some popular repositories to work with git: