Skip to content

Instantly share code, notes, and snippets.

@adojos
Last active September 18, 2023 03:01
Show Gist options
  • Save adojos/cf5ae65acdfc4202462024c0e90f34ba to your computer and use it in GitHub Desktop.
Save adojos/cf5ae65acdfc4202462024c0e90f34ba to your computer and use it in GitHub Desktop.
Git: Common Git Commands #git

Git Configuration Commands


Commands Description
git config --list Prints all your git configuration settings
git config --list --show-origin Prints all git configuration settings along with file name and path to git configuration files. You would see which configuration is set where?
git config --list --show-origin --show-scope Prints all git configuration settings and scope along with file name and path to git configuration files. You would see which configuration is set where?
git config --global user.name "Your name here" Sets your global username in git config
git config --global user.email "your_email@example.com" Sets your global email in git config

Git Repository Commands


Commands Description
git init (project_name) Create a new local repository
git clone project_url Download from an existing repository
git add (file_name) Stage the mentioned file, ready for commit
git add . Stages all changed files ready for commit
git add -A Recursively add all new files to staging area, ready for commit
git commit -m "commit message" Commit all staged files/changes to repository
git commit -am "commit message" First add all tracked files to staging area and then commit files/changes to repository
git reset (file_name) Unstage mentioned file, keeping the file changes
git reset --hard Revert everything to last commit
git remote add origin repo_url Add a remote repository using url provided via repo_url
git remote set-url origin repo_url Set a repository's origin branch to SSH

Git Repository Diagnostic Commands


Commands Description
git status List new or modified files not yet committed
git diff Show the changes to files which are not yet staged
git diff --cached Show the changes to staged files
git diff HEAD Show all staged and unstaged file changes
git diff commit_ID1 commit_ID2 Show the changes between two mentioned commit IDs
git blame file_name List the change dates and authors for a file
git show [commit_ID] : [file_name] Show the commit changes for a commit ID and/or file
git log Show full change history
git log -p [file/directory] Show change history for file/directory including diffs
git log --summary View detailed changes
git log --oneline View brief changes

Git Branches Commands


Commands Description
git branch List all local branches
git branch -av List all branches both local and remote
git checkout my_branch Switch to the mentioned branch and update working directory
git branch new_branch Create a new branch called 'new_branch'
git branch -d my_branch Delete the branch called 'my_branch'
git checkout -b [branch name] origin/[branch name] Clone a remote branch and switch to it
git branch -m [old branch name] [new branch name] Rename a local branch
git checkout branch_a
git merge branch_a
Merge branch_a into branch_b
git merge [branch name] Merge a branch into the active branch
git merge [source branch] [target branch] Merge a branch into a target branch
git stash Stash changes in a dirty working directory
git stash clear Remove all stashed entries
git tag my_tag Tag the current commit

Git Synchronize Commands


Commands Description
git fetch Get the latest changes from origin
git pull Fetch the latest changes from origin and merge
git pull origin [branch name] Pull changes from named remote repository
git pull --rebase Fetch the latest changes from origin and rebase
git push Push local changes to origin (remembered branch)
git push origin [branch name] Push a branch to named remote repository

Reference Links

Official Git SCM Commands Reference

Official Git SCM Tutorials


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment