Git Cheat Sheet - Alek772.github.io

Transcription

Git Cheat SheetThe essential Git commands every developer must know

This cheat sheet covers all of the Git commands I’ve covered in my Ultimate GitMastery course. Creating snapshots Browsing history Branching & merging Collaboration using Git & GitHub Rewriting history

Hi! My name is Mosh Hamedani. I’m a software engineer withtwo decades of experience. I’ve taught millions of people howto code or how to become a professional software engineerthrough my YouTube channel and online coding school. It’s mymission to make software engineering simple and accessibleto everyone.Check out the links below to master the coding skills you edanihttps://facebook.com/programmingwithmosh/

Want to master Git?Stop wasting your time memorizing Git commands or browsing disconnectedtutorials. If you don’t know how Git works, you won’t get far.My Ultimate Git Mastery course teaches you everything you need to know to useGit like a pro. Learn & understand Git inside out Master the command line Version your code and confidently recover from mistakes Collaborate effectively with others using Git and GitHub Boost your career opportunitiesClick below to enroll course/

Table of ContentCreating Snapshots6Browsing History8Branching & Merging10Collaboration12Rewriting History13

Creating SnapshotsInitializing a repositorygit initStaging filesgit add file1.js# Stages a single filegit add file1.js file2.js# Stages multiple filesgit add *.js# Stages with a patterngit add .# Stages the current directory and all its contentViewing the statusgit status# Full statusgit status -s# Short statusCommitting the staged filesgit commit -m “Message” # Commits with a one-line messagegit commit# Opens the default editor to type a long messageSkipping the staging areagit commit -am “Message”Removing filesgit rm file1.js# Removes from working directory and staging areagit rm --cached file1.js# Removes from staging area onlyRenaming or moving filesgit mv file1.js file1.txt

Viewing the staged/unstaged changesgit diff# Shows unstaged changesgit diff --staged# Shows staged changesgit diff --cached# Same as the aboveViewing the historygit log# Full historygit log --oneline# Summarygit log --reverse# Lists the commits from the oldest to the newestViewing a commitgit show 921a2ff# Shows the given commitgit show HEAD# Shows the last commitgit show HEAD 2# Two steps before the last commitgit show HEAD:file.js# Shows the version of file.js stored in the last commitUnstaging files (undoing git add)git restore --staged file.js # Copies the last version of file.js from repo to indexDiscarding local changesgit restore file.js# Copies file.js from index to working directorygit restore file1.js file2.js# Restores multiple files in working directorygit restore .# Discards all local changes (except untracked files)git clean -fd# Removes all untracked filesRestoring an earlier version of a filegit restore --source HEAD 2 file.js

Browsing HistoryViewing the historygit log --stat# Shows the list of modified filesgit log --patch# Shows the actual changes (patches)Filtering the historygit log -3# Shows the last 3 entriesgit log --author “Mosh”git log --before “2020-08-17”git log --after “one week ago”git log --grep “GUI”# Commits with “GUI” in their messagegit log -S“GUI”# Commits with “GUI” in their patchesgit log hash1.hash2# Range of commitsgit log file.txt# Commits that touched file.txtFormatting the log outputgit log --pretty format:”%an committed %H”Creating an aliasgit config --global alias.lg “log --oneline"Viewing a commitgit show HEAD 2git show HEAD 2:file1.txt# Shows the version of file stored in this commitComparing commitsgit diff HEAD 2 HEAD# Shows the changes between two commitsgit diff HEAD 2 HEAD file.txt # Changes to file.txt only

Checking out a commitgit checkout dad47ed# Checks out the given commitgit checkout master# Checks out the master branchFinding a bad commitgit bisect startgit bisect bad# Marks the current commit as a bad commitgit bisect good ca49180# Marks the given commit as a good commitgit bisect reset# Terminates the bisect sessionFinding contributorsgit shortlogViewing the history of a filegit log file.txt# Shows the commits that touched file.txtgit log --stat file.txt# Shows statistics (the number of changes) for file.txtgit log --patch file.txt# Shows the patches (changes) applied to file.txtFinding the author of linesgit blame file.txt# Shows the author of each line in file.txtTagginggit tag v1.0# Tags the last commit as v1.0git tag v1.0 5e7a828# Tags an earlier commitgit tag# Lists all the tagsgit tag -d v1.0# Deletes the given tag

Branching & MergingManaging branchesgit branch bugfix# Creates a new branch called bugfixgit checkout bugfix# Switches to the bugfix branchgit switch bugfix# Same as the abovegit switch -C bugfix# Creates and switchesgit branch -d bugfix# Deletes the bugfix branchComparing branchesgit log master.bugfix# Lists the commits in the bugfix branch not in mastergit diff master.bugfix# Shows the summary of changesStashinggit stash push -m “New tax rules”# Creates a new stashgit stash list# Lists all the stashesgit stash show stash@{1}# Shows the given stashgit stash show 1# shortcut for stash@{1}git stash apply 1# Applies the given stash to the working dirgit stash drop 1# Deletes the given stashgit stash clear# Deletes all the stashesMerginggit merge bugfix# Merges the bugfix branch into the current branchgit merge --no-ff bugfix# Creates a merge commit even if FF is possiblegit merge --squash bugfix # Performs a squash mergegit merge --abort# Aborts the merge

Viewing the merged branchesgit branch --merged# Shows the merged branchesgit branch --no-merged# Shows the unmerged branchesRebasinggit rebase master# Changes the base of the current branchCherry pickinggit cherry-pick dad47ed# Applies the given commit on the current branch

CollaborationCloning a repositorygit clone urlSyncing with remotesgit fetch origin master# Fetches master from origingit fetch origin# Fetches all objects from origingit fetch# Shortcut for “git fetch origin”git pull# Fetch mergegit push origin master# Pushes master to origingit push# Shortcut for “git push origin master”Sharing tagsgit push origin v1.0# Pushes tag v1.0 to origingit push origin —delete v1.0Sharing branchesgit branch -r# Shows remote tracking branchesgit branch -vv# Shows local & remote tracking branchesgit push -u origin bugfix# Pushes bugfix to origingit push -d origin bugfix# Removes bugfix from originManaging remotesgit remote# Shows remote reposgit remote add upstream url# Adds a new remote called upstreamgit remote rm upstream# Remotes upstream

Rewriting HistoryUndoing commitsgit reset --soft HEAD # Removes the last commit, keeps changed stagedgit reset --mixed HEAD # Unstages the changes as wellgit reset --hard HEAD # Discards local changesReverting commitsgit revert 72856ea# Reverts the given commitgit revert HEAD 3.# Reverts the last three commitsgit revert --no-commit HEAD 3.Recovering lost commitsgit reflog# Shows the history of HEADgit reflog show bugfix# Shows the history of bugfix pointerAmending the last commitgit commit --amendInteractive rebasinggit rebase -i HEAD 5

This cheat sheet covers all of the Git commands I’ve covered in my Ultimate Git Mastery course. Creating snapshots Browsing history Branching & merging Collaboration using Git & GitHub Rewriting history. Check out the links below to master the coding skills you need: https://codewithmosh.com https://youtube.com/user/programmingwithmosh .