Back to blog
May 18, 2024
4 min read

Git, cheet sheet

How to do something in git, helpers

Repository

How to know current repository

git remote -v

How to set remote repository (github)

git remote add origin [email protected]:[username]/[repository-name].git

How to update remote repository

git remote set-url <remote_name> <remote_url>

// example
git remote set-url origin [email protected]:user/repository.git

Branches

Delete a git branch both locally and remotely

// delete branch locally
git branch -d localBranchName

// delete branch remotely
git push origin --delete remoteBranchName

Commits

Reset last commit —soft HEAD~1

https://www.git-tower.com/learn/git/faq/undo-last-commit/

git reset --soft HEAD~1

Note the —soft flag: this makes sure that the changes in undone revisions are preserved. After running the command, you’ll find the changes as uncommitted local modifications in your working copy.

Update name & email in a last commit

git commit --amend --author="username <[email protected]>"

If you lost your changes

Try to find your HEAD with:

git reflog

git only garbage collects after about a month or so unless you explicitly tell it to remove newer blobs.

https://stackoverflow.com/questions/5473/how-can-i-undo-git-reset-hard-head1

Rebase & Squash

  1. See git log, for example:

    git log --graph --oneline
    

    or

    git log -5
    
  2. Choose a needed commit

    git rebase -i [commit id]
    
  3. Edit:

  • first commit pick ...
  • second commit s or squash
  • next commit s or squash
  • etc.
  1. Rename final commit message and save
  2. git push --force if you already pushed commits

GIT configuration

Global Configuration

This command lists the global configuration settings. These settings are specific to the user and are stored in the user’s home directory (typically in a file located at ~/.gitconfig).

git config --list --global

or

cat ~/.gitconfig

Global settings apply to all repositories for the current user.

Local Configuration

This command lists the local configuration settings for the current repository. These settings are stored in the .git/config file within the repository’s directory.

git config --list --local

or

git cat .git/config

Local settings override global settings for that particular repository.

Default Configuration

This command lists the effective configuration settings by combining settings from all available levels: local, global, and system.

git config --list

If a setting is specified in multiple levels, the local level takes precedence over the global level, which in turn takes precedence over the system level.

Check / Update user name / email

Global

// Check
git config --global user.name
git config --global user.email 

// Update
git config --global user.name "yourusername"
git config --global user.email "[email protected]"

Local

// Check 
git config user.name
git config user.email

// Update
git config user.name "yourusername"
git config user.email "[email protected]"

Aliases

For example, global, add aliases to ~/.gitconfig file:

[alias]
        st = status
        co = checkout
        br = branch
        logg = log --graph --oneline
        rup = remote update --prune
        setanotheruserconfig = "!git config user.name \"Your Local Name\" && git config user.email \"[email protected]\""

Fix the warning: LF will be replaced by CRLF in | Windows | VSCode terminal

git config --global core.autocrlf false

How to add a new SSH keys

Don’t forget regiser your new key ;)

ssh-add c:/Users/YOU/.ssh/id_ed25519

or

eval $(ssh-agent -s)
ssh-add <directory to private SSH key>

How to rename a branch

git branch -m old-name new-name

How to rollback to a commit

! Make sure you have a backup branch with commits you want to remove

  1. Check the history of the dev branch: You can see the commit history to identify the commit you want to rollback to.
git log
  1. Use the git reset command to rollback to the desired commit. This command can work in different modes, but for removing the commits entirely, use the —hard option.
git reset --hard <commit-hash>
  1. Force push to update the remote branch:
git push origin <branch-name> --force