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 CVS, Subversion (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
- Unstage first (if already in staging)
- 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“
- Add new files and modified files to Git (stage files)
- 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
- git diff –name-only <name of origin branch>
- 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>
- Make sure you are in the branch you want to update
- 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
- Cancel commit by merge
- 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)
- http://stackoverflow.com/questions/2862590/how-to-replace-master-branch-in-git-entirely-from-another-branch
- git checkout <branch-replacer>
- git merge -s ours <branch-to-be-replaced>
- git checkout <branch-to-be-replaced>
- git merge <branch-replacer>
- http://stackoverflow.com/questions/2862590/how-to-replace-master-branch-in-git-entirely-from-another-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>
- Locally
Tools for Git
- Git core: https://git-scm.com
- UI Tracker for Git to view version as tree:
- Benefit:
- Good to have overview of version and branches
- Good to learn and understanding how Git works
- Tools:
- Sourcetree: https://www.sourcetreeapp.com
- GitKraken: https://www.gitkraken.com/
- Git Tower: https://www.git-tower.com/
- Benefit:
- IDE with Git Integration to learn Git
- Webstorm: https://www.jetbrains.com/webstorm/
- Many simplification syntax we can do out of the box
- Tree view supported
- Seeing changes and merge file easily
- Visual Studio Code: https://code.visualstudio.com
- Very simple and we can learn a lot about what happen at lower-layer
- Webstorm: https://www.jetbrains.com/webstorm/
Git Repository
Looking for git repository? Here are some popular repositories to work with git:
- GitHub: https://github.com/
- GitLab: https://about.gitlab.com/
- Bitbucket: https://bitbucket.org/