Skip to content

Useful Git commands

shamik edited this page Apr 25, 2024 · 11 revisions

Git Commands

Setting up default editor

git config --global core.editor "vim"

or

export GIT_EDITOR=vim

Configuring global config

git config --global user.email "shamik@jamba.com" 
git config --global user.name "Shamik" 
git config –l

Git Credential Manager(GCM) for Linux

Installation

Setting up GCM cache for authentication

https://github.com/GitCredentialManager/git-credential-manager/blob/main/docs/credstores.md#gits-built-in-credential-cache

Checkout to a specific PR, in this example PR=42

git fetch origin pull/42/head:pr-42 switch to the pr git checkout pr-42 and then install it to check the code by pip install -e .

Ignoring git LFS files while cloning

https://stackoverflow.com/questions/42019529/how-to-clone-pull-a-git-repository-ignoring-lfs

GIT_LFS_SKIP_SMUDGE=1 git clone SERVER-REPOSITORY

Cloning into a specific dir

git clone https://github.com/username/repository.git /path/to/directory

Cloning a specific branch

git clone -b <branchname> <remote-repo-url>

Tagging a release/creating an experiment identifier in git 

git tag -a <any name/version number> -m <any message of choice> 
git tag -a sgd-classifier -m "SGDClassifier with accuracy 67.06%"

Pushing tags after creating them. Tags aren't pushed with regular commits so they need to be pushed separately

git push origin [branch name] --tags

Viewing a list of tags 

git tag

Deleting a tag locally

git tag -d <tag-name>

Deleting a tag in remote

git tag --delete origin <tag-name>

If the tag and branch name is the same then: git push origin :refs/tags/<tag-name>

Checking how many commits one branch is behind another

git rev-list --left-right --count <branch to compare to>...<branch to be compared> 
# git rev-list --left-right --count nlp_amplify_master...nlp_amplify_aue131
# Output 
# 3 6 
# Meaning nlp_amplify_aue131 is 3 commits behind  nlp_amplify_master and 6 commits ahead of it

Saving changes to a stash(saves all changed files)

git stash

Stashing specific changes to the stash

git stash push -- <file path> <file path>
# git stash push -- common/anlpModels/ESG/ESGModel.py ds/experiments/experiment_process/simulate_gradual_training.py

Checking all the stash

git stash list

Restoring from a stash

git stash apply `stashnumber`  
# git stash apply stash@{0}

Deleting a particular stash

git stash drop stash@{2}

Deleting all stashes

git stash clear

Saving stashes and uploading them to remote

git stash show <stash> -p > <filepath>
# git stash show stash@{0} -p > ds/patch/possiblepatch

Pushing changes to remote if it doesn't exist in remote

git push -u origin [branch name]

Renaming a branch

git branch -m <old-name> <new-name>

Deleting a branch in the origin

git push origin -d <old-name>

Deleting a branch locally

git branch -d <branch-name>

Deleting commits or moving back changes to a prior commit without reverting the changes

This deletes the entire commit history and all the changes associated to each commit. 

git reset --hard <commit hash>
# or
git reset --hard 
# the above resets the branch to the latest commit

If removing commit history is required but by keeping the changes in a staged area

git reset --soft <commit hash>

Creating a remote repo from cline for the first time

First create the repo on github with the same repo name

git remote add origin https://github.com/shamik/<reponame>.git 
git push –u origin master

Setting the remote branch as upstream

git push --set-upstream origin master

Merge error due to unrelated histories

"fatal: refusing to merge unrelated histories" git merge <branch> --allow-unrelated-histories

To stop tracking an already tracked directory

git rm –r –cached <dir> 
git commit –m "some message"

Getting remote branch url

git remote –v 
# or
git remote get-url <branch name, e.g. origin>

Find a branch which contains specific commit

git branch –a --contains 55bc284d49b2387865baf06573c8b04584bf4b03

Emptying the current git directory and removing all untracked files recursively across all directories

  • Perform a dry run first, -d is for removing directories git clean –dn
  • Then clean all the files git clean
  • For a specific path git clean –dn <path>

Showing the latest commit and the commit message

git log –1 
git show -s

Showing all the files that are different in a commit, --name-only can be replaced by –name-status to show the status of the different files

git show --pretty="" --name-only <sha1-commit-hash>

Logging the short commit hash, the author name, the time of the commit, and the commit message

git log --pretty=format:"%h - %an, %ar : %s"

Changing a commit message, if it's the last commit

git commit --amend

Checking the diff for a file between two different commits

git diff <older commit hash> <newer commit hash> -- <path to the file> 
# git diff 355dc53 e92f514 -- feextraction/nlp/processors/granular_concept_extractor.py

Checking all the commits to a particular file

git log -p --follow -- <filename>

Git LFS

git lfs install 
git lfs clone 
git lfs pull

Git error: Encountered 7 file(s) that should have been pointers, but weren't

https://stackoverflow.com/questions/46704572/git-error-encountered-7-files-that-should-have-been-pointers-but-werent

git rm --cached -r . 
git reset --hard 
git rm .gitattributes 
git reset . 
git checkout .

Clone this wiki locally