Programming

Useful Git commands

Sponsored
Sponsored
Sponsored
Sponsored

Git is a free and open source distributed version control system designed to handle everything from small to very large projects with speed and efficiency.

Its goals include speed, data integrity, and support for distributed, non-linear workflows (thousands of parallel branches running on different systems).

Git was created by Linus Torvalds in 2005 for development of the Linux kernel, with other kernel developers contributing to its initial development. Since 2005, Junio Hamano has been the core maintainer. As with most other distributed version-control systems, and unlike most client–server systems, every directory on every computer is a full-fledged repository with complete history and full version-tracking abilities, independent of network access or a central server. Software is distributed under GNU General Public License Version 2.

Create with Git

# Clone an existing repository

git clone ssh://user@domain.tld/repo.git

# Clone a unique branch from an existing repository

git clone -b <branchname> --single-branch ssh://user@domain.tld/repo.git

# Clone an existing repository and all its sub-modules recursively

git clone --recurse-submodules ssh://user@domain.tld/repo.git

# Create a new local repository

git init

Git Configuration

# Set the name attached to all your commits

git config [--global] user.name <name>

# Set the email attached to all your commits

git config [--global] user.email <email>

# Set colorization of command line output for all repos

git config --global color.ui auto

# Print set name (in current repository or globally)

git config [--global] user.name

# Print set email (in current repository or globally)

git config [--global] user.email

Local Changes

# List changed files in your working directory

git status

# List changes to tracked files

git diff

# Add all current changes in file to the next commit

git add <file>

# Add all current changes to the next commit

git add .

# Add changes to the next commit interactively

git add -p <file>

# Rename file and add it to next commit

git mv <file> <new file name>

# Delete file and add its deletion to next commit

git rm <file>

# Commit all local changes in tracked files

git commit -a

# Commit previously staged changes

git commit

# Change the last commit

git commit --amend

# Note: You shouldn't amend published commits!

Commit History

# Show all commits

git log

# Show changes over time for a specific file

git log -p <file>

# Show changes over time for a specific committer

git log --author=<committer name>

# Note: <committer name> is a pattern, so Ed will match Edward Smith. Quotes are optional if the pattern doesn't contain spaces.

# Search (grep) commit messages for the given string

git log --grep=<string>

# Who changed what and when in file

git blame <file>

# Store changes temporarily

git stash

# Remove and apply stashed changes

git stash pop

# Remove file from all previous commits but keep it locally

git rm --cached <file>

Branches & Tags

# List all existing branches

git branch

# Switch HEAD branch

git checkout <branch>

# Create a new branch based on your current HEAD

git branch <new-branch>

# Create a new tracking branch based on a remote branch

git branch --track <new-branch> <remote-branch>

# Delete a local branch

git branch -d <branch>

# Delete a remote branch

git push origin --delete <branch>

# Rename a branch locally

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

# Rename a branch on remote

git push <remote> :<old name>
git push <remote> <new name>

# Tag the current commit

git tag <tag-name>

Update & Publish

# List all currently configured remotes

git remote -v

# Show information about a remote

git remote show <remote>

# Add new remote repository

git remote add <remote> <url>

# Rename a remote

git remote rename <old-name> <new-name>

# Download all changes from remote, but don't merge into HEAD

git fetch <remote>

# Download all changes from remote, but don't merge into HEAD and clean up deleted branches from origin

git fetch -p <remote>

# Download changes and directly merge into HEAD

git pull <remote> <branch>

# Publish local changes on a remote

git push <remote> <branch>

# Track a remote repository

git remote add --track <remote-branch> <remote> <url>

# Publish your tags

git push --tags

Merge & Rebase

# Merge branch into your current HEAD

git merge <branch>

# Rebase your current HEAD onto branch

git rebase <branch>

# Note: You shouldn't rebase published commits!

# Abort a rebase

git rebase --abort

# Continue a rebase after resolving conflicts

git rebase --continue

# Resolve conflicts using your configured merge tool

git mergetool

# Manually resolve conflicts using your editor and mark file as resolved

git add <resolved-file>
git rm <resolved-file>

Undo

# Discard all local changes in your working directory

git reset --hard HEAD

# Discard local changes in a specific file

git checkout HEAD <file>

# Revert a commit by providing a new commit with contrary changes

git revert <commit>

# Restore a specific file from a previous commit

git checkout <commit> <file>

# Reset your HEAD pointer to a previous commit

## Discarding local changes:

git reset --hard <commit>

## Preserving all changes as unstaged changes:

git reset <commit>

## Preserving uncommitted local changes:

git reset --keep <commit>

Maybe conda commands also will be useful.

Share
Published by
Serhii Kupriienko

Recent Posts

Як стати  Software Engineer: моя історія

Для кого ця стаття? Для таких як я сам, хто в часи пандемії та суттєвої…

3 роки ago

Useful ASCII tables

ASCII Tables ASCII abbreviated from American Standard Code for Information Interchange, is a character encoding standard for electronic communication.…

3 роки ago

Conda cheatsheet

Conda Managing Conda and Anaconda, Environments, Python, Configuration, Packages. Removing Packages or Environments Читати далі…

3 роки ago

TOP-10 questions about Python

Читати далі TOP-10 questions about Python

3 роки ago

TOP-10 questions about Django

Читати далі TOP-10 questions about Django

3 роки ago

Find the sum of the digits in the number 100! (Python)

Python code to demonstrate a method to compute the factorial digists sum, i.e. 100 factorial (100!). This test exercise I've recieved when I was looking…

3 роки ago

This website uses cookies.