DEV Community

Zamzam Hassan
Zamzam Hassan

Posted on

Git cheat sheet

Setting up Git
git config --global user.name "Your Name" : Set your Git username
git config --global user.email "youremail@example.com" : Set your Git email address
git config --list : List all Git configuration settings

Creating a new repository
git init : Create a new local repository
git clone [url]: Clone an existing repository from a remote source

Adding and committing changes
git add [file] : Add changes to the staging area
git add . : Add all changes to the staging area
git commit -m "Commit message" : Commit changes to the local repository with a message

Working with branches
git branch: List all branches
git branch [branch-name]: Create a new branch
git checkout [branch-name]: Switch to a specific branch
git merge [branch-name] : Merge changes from a specific branch into the current branch
git push origin [branch-name] : Push changes to a remote repository

Working with remote repositories
git remote add origin [url] : Add a remote repository
git pull : Fetch and merge changes from a remote repository
git push : Push changes to a remote repository
git remote -v : List all remote repositories

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.

These are some of the most commonly used Git commands. I hope this cheat sheet helps you in your Git journey!

Top comments (7)

Collapse
 
offline profile image
Offline • • Edited

For example, if you’re using Github, please use your Github email/username adress, you don’t want everybody see your private email adress:

git config --global user.email "username@users.noreply.github.com"

Collapse
 
knightndgale profile image
Mark-Dave-S •

I'd suggest git switch for just switching. git checkout can be a little broad. correct me if I'm wrong.

Collapse
 
misszamzam profile image
Zamzam Hassan •

I will give it a try, thanks

Collapse
 
ant_f_dev profile image
Anthony Fung •

Great summary of some common (and not-so-common) commands!

I'd also recommend taking a look at Git Extensions for anyone on Windows. It's free and lets you do many things from a UI. Most actions within the UI result in performing something command-line based, which will be shown in a popup window. This helps when trying to learn Git commands too.

I personally use a combination of Git Extensions and the command line.

Collapse
 
misszamzam profile image
Zamzam Hassan •

Thanks for that piece of information.

Collapse
 
neji profile image
Newton Jillian •

Great work here. These are most common git commands. Can you do a part 2 of the post talking of more other git commands. Guess it will also help you learn more as You'll practice on them before posting them

Collapse
 
misszamzam profile image
Zamzam Hassan •

Thank you for your feedback. I appreciate your suggestion to create a part 2, I will definitely consider it.