Git Version Control
YouTube Video
Commands
git init
: Initialize the repositorygit status
: Check 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
: Print the log as one linegit log --oneline --decorate --all --graph
: Print the log as one line and in graph-modegit reflog
: Show 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>
: Show 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 a 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 <remote-name> <repository-name>
: Add a remote tracking branch.<remote-name>
is usuallyorigin
.git push --set-upstream <remote-name> <branch-name> <repository-name>
: Set a remote for a branchgit push -u <remote-name> <branch-name> <repository-name>
: Set a remote for a branch (Same to the above one)git push
: Push local commitsgit fetch
: Fetch commits from remotegit pull
: Fetch commits and mergegit fetch --prune
: Remove 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.