GIT Bash frequently used commands- Cheat sheet

GIT cheat sheet

Below are some of the common git bash commands, the most frequently used ones are in italic fonts

Scratch- create a new local repository – $ git init {project_name}

Download from existing repo – $ git clone my_appliction_url

List new or modified files not committed – $ git status

show the changes to files not staged/committed – $ git diff

Show all the staged and unstaged file changes – $ git diff HEAD

Show the changes between two commit ids – $ git commit1 commit2

List the change dates and authors for a file – $ git blame {file}

Show the file changes for a commit id and file – $ sit show [commitId]:[file]

Show full change history – $ git log

Show the change history for file including diffs – $ git log -p [file]

List all local branches – $ git branch

list all branches,local and remote – $ git branch -av

switch from one branch to another/update working directory – $ git checkout new_branch

Delete branch – $ git branch -d branch_name

Merge branch_1 into branch_2 – $ git checkout branch_2

$ git merge branch_1

Tag the current commit – $ git tag my_tag

Stages the files ready for commit – $ git add {file}

Stages all the changed files for commit – $ git add .

Commit the staged files – $ git commit -m “commit message”

unstages files, keeping the file changes – $ git reset {file}

revert to the last commit – $ git reset –hard

get the latest changes from origin (without merging) – $ git fetch

fetch the latest changes from origin and merge – $ git pull

Fetch the latest changes from origin and rebase – $ git pull –rebase

push local changes to the origin – $ git push

How to get new branch from your upstream:

  • git clone our_forked_repository
  • git remote add upstream upstream_git_url
  • git fetch upstream
  • git checkout -b new_branch_name upstream/new_branch_name
  • git push origin

still getting any error? then remove your branch in git which does not exist in your local.

How to change your remote upstream/origin Url:

git remote -v

This command will list down all the remote urls (both origin and upstream urls).

How to set origin URL ?

git remote set-url origin your_actual_git_url

This command will set the url you mentioned as your origin url for your forked repository.

How to set upstream url ?

git remote set-url upstream your_actual_git_url

This command will set the url you mentioned as your upstream url for your forked repository.

How to Revert your Changes to last commit ?

git reset –hard  ( to revert to head)

to reset to particular commit then,

git reset –hard <b748323> (commit id)

git push origin YOUR_BRANCH_NAME ( to change or revert to head in the repo as well)

add ( git push origin YOUR_BRANCH_NAME -f ) if it is throwing any warning or exception to update or push forcefully.

How to clear git credentials in Windows ?

git config --global --unset credential.helper

Another way is to Remove your credentials from Credential Manager in windows,

Then select your credentials and remove.

 1,435 total views,  5 views today

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.