Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
March 19, 2022 06:32 am GMT

Git Cheat sheet for Beginners

This Git cheat sheet saves you time when you just can't remember what a command is or don't want to use git help in the command line. It is hard to memorize all the important Git commands by heart, so this can help you when you get stuck.

Setup

git --versionTo check the version of git
which gitSee where Git is located
git helpterminal-based documentation
cdNavigate to the directories using terminal
lslist files and directories
ls -Alist hidden files and directories
mkdirCreating a new directory using terminal
touch readme.txtcreating new file

Git configuration

Configuring user information used across all local repositories

git config --global user.name "First name last name"
git config --global user.email "[valid-email"

Git basics

git initinitialize an existing directory as a Git repository
git statusshow modified files in the working directory, staged for your next commit
git add [filename]Stage all changes in for the next commit.
git commit -m "Describe Message"commit your staged content as a new commit snapshot

Branches

git branchList all branches
git branch newbranchCreate a new branch
git checkout newbranchswitch to another branch and check it out into your working directory
git checkout -b newbranchCreate and switch to new branch at the same time
git branch -m branchname new_branchnameRenaming a branch
git branch -d branchnameDelete merged branch (only possible if not HEAD)
git branch -D branch_to_deleteDelete not merged branch

Merge

git merge branchnameMerge the specified branchs history into the current one
git merge --ff-only branchnameMerge to master only if fast forward
git merge --abortStop merge in case of merge conflicts
git cherry-pick 073791e7Merge a Specific commit
git checkout branchname git rebase masterRebase the current branch onto master
git rebase --abortCancel rebase
git rebase -i HEAD~3Squash Multiple commits into one

Git log

git logshow all commits in the current branchs history
git log --onelineShow oneline-summary of commits
git log -pshow changes
git log --stat --summaryShow stats and summary of commit
git log --graphShow history of commits as graph
git log --oneline --graph --all --decorateShow history of commits as graph-summary

Compare

git diffTo compare modified files
git diff --stagedTo compare modified files within the staging area
git diff master..branchnameTo compare branches
git diff 6eb715dCompare with the previous commit
git diff --ignore-space-change 6eb715d..HEADto compare without caring about spaces:
git diff --ignore-all-space 6eb715d..HEADTo compare without caring about all spaces

Make sure you replace this commit 6eb715d with your commit ID

Stash

git stashSave modified and staged changes
git stash save "Message"Put in the stash with message
git stash listlist stack-order of stashed file changes
git stash show stash@{0}Show stash stats
git stash popwrite working from the top of stash stack
git stash branch new_branchCreate branch from stash
git stash dropdiscard the changes from the top of stash stack

Tags

git tagShow all released versions
git tag -l -n1Show all released versions with comments
git tag v1.0.0Create release version
git tag -a v1.0.0 -m "Message"Create release version with comment
git checkout v1.0.0Checkout a specific release version

Reset

git revert 073791Go back to commit
git reset --soft 073791Soft reset (move HEAD only; neither staging nor working directory is changed)
git reset --soft HEAD~Undo the latest commit

Remote

git remote add origin [repo-url]Adding remote origin to local repository
git remote -vShow remote details
git remote rm originRemove origin
git branch -M mainRenaming a branch
git push -u origin masterPush changes to the remote repository
git pullPulling changes from the remote repository
git pull origin branchnamePull specific branch
git clone [repo-url]Cloning a local repository
git push origin --delete branchnameDeleting branch from the remote origin

Original Link: https://dev.to/adilshehzad786/git-cheat-sheet-for-beginners-46b4

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To