Set the global config
git config --global user.name "[name]"
git config --global user.email "[email]"
Create a git repository
git init
Clone an existing git repository
git clone [url]
Commit all tracked changes
git commit -am "[commit message]"
Add new modifications to the last commit
git commit --amend --no-edit
Change last commit message
git commit --amend
Undo most recent commit and keep changes
git reset HEAD~1
Undo the N
most recent commit and keep changes
git reset HEAD~N
Undo most recent commit and get rid of changes
git reset HEAD~1 --hard
Reset branch to remote state
git fetch origin
git reset --hard origin/[branch-name]
Renaming the local master branch to main
git branch -m master main
Git is a decentralized version management software. Think of it as a time machine for your code. It allows you to travel back in time to see what your code looked like in the past, and if necessary, revert back to that point.
Git is crucial in the world of software development. It allows for collaboration, keeping track of changes, and maintaining the integrity of a project. It’s like a safety net for developers, allowing them to experiment without the fear of losing or overwriting their work.
Before diving into commands, let’s get familiar with some git terminology:
Here are some of the most common git commands:
git init
: Initializes a new Git repositorygit add
: Adds a file to the staging areagit commit
: Saves your changes to the local repositorygit push
is like sending your local changes to the remote repository. Imagine it as sending a letter. Your local changes are the letter content, and git push
is the act of mailing it.
git pull
is the opposite of git push
. It fetches changes from the remote repository and merges them into your local repository. It’s like receiving a letter in the mail and adding its contents to your personal record.
git commit
is like saving a game. It creates a snapshot of your changes, which you can revisit anytime.
git commit --amend
lets you modify the last commit. It’s like having a conversation and realizing you misspoke, so you quickly correct yourself.
git rebase
is a way to integrate changes from one branch into another. It’s like weaving two threads together to create a stronger, unified piece.
git merge
is used to combine changes from different branches. It’s like merging lanes on a highway.
git reset
is used to undo changes. It comes in three flavors: soft, mixed, and hard. It’s like having an undo button in a word processor.
We’ve covered a lot of ground in this Git cheatsheet, from basic concepts and terminology to more advanced commands.
Cheatsheets like this one are invaluable tools for developers. They provide quick access to information, saving time and increasing productivity.