Skip to main content

Git CheatSheet for Developers

Git Configuration

CommandDescription
git configCheck all configuration options
git config --listCheck all configuration options with name and email
git clone [https://url]Clone source code from a remote repository
git config --global user.name "Your name"Configure username
git config --global user.email "Your email"Configure email
git config --global core.editor vimConfigure editor
git config user.nameList username
git config user.emailList email

Getting & Creating Projects

CommandDescription
git initInitialize a local Git repository

Basic Commands

CommandDescription
git statusCheck status
git diffCompare and show updated codes
git add [file name]Add file changes in next commit
git add .Add all unstaged changes in next commit
git reset [file name]Used to unstage the staged files
git clean -fTo delete or remove unstaged files forcefully
git commit -m "message about updates"Commit changes to current branch
git commit -amendAmend with last commit but use the previous commit log message
git rm --cached [file]Removes the file from the staging area (Unstage)
git rm [file]Deletes the file from your working directory and stages the deletion
git pullFetches and merges changes on the remote server to your working directory
git pull --allow-unrelated-historiesPull changes form remote branch with unrelated histories
git fetchGathers remote commits but does not merge them unlike pull
git remote add origin [url]Adding a remote repository
git showShows information about any git object
gitkShows graphical interface for a local repository

Branching & Merging

CommandDescription
git branchList branches
git branch [branch-name]Create a local branch
git branch -m [old branch name] [new branch name]Rename a local branch
git branch -d [branch-name]Delete a branch
git branch -D [branch name]Delete a branch forcefully
git branch -aSee all branches (local and remote)
git checkout [branch-name]Switch to another branch
git checkout -b "branch name"Create a new branch and switch to that branch
git checkout -b [branch name] origin/[branch name]Clone a remote branch and switch to it
git switch [branch-name]Switch to another branch
git merge [branch-name]Merge branchs
git merge [source branch] [target branch]Merge a branch into a target branch
git merge --allow-unrelated-historiesMerge unrelated histories
git cherry-pick [commit-ID]Bring in changes from one (or more) particular commit to the current branch.

Discard Changes

CommandDescription
git checkout -Switch to the branch last checked out
git checkout -- [file-name.txt]Discard changes to a file
git checkout [file]Matches the file with last commit
git restore .To restore all files in the current directory
git revert [commit-ID]Create new commit, reverting the changes from a specified commit in remote branch
git help -aShows the list of all available Git commands

Set Upstream Branch

CommandDescription
git push --set-upstream origin current-branch-name
or
git push -u origin current-branch-name
- To push the current branch and set the remote as upstream
- git push --set-upstream origin development
- git push -u origin development
git pull origin main
or
git pull origin development
- Update the current branch from main branch
- Update the current branch from development branch
- Before updating, push you all modification to remote brach
- Otherwise every modification will be removed

Sharing & Updating Projects

CommandDescription
git push origin [branch name]Push a branch to your remote repository
git pull origin [branch name]Pull a branch from your remote repository
git remote origin [branch name]Connect repository to local server
git push <remote> --forceForces the push even if it results in a non-fast-forward merge. Be sure that nobody has pulled the commits before using the --force option.

Inspection & Comparison

CommandDescription
git logView changes
git log --summaryView changes (detailed)
git log --follow [file]Show the commits that changed file, even across renames
git log --onelineView changes (briefly)
git log branchB..branchAView changes on branchA that are not on branchB
git log --graphEnables you to view your git log as a graph
git log --decorateMakes git log display all of the references (e.g., branches, tags, etc) that point to each commit
git log --author="name_of_author"Search for specific author
git shortlogIt groups each commit by author and displays the first line of each commit message
git reflog Git keeps track of updates to the tip of branches using a mechanism called reflog. This allows you to go back to changesets even though they are not referenced by any branch or tag
git diff branchB...branchAView the differences of what is in branchA that are not on branchB
git blame [file name]Display the modification on each line of a file
git diff --name-onlyShow only names of changed files
git bisect startStarts the bisection search process to find that bad commit which introduced the bug we're facing right now
git bisect good
git bisect good [Commit ID]
Takes up the good commit, which is that one where the bug was not there
git bisect bad [Commit ID]Takes up the bad commit, which is that one where the bug was there. If commit ID is not provided, then it takes up the current commit as the bad commit
git grep "hello"A text search on all files in the directory
git bugreportCreated new report at git-bugreport-2022-10-25-1228.txt

Tracking Path Changes

CommandDescription
git rm [file]Delete the file from project and stage the removal for commit
git mv [existing-path] [new-path]Change an existing file path and stage the move
git log --stat -MShow all commit logs with indication of any paths that moved

Setting up Alias

CommandDescription
git config --global alias.[short name for command] [actual command]Alias make the commands short and handy

Rewrite History

CommandDescription
git rebase [branch]Apply any commits of current branch ahead of specified one
git reset --hard [commit]Clear staging area, rewrite working tree from specified commit
git reset HEAD "file-name"Go back to the pointer I'm already at, remove from staged
git reset --soft "commit-hash"Take my changes and go back/kill the commit I made but the file will be staged with the change ready to be committed again
git reset --mixed "commit-hash"Return/Kill the commit too but it returns the files to before staged i.e. modified but it will still return
git reset --hard "commit-hash"It will simply ignore the existence of this commit and undo everything that was done in this commit.
It is a very rough reset and is normally used before pushing your commit to the remote repository.

Deletion

CommandDescription
git gcCleans unnecessary files and optimizes the local repository
git pruneDeletes objects that don’t have any incoming pointers

Temporary Commits

commandDescription
git stashSave modified and staged changes
git stash listlist stack-order of stashed file changes
git stash popwrite working from top of stash stack
git stash dropdiscard the changes from top of stash stack