Skip to content

Useful Git commands

shamik edited this page Nov 7, 2024 · 8 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 "[email protected]" 
git config --global user.name "Shamik" 
git config --global push.autoSetupRemote true
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 .

Git Submodules

When one needs to add another repo to the existing repo without copy pasting code.

Adding submodule

Navigate to the repo where you want to add the submodule and clone the repo that you want to add as a submodule by executing:

git submodule add <remote url> <optional: folder name of your choice>
git pull --recurse-submodules
# e.g. git submodule add personal-github:Shamik-07/nn-zero-to-hero.git
# if the folder name isn't given then a folder is created automatically as per the repo

Once added, commit and push the changes to the remote.

Initialise submodule

To setup the necessary configurations for the submodule, navigate inside the submodule directory and execute:

git submodule init

Updating a submodule

To fetch the updates of the submodule, navigate inside the submodule directory and execute:

git submodule update

Making changes to the submodule

If one makes changes to the files in the submodules, then one needs to add, commit and push the changes to the remote.

Git Worktrees

https://git-scm.com/docs/git-worktree

Creating a worktree

git worktree add <path> <branch>
  • <path>: The directory where the new worktree will be created.
  • <branch>: The branch you want to check out in the new worktree. For example, to create a worktree for a new feature branch:
git worktree add ../feature-x feature-x

Listing Worktrees

To see all the worktrees associated with your repository, use:

git worktree list

Switching Between Worktrees

Navigate to the directory of the worktree you want to switch to:

cd ../feature-x

Removing a Worktree

When you’re done with a worktree, you can remove it with:

git worktree remove <path>

For example:

git worktree remove ../feature-x

Signing Commits

Check out this to set up GPG for signing commits

https://docs.github.com/en/authentication/managing-commit-signature-verification

Clone this wiki locally