Skip to main content

Command Palette

Search for a command to run...

Git most used commands

A list of the most frequently used Git commands that have been updated since 2015

Updated
6 min read
Git most used commands

This page contains a list of basic Git commands. The information is not detailed, and the page will be updated when necessary (this list has been updated since 2015).

Configuration

Initial configuration

git config --global user.name "username"
git config --global user.email "user@domain.com"

Enlist all configurations

git config --list

Edit configuration in a text editor

git config --global --edit

Configuration list example

[core]
    editor = \\"C:\\\\Users\\\\eric\\\\AppData\\\\Local\\\\Programs\\\\Microsoft VS Code\\\\Code.exe\\" --wait
[user]
    email = eric@milaneze.com.br
    name = Eric Milaneze
[difftool "sourcetree"]
    cmd = '' \\"$LOCAL\\" \\"$REMOTE\\"
[mergetool "sourcetree"]
    cmd = "'' "
    trustExitCode = true

Store the remote repository password

git config --global credential.helper store

In this case, the password will be saved for future use when requested. For further information, please see this ShellHacks' post:

Basic commands

Init & status

# create a repository on the current directory
git init

# create a git repository to be used as remote 002 - Git Commands
git init --bare

# shows files added or modified in the stage and working directory
git status

Commit

# add all files to the staging area
git add .

git add a.txt

git add *.txt

# remove the file from the stage
git reset HEAD {{file-path}}

# goes back to a previous commit
# CAUTION, there is no going back
# actually, you can go back by getting your previous hash using reflog
git reset --hard {{commit-hash}}

git commit -m "initial commit"

# adds files in Stage to the last commit
git commit --amend -m "initial commit (editing)"

# create a new commit unlike {{commit-hash}} to reverse it
git revert {{commit-hash}}

Logs

# view the commit log
git log

# shows all commits in a row (also shows the single code of the commit in smaller size)
git log --oneline

# same as the previous one, but shows the other branchs and chart
git log --oneline --decorate --all --graph

# shows all commits in a row (also shows the hash of the commit)
git log --pretty=oneline

# works just like git log + git diff
git log -p

# shows the last two logs (last two commits)
git log -p -2 

# search for "function_name"
git log -S {{function-name}}

Branch


# shows the existing branches locally
git branch

# creates a new branch
git branch {{branch-name}}

# deletes a local branch
git branch -d nomeDaBranch

# checkout to a specific hash
git checkout {{hash}}

git checkout {{branch-name}}
git switch {{branch-name}}

# checkout to a tag
git checkout tagName

# create a branch and checkout to it
git checkout -b {{name-of-the-branch}}
git switch -c {{name-of-the-branch}}

# gives the option to analyze the differences between the two files
# and decide what to keep or not
git checkout -p source_branch -- <paths>

# open change report viewer (graphical interface)
gitk

# example:
# git remote origin https://github.com/ericmilaneze/karaoke
git remote nome url

# sends changes to the remote repository
git push origin master

# -force => ignores if any pull should be done before pushing
git push origin master --force

# takes changes in the remote without merging with local code
git fetch

# download changes to remote and apply to master branch
# (git fetch is more recommended)
git pull origin master

Tags


# creates a tag for the last commit
git tag -a v1.0 -m "Version 1.0"

# creates a tag for code commit
git tag -a v0.0 f15cf833e9a6040dfdffe1b488c9cfafe09248e2 -m "Version 0.0"

# shows details of the tag
git show v1.0

# deletes a tag
git tag -d v1.0

Files

# informs Stage of deleted files
git rm {{file-path}}

# to take from Stage
git rm --cached {{file-path}}

# check if file is tracked by git
git ls-files "{{file-path}}"

Stash

# creates a place to hide the files that are in Stage and take them from there.
git stash save "{{stash-name}}"

# lists the saved stashes
git stash list

# applies a saved stash
git stash apply stash@{0}

# erases a target stash
git stash drop stash@{0}

# apply + drop in the stack of saved stashes
git stash pop

# should be used after the save and is equal to the pop (apply + drop), but applied inside a new branch.
git stash branch {{branch-name}}

# restore file from stash
git restore -s stash@{0} -- {{filename}}

# gets a specific file from stash
git checkout stash@{0} -- {{filename}}

# check the differences in a specific file
git difftool stash@{0}..HEAD -- {{filename}}

# shows the content of an entire file (not just the changes)
git show stash@{0}:{{full-filename}}

# creates a new file base on the file from stash (could be from another branch as well)
git show stash@{0}:{{full-filename}} > {{newfile}}

Ways to delete changes

# Removes Unstaged Tracked files ONLY [Type 2]
git checkout . 

# Removes Unstaged UnTracked files ONLY [Type 3]
git clean -f -d
git clean -fd

# Removes Staged Tracked and UnStaged Tracked files ONLY [Type 1, Type 2]
git reset --hard 

# Removes all changes [Type 1, Type 2, Type 3]
git stash -u

Merge

git merge {{branchB}}

git merge -s ours {{branchB}}

git merge -X theirs {{branchB}}

# merge, but does not create any commit
# the files where the merge was done without conflicts are in Stage
# the others are waiting for the resolution of conflicts
git merge {{branchB}} --squash

# cancel merge when there are conflicts
git merge --abort

git mergetool

Rebase

# make the interactive rebase of the last two commits
git rebase -i HEAD~2

# take changes from another branch (interactive mode)
git rebase -i {{other-branch-name}}

# use this command after resolving conflicts
git rebase --continue

# ignores a commit
git rebase --skip

# aborts a rebase in progress
docker rebase --abort

Reset

# mixed - does not place changes from upcoming commits in Stage
git reset <hash>

# places changes to upcoming commits in Stage
git reset --soft <hash>

# has no return (except through "git reflog")
git reset --hard <hash>

Not-so-basic commands

Difference between commits/branches/files

# shows files that are different between the Working directory and the staging area
git diff

# shows what is on the staging area and will be comited
git diff --staged

git diff <COMMIT_HASH> -- {{file-path}}
git diff <BRANCH_NAME> -- {{file-path}}
git diff <COMMIT_HASH_1> <COMMIT_HASH_2> {{file-path}}
git diff stash@{0}^1 stash@{0} -- <filename>

# shows (with Meld/VS Code) the differences between what has current and the specified commit
git difftool -d 35c37fa607

# shows (with Meld/VS Code) the differences between what you have current and the specified branch
git difftool -d branch

# shows with Meld/VS Code the differences between what you currently have and what is in the staging area
git difftool -d --staged

git difftool <COMMIT_HASH> {{file-path}}
git difftool <BRANCH_NAME> {{file-path}}
git difftool <COMMIT_HASH_1> <COMMIT_HASH_2> {{file-path}}

Logs

# get all the changes/commits (even after a hard reset => reset --hard)
git reflog

Cherry-pick

# adds the commit to the top of the current branch
git cherry-pick {{commit-hash}}

# copy all the commits, including the first
git cherry-pick {{commit-hash}}^..{{other-commit-hash}}

# copy all the commits, ignoring the first
git cherry-pick {{commit-hash}}..{{other-commit-hash}}