GIT Version Control
YouTube Video
Commands
git init
: Intialize the repositorygit status
: Checks the current directory changes and staging areagit add <directory>
: Add current files to staginggit commit -m "<commit-message>"
: Commit staging with a messagegit commit -am "<commit-message>"
: Add & Commit staging with a message (only for edited and deleted)git log --oneline
: Prints the log as one linegit reflog
: Shows the log of changing the HEAD. It's useful to find missing commits.git branch <branch-name>
: Create a new branchgit checkout <branch-name>
: Checkout to a branchgit checkout -b <branch-name>
: Create and checkout to a branchgit diff <commit-hash-source> <commit-hash-destination>
: Shows the difference between two commitsgit merge <branch-name>
: Merge a branch into the current branchgit merge --squash <branch-name>
: Merge a branch into the current branch as single commitgit branch -D <branch-name>
: Delete a branchgit rebase <branch-name>
: Rebase the current branch on another branchgit rebase <branch-name> -i
: Rebase the current branch on another branch (with interactive mode)git cherry-pick <commit>
: Apply changes for a certain commit to the current branchgit reset --hard <commit-hash>
: Move the branch Head to a certain commitgit mv <source> <destination>
: Move a file/directory from one place to another while trackinggit remote add origin <repository-name>
: Add a remote tracking branchgit push --set-upstream origin <branch-name> <repository-name>
: Set a remote for a bracnhgit push -u origin <branch-name> <repository-name>
: Set a remote for a brach (Same to the above one)git push
: Push local commitsgit fetch
: Fetch commits from origingit pull
: Fetch commits and mergegit fetch --prune
: Removes and branches that no longer exists on remote before fetchinggit remote prune origin
: Remove branches from local repository that no longer in remote
Collaboration
Pull Request
We need merge to master locally but rather we push the branch and create a Pull Request/Merge Request.
Other developers will review your changes and add comments.
When all is resolved and approved, you can merge your changes.
Merge Conflicts
Merge conflicts happen when git can't merge two branches. It's always 99% to happen if a file is changed on the same line in two different branches, that's when git can't decide how to merge.
To resolve it, it has to be done manually and you will need to check the git log to find the developer responsible for the conflict and align with them.
Branching Strategies
Single Branch
Create a single main branch and push all your commits there.
Good for personal projects.
Feature Branching
We have a single branch and each time there is a feature/bug, we create a new branch, do the changes and then merge back to main branch.
GIT Flow
We have two branches develop and main.
When creating a feature/bugfix, we create a feature branch from develop. Changes are merged back to develop.
When creating a hotfix, we create a feature branch from main. Changes are tagged with a version and only merged back to develop (if needed).
We can only merge develop into master and we need to tag the commit with a release version.