Git
đ Resources đ
GitHub Git Cheat Sheet
Notes taken from âĄī¸ GitHub Git Cheat Sheet
Install
GitHub Desktop
Git for All Platforms
Configure tooling
Configure user information for all local repositories
Sets the name you want attached to your commit transactions
Sets the email you want attached to your commit transactions
Enables helpful colorization of command line output
Branches
Branches are an important part of working with Git. Any commits you make will be made on the branch youâre currently âchecked outâ to. Use git status
to see which branch that is.
Creates a new branch
Switches to the specified branch and updates the working directory
Combines the specified branchâs history into the current branch. This is usually done in pull requests, but is an important Git operation.
Deletes the specified branch
Create repositories
A new repository can either be created locally, or an existing repository can be cloned. When a repository was initialized locally, you have to push it to GitHub afterwards.
The git init command turns an existing directory into a new Git repository inside the folder you are running this command. After using the git init
command, link the local repository to an empty GitHub repository using the following command:
Specifies the remote repository for your local repository. The url points to a repository on GitHub.
Clone (download) a repository that already exists on GitHub, including all of the files, branches, and commits
The .gitignore file
Sometimes it may be a good idea to exclude files from being tracked with Git. This is typically done in a special file named .gitignore
. You can find helpful templates for .gitignore
files at github.com/github/gitignore.
Synchronize changes
Synchronize your local repository with the remote repository on GitHub.com
Downloads all history from the remote tracking branches
Combines remote tracking branches into current local branch
Uploads all local branch commits to GitHub
Updates your current local working branch with all new commits from the corresponding remote branch on GitHub. git pull
is a combination of git fetch
and git merge
Make changes
Browse and inspect the evolution of project files
Lists version history for the current branch
Lists version history for a file, beyond renames (works only for a single file)
Shows content differences between two branches
Outputs metadata and content changes of the specified commit
Snapshots the file in preparation for versioning
Records file snapshots permanently in version history
Redo commits
Erase mistakes and craft replacement history
Undoes all commits after [commit]
, preserving changes locally
Discards all history and changes back to the specified commit
CAUTION! Changing history can have nasty side effects. If you need to change commits that exist on GitHub (the remote), proceed with caution. If you need help, reach out at github.community or contact support.
Glossary
git: an open source, distributed version-control system
GitHub: a platform for hosting and collaborating on Git repositories
commit: a Git object, a snapshot of your entire repository compressed into a SHA
branch: a lightweight movable pointer to a commit
clone: a local version of a repository, including all commits and branches
remote: a common repository on GitHub that all team members use to exchange their changes
fork: a copy of a repository on GitHub owned by a different user
pull request: a place to compare and discuss the differences introduced on a branch with reviews, comments, integrated tests, and more
HEAD: representing your current working directory, the HEAD pointer can be moved to different branches, tags, or commits when using
git switch
Version Control
Version control - practice of tracking and managing changes to software code, allowing multiple people to simultaneously work on a single project
keeps a history of changes, everything is tracked
commits, labeled with a message, can be reverted
Remote Git repository - code is hosted centrally on the internet (Gitlab, Github, etc)
Local Git repository - the user has an entire copy of the code locally
Code is pulled (fetched) from remote repo and pushed to it.
Git client - command line or GUI tool to execute git
commands
History - of code changes git log
Staging - where working changes are saved (to commit or not)
Remote Git repository
Online
Self hosted
Gitlab
Gitea
There are private and public repositories.
Create a GitLab/GitHub user and a private project
Install git client.
Connect the
git
client with GitHub (I'm using it for the training)
Create an SSH key pair and add the public SSH key to GitHub
Clone the remote repository via SSH to the local machine
The same can be done for GitLab too
Branches
A main
branch (also called master
) is created by default with a new initialized repository.
Other temporary branches can be created and worked on, e.g. for a feature or a bugfix, without touching the main branch.
A branch is based on the main
branch and can be merged back to the main branch.
Try to create a branch in the UI
main
- state ready and stable for production, tested code
develop
- for features and bugfixes during sprint - at the end of the sprint and testing develop
is merged into main
The branches can be left or deleted (better to clean them)
Create a new branch when needed
HEAD
= latest commit in the branch
Git commands
Move the changes of a file from "working directory" to the "staging area"
Proceed with the commit by saving the changes in the local repository
Show local history
history of commits
Publish the local commits to the remote Git repo
Create an empty Git repository or reinitialize a local existing one (it creates a
.git
directory inside the project directory). Push it online, after creating a remote empty repository - e.g.test-repo
Check branches
Create a Pull request (merge request) from the GUI - a request to merge one branch into another. A reviewer see the changes and approve or decline the PR (Github Rulesets, Actions (for CI) can be set).
Avoid a merge of the local and modified remote branch, by first pulling the changes from remote branch and stack the local changes on top of them
When having conflict, resolve them using a UI editor, like VSCode.
.gitignore
- exclude folders or files from git to be tracked by specifying them in the.gitignore
file in the repo root directory.exclude build directories, only local necessary files/directories/dependencies
Stash - save work-in-progress or hide local changes. Happens when changing branches for example.
Undoing and changing commits
Revert a commit in the
main
/dev
team working branches
Merging from other branches when the working local branch is diverting too much from the main/dev branch
GIT for DevOps
Configuration file should be tracked, versioned and securely stored.
Use cases:
Infrastructure as Code
Kubernetes, Terraform, Ansible configuration files
Automation scripts
Bash/Python scripts files, written to create and provision infrastructure
CI/CD pipelines
checkout code, test and build application
setup integration with build automation tools and app git repository
check git changes, commits hash, etc
Best practices
Push and pull often from remote repo (Continuous Integration)
Donât
git push
straight to master/main branchUse
--force
push carefullyCreate a separate branch for each feature or bugfix and name the branch with prefix âfeature/xxâ and âbugfix/xxxâ respectively
Doing code reviews via Merge Requests
Use
.gitignore
file to ignore e.g. editor specific files, build folders
Commits
use descriptive and meaningful commit messages - check Conventional Commits
commit in relatively small chunks and only related work
adequately configure the commit authorship (name and email address) with
git config
Avoiding very large deviations between local and remote repository
keep your feature-bugfix branch up-to-date with remote
master
/main
and/ordevelop
branchbranches shouldnât be open for too long
master
/main
branch should be merged into your feature/bugfix branch often
Last updated