Frequently Used Git Commands

Author posted by Jitendra on Posted on under category Categories Tech Tips and tagged as Tags with 1 Comment on Frequently Used Git Commands

Most frequently used Git commands by developers

Set Git to store credentials

Below command can be executed from anywhere in your system.

git config --global credential.helper wincred

Turn off Warning _LF will be replaced by CRLF_

git config core.autocrlf true
git config --global core.autocrlf true

In Unix systems the end of a line is represented with a line feed (LF). In windows a line is represented with a carriage return (CR) and a line feed (LF) thus (CRLF). when you get code from git that was uploaded from a unix system they will only have an LF.

Mark local folder as git folder

git init

Add / Link remote repository to local repository

git remote add origin

Add new / multiple remote repository

git remote set-url origin

 Clone complete repository

Git clone
Example : Git clone

Clone git repository with branch

Git clone -b
Example : Git clone -b Dev1

Git fast forward push

git push --force

Ignore a file temporarily or Remove a file from staged area or commit everything except one file

Run below command before commit, to ignore file

git reset -- "FileName"

Run below command before commit, to ignore file in folder

git reset -- "Folder/FileName"

Run below command before commit, to ignore every file in folder

git reset -- "Folder/*"

Ignore every change in local git repository

Run below all 3 commands in sequence

git reset --hard
git reset --hard Example : git reset --hard origin
git pull

Rollback last Git command

git lastcommand --revert

Example if we want to revert last pull command then run below command

git pull --revert

Change or Switch Branch

Git checkout

Rename Branch

git branch -m new-name

If you are on different branch

git branch -m old-name new-name

Once Branch renamed, push branch, it will create new and then run delete command for branch

Delete remote branch

git push origin --delete

Current code base points to which branch

git branch

starred branch would be current branch

Create a branch and switch to new branch

git checkout -b CPQAdmin

CPQAdmin is new branch name

Push branch to remote

git push origin CPQAdmin

CPQAdmin is branch name to be pushed

Status of modified or added files

git status

Add all local changed files for commit

git add .

Delete local deleted file from remote also

git add -u .

Commit with message

git commit -m "My custom message"

Create gitignore file

touch .gitignore

above command will create gitignore file

Create tag

git tag -a v0.1 -m "Information about Tag"

Push local tag to remote

git push --tags

clear git cache

Sometimes  gitignore  file does not work. So, we need to clear the cache using below command

git rm -r --cached .

Get List of files changed in last 24 hours

Below commands should be executed in  local git folder 

git pull origin git log --pretty=format: --name-only --since="24 hours" | sort | uniq > OutputFileName.txt

After running above command, new file with name OutputFileName.txt  would be created which will have list of all components changed in last 24 (n) hours.

Save Password in Git Store

If you are not using SSH keys then you might end up providing password every time. We can run below command to let Git store your password so you don’t have to remember it

git config credential.helper store

After above command, execute pull request

git pull

List of file changed containing word in commit comments

Below command will return unique file names by searching words in all commits messages in branch

git log remoteName/branchName --grep "word1InCommit\|word2\|word3" --pretty=format: --name-only | grep -v -e "^$" | sort | uniq


git log origin/UAT --grep "profiles changed\|FLS modified\|Apex Class for callout" --pretty=format: --name-only | grep -v -e "^$" | sort | uniq

Remove Remote URL from Git

Use below command to remove remote URL from git repository.

git remote rm <remote name>
Example : git remote rm origin

Keep file locally but delete from remote

git rm --cached somefile.ext
git rm --cached somefolder

Related posts

One thought on “Frequently Used Git Commands”

Leave a Reply

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