Hacktoberfest is upon us. Here is a git cheat sheet that you can keep handy for your future hackings:
0. Git Configuration
Main article: Seven Git Configs to Set When Moving to a New Machine
When starting with git for the first time (on a new machine), there are a few configs you should set.
# set name and email
git config --global user.name "Mohammad-Ali A'râbi"
git config --global user.email "my-name-at-work@employer.com"
# set default branch name
git config --global init.defaultBranch master
# set rebase the default pull policy
git config --global pull.rebase true
# set to auto-stash upon rebase(/pull)
git config --global rebase.autoStash true
# set to default branch name upon push
git config --global push.default current
For the default branch name, main
is gaining more popularity replacing the good old master
. Alternatives are trunk
and develop
, so feel free.
We will also get into stashing in a bit.
1. Basic Git Workflow
These commands are the bread and butter of working with Git. They help you interact with the repository and manage your working directory.
Cloning a Repository
Clone a remote repository to your local machine.
git clone <repository-url>
You can copy the repository URL from the repo's GitHub/GitLab page.
Check Repository Status
Check the status of your working directory.
git status
This command shows which files are changes, which ones are added for committing, etc.
Adding Changes
Stage changes (add files to the staging area) before committing.
git add <file-name>
I will skip the command for "staging all changes" as that's a bad practice.
Commit Changes
Main article: Ten Commandments of Git Commit Messages
After staging, commit changes with a meaningful message.
git commit -m "Commit message"
Viewing Commit History
Check your commit history.
git log
For a simpler, one-line format:
git log --oneline
Push Changes to Remote
Push committed changes to the remote repository.
git push
2. Branching and Merging
Main article: 7 Tips to Work with Branches on Git
Branching allows you to work on different parts of your project independently. Here's how to manage branches:
Create a New Branch
Create a new branch for your feature or bug fix.
git switch -c <branch-name>
If you just want to create the new branch and don't want to switch to it:
git branch -c <branch-name>
Switch to Another Branch
Main article: Git Commands to Use Instead of Checkout
git switch <branch-name>
List All Branches
See all branches in your repository.
git branch
Merge a Branch
Merge changes from one branch into your current branch.
git merge <branch-name>
Delete a Branch
Once a branch is no longer needed, you can delete it.
# Locally
git branch -d <branch-name>
# Remotely
git push origin --delete <branch-name>
3. Stashing Changes
Stashing temporarily stores your changes without committing them.
Save Changes to a Stash
If you're working on something and want to switch branches without committing:
git stash
Apply Stash
To retrieve your changes later:
git stash apply
List Stashed Changes
If you have multiple stashes, you can see them with:
git stash list
Final Words
The following articles should also be useful:
Top comments (12)
master
is a bit outdated,main
is nowadays more used.Since the name does not contain spaces or special characters it is not required to be wrapped in quotation marks (
"
)Thanks for the comment. I added a few lines explaining the fact you just mentioned. Also, the quotation mark were left originally for a better visibility through syntax highlight.
Very cool post to preparing for the Hacktoberfest!
Thanks, Thomas. 😊
Looking forward to it. 🥳
Great work! Nice design and amazing organization.
Would you consider adding
git clean
andgit bisect
?Those are commands I use a lot :)
Thanks Martin. 😊
Definitely. Do you think it would be better if I add them to a follow-up article, or should I add them here?
A follow up would be great!
This hacktoberfest, we are inviting contributors:
github.com/ujjwall-R/Dree
join our community group : join.slack.com/t/dreecommunity/sha...
Nice read, Mohammad
We look forward to Hacktoberfest
Thanks, Chidera! 🫡
Yeah, me too.
Good and and in simplicity way to recap on Git
Thank you. i am new to Git so this helps
Some comments may only be visible to logged-in visitors. Sign in to view all comments.