Git
What is Git and how to use it.
What is Git?
Git is a popular open-source version control tool commonly used for code but can be used with all types of files
More information about Git can be found on its website
Install
Git is typically installed by default on Linux distributions and MacOS however it will need to be installed on Windows
Windows:
winget install git.git
Linux:
sudo apt install git
MacOS:
brew install git
Pushing Local Code to a Remote Repository
With an empty remote repository, we can add local code to it
Cloning Repositories Locally
We can download a repository locally if we have permission
Merging Code
When you want to merge your changes to a repo
GitLab - Merge Request (MR)
GitHub - Pull Request (PR)
Merge Conflicts
This can occur during a merge when your file is different than an existing file and git is unable to determine which changes should be committed.
The error may look like this after running
git push origin <branchName>
To fix, run
git pull origin <branchName>
(the conflicting branch) and identify the problem file(s) in the output.
Open the file (GUI, IDE, etc.) and select the data to keep removing all the git added stuff and leaving only the data you want.
Fork
Someone with READ access can download your repo/code, make changes, and then open a Merge Request for you to approve
Rebasing
When the branch you're working on is behind commits from others
Merging
Running this command from your branch brings changes from others into your branch and keeps the git commit history intact
Rebasing
Running this command moves your branch on top of the updated branch, changing the git commit hashes from others.
Not a big deal but your coworkers may not like this due to the commit history changing
Squashing Commits
When you have multiple commits and want to squash them into one
The final commit will be
Addng new feature
If you've already deployed a commit to a remote branch, but now need to revert it or change it, here's how you can squash them,
Let's say you want to squash the new changes into the old commit
Cherry Picking
When you want to incorporate a particular commit from another branch into your branch
Reverting Commits
You've committed some changes and now want to undo them
Keeping Commit History
Removing the commit shows the file(s) as deleted in the commit history
Undoing Commit
This undoes your commit
Use
--soft
to undo the commit but keep your modified file(s)Use
--hard
to undo the commit and delete your modified file(s)
Stashing
Storing your code when you don't want to commit it yet but need to hop to another branch and don't want your modified code to come with you
Reflog
Shows the state of the repository and enables you to undo mistakes e.g., you deleted a file
Useful Commands
General
Branches
Modify Terminal to show current git branch
Update your shell's configuration file e.g.,
~/.zshrc
or~/.bashrc
Save and then reload your shell for the changes to take effect
source ~/.zshrc
Last updated