Frequently Used Git Commands

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
or
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 https://github.com/JitendraZaa/sfdx-object-export

Add new / multiple remote repository

git remote set-url origin  https://github.com/JitendraZaa/sfdx-object-export

 Clone complete repository

Git clone
Example : Git clone https://github.jitendraZaa.com/CPQ/cpq-salesforce.git

Clone git repository with branch

Git clone -b
Example : Git clone -b Dev1 https://github.jitendraZaa.com/CPQ/Salesforce-Backup.git

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

Example

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
or
git rm --cached somefolder

Posted

in

by

Tags:


Related Posts

Comments

One response to “Frequently Used Git Commands”

  1. Ashok Yadav N Avatar

    One Stop having good Set of Commands to look for instead of memorizing each time when the GIT task starts. Awesome job in posting them!!!!

    Thanks
    Ashok

Leave a Reply

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

Discover more from Jitendra Zaa

Subscribe now to keep reading and get access to the full archive.

Continue Reading