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.

How to show the current branch of your git application ?

git branch --show-current

How to set the Visual studio code as your default editor in Git ?

git config --global core.editor "code --wait"

How to set the notepad as your default editor in Git ?

git config --global core.editor "notepad"

Steps to be followed for successful git rebase from master

What is Rebase? Basically merging all the master changes to your feature branch and it is always advicable to make sure that we work on top of the everyone’s changes and its required to be performed multiple times a day at worst case atleast once a day.

  1. Clone your git project (git clone <your-project-repo>)
  2. Checkout your feature branch (or the branch you want to perform rebase from master) (git checkout -b <your-feature-branch>)
  3. Rebasing from master (git rebase origin/master)
  4. Now two things could have happened
    4.1 once all the master changes are pulled to your local feature branch.
    4.2 All the conflicts are created with Head and your branch additional lines with =======
  5. Changes about 4.1 we dont want to worry. But 4.2 we need to open that file in some editor, I prefer to do this in visual studio code. (Refer the above to set the visual studio code as your default editor)
  6. Resolve the conflicts, meaning which change you want, few may be from master and few may be from your feature branch, once you are good with this step, then.
  7. Add all this files to staging (git add .)
  8. And again continue to run the rebase (git rebase –continue)
  9. Resolution for “hint: waiting for the editor to close the file” – close the file in your visual studio editor or any other editor in your machine with some commit message and continue to perform the rebase.
  10. Finally do git pull or git push –force origin <your-feature-branch> [I prefer to do git push –force, but you need to make sure that no one else is committed something to your feature branch in between, in that case it may lose, so do with some caution]

Leave a Reply