Git Cheat Sheet
What is Git?
Git is a distributed version control system (VCS) used in software development to manage source code and track changes over time. It allows multiple developers to work on a project simultaneously and collaborate effectively.
At its core, Git helps developers track modifications made to files in a project. Instead of saving complete copies of each file for every change, Git focuses on capturing and storing the differences or “delta” between versions. This approach makes Git efficient in terms of storage and allows for faster operations.
Git operates on the concept of a repository, which is a collection of files and their complete history. Each developer has their local copy of the repository, enabling them to work independently and commit changes to their local version. These commits create a timeline of the project’s development, forming a complete history.
Git supports branching, allowing developers to create separate lines of development. Branches can be used to work on new features, bug fixes, or experiments without affecting the main codebase. Branches can be merged back into the main branch when the changes are complete.
Collaboration is a key aspect of Git. It enables developers to share their changes by pushing their local commits to a remote repository hosted on a server. Remote repositories, such as GitHub, GitLab, and Bitbucket act as centralized points for sharing and syncing code between team members. This allows developers to work together, review each other’s changes, and resolve conflicts that may arise.
Key benefits of Git include
- Version control: Git tracks changes to files, making it easy to revert to previous versions or investigate the history of the project.
- Collaboration: Git facilitates effective collaboration among team members, allowing them to work simultaneously on different parts of a project.
- Branching and merging: Git’s branching and merging capabilities provide flexibility in managing parallel lines of development and integrating changes.
- Speed and efficiency: Git’s design enables fast and efficient operations, making it suitable for projects of any size.
Git is not the only version control system or tool out there, however, it is widely adopted in software development due to its reliability, flexibility, and ability to handle both small and large-scale projects effectively.
Git comes with different commands, which can be overwhelming. Below is the list of commands that you will get to use more often.
Set your Git usernamegit config --global user.name "Your Name"
Set your Git email addressgit config --global user.email "[email protected]"
Store login credentials in the cache so you don’t have to type them in each timegit config --global credential.helper cache
List all Git configuration settingsgit config --list
or git config -l
Creating a new repository
Create a new local repositorygit init
Clone an existing repository from a remote sourcegit clone [url]
Adding and committing changes
Add changes to the staging areagit add filename_here
Add all changes to the staging areagit add .
Commit changes to the local repository with a messagegit commit -m "Commit message"
Commit changes and skip the staging areagit commit -a -m"your commit message here"
orgit commit -am "your commit message here"
Amend the most recent commit messagegit commit --amend "the correct commit message"
Revert unstaged changesgit restore filename
Revert staged changesgit restore --staged filename
Rollback the last commitgit revert HEAD
Rollback to an old commitgit revert commit_id
Working with branches
List all branchesgit branch
Create a new branchgit branch [branch-name]
Switch to a specific branchgit checkout [branch-name]
Create a new branch and switch to it instantlygit checkout -b [branch_name]
Merge changes from a specific branch into the current branchgit merge [branch-name]
Delete a branchgit branch -d [branch_name]
Push changes to a remote repositorygit push origin [branch-name]
Working with remote repositories
Add a remote repositorygit remote add origin [url]
Fetch and merge changes from a remote repositorygit pull
Push changes to a remote repositorygit push
List all remote repositoriesgit remote -v
Display all remote branch(es) being trackedgit branch -r
Remove a remote branchgit push --delete origin [branch_name]
Other helpful commands
git status
: Show the status of your repository
git log
: Show the commit history of your repository
git diff [file]
: Show the changes made to a specific file.
git push -f
This will forcefully push a git commit to the remote repository
Conclusion
These git commands will improve your productivity as a developer. You don’t have to memorize them — that’s the reason for this cheat sheet. Please go ahead and bookmark this page or print this page out for future reference.
Originally published at https://codaholic.com on June 25, 2023.