Git Commands

Git Commands lists some basic commands that makes working with GIT easier. It’s a go-to cheat-sheet for everyday GIT.

Initialize an empty git repository

git init

Add (stage) all the added/modified files for commit

git add .

To add specific files, use

git add path/to/file1.txt path/to/file2.ts

Commit all the staged files from the above command

git commit -m "your message"

Push the changes to master branch

git push origin master

Push the changes to any other branch

git push origin branch_name

To see all the remote branches and their tracking urls

git remote -v

Add a remote url for tracking

git add remote remote_name your_remote_url

If adding the first remote, set the remote_name preferably to origin like

git add remote origin your_remote_url

Update an already existing remote url

git remote set-url origin new_remote_url

Create a new branch in the local

git branch new_branch_name
git checkout new_branch_name

Can be shortened to

git checkout -b new_branch_name

Push the new branch and the changes to the git repo and also set an upstream for it

git push -u origin new_branch_name

One can also set/change a branch tracking relationship by

git branch -u origin/master // Any remote_name/branch_name

To reset the files that have been added with git add …

git reset HEAD
git reset --soft HEAD^

To reset the files that were committed but not pushed, and still keep the local changes

git reset origin/branch_name

To reset the files that were committed but not pushed, and remove the local changes

git reset --hard HEAD

To reset the files that were committed but not pushed, and remove the local changes – for a particular branch

git reset --hard origin/branch_name

Rename a branch in the local git repo
If on the same branch

git branch -m new_branch_name

If on another branch,

git branch -m old_branch_name new_branch_name

To store the current changes to a pointer and reset the working directory to the HEAD commit, basically a clean working directory

git stash

To stash all the un-tracked files too

git stash --include-untracked

To view a stash, where n is the index of the stash

git stash show n

To have a name based save

git stash save "my stash name"

To recover the changes and write to the working directory.

git stash pop

To retrieve the stash, find the index of the stash (n) that you want to pop

git stash pop stash@{n}

To drop a particular stash without keeping the changes

git stash drop n

Other commands related to stash

git stash list
git stash show
git stash clear

Delete a branch locally

git branch -d branch_name

Delete a remote branch

git push origin --delete branch_name
// origin stands for the remote name. Can be some other name also.

Merge another branch to master

git checkout branch_name 
git pull
git checkout master 
git pull 
git merge --no-ff --no-commit branch_name

Clone a git repo, without actually pulling anything

git clone --bare

Clone a single branch only

git clone some_git_url.git --single-branch --branch=your_branch_name

Add another branch to the single branch repo folder

git remote set-branches --add origin another_branch_name
git fetch
git checkout another_branch_name

See all the git commits in a tree view

git log --graph --oneline --all
// --all for showing all the branches

Merge changes without the commit history. This will stage all the files without the commit, so a custom commit message can be provided.

git merge --squash

Force merge current branch with another overriding it with the current branch

git merge --strategy=ours another_branch
// or the shorthand notation
git merge -s ours another_branch

See all unpushed commits

git log --branches --not --remotes
git log origin/master..HEAD // See what this does

See the commit history of a file

git log -p -- path/to/file

Set email in the git config at the global level

git config --global ""
git config --global

Setting email in repository level

git config ""

To have a folder level settings, add a .gitconfig file in the folder, and provide the settings. An example file would be:

    name = <your-user-name>
    email = <>

More GIT related posts can be found here.

Leave a Reply