Перейти к содержанию

Git Cheat Sheet

Basic Snapshotting

Command Description
git add Add file contents to the index. Use it after making any changes to the working tree, and before running the commit command.
git status Show the working tree status.
git diff Show changes between commits, commit and working tree, etc.
git commit Record changes to the repository.

Sharing and Updating Projects

Command Description
git pull git pull runs git fetchwith the given parameters andthen depending on configuration options or command line flags, will call either git rebase or git merge to reconcile diverging branches.

Inspection and Comparison

Command Description
git log README.md Shows the commit logs.

Debugging

Command Description
git blame README.md Show what revision and author last modified each line of a file.
Annotates each line in the given file with information from the revision which last modified the line.

Change username and e-mail

 git config --local user.name "Viacheslav Kolupaev"
 git config --local user.email my@email.com

Working with a branch

git checkout main  # Switch to the main branch.
git pull  # Fetch from and integrate with origin repository.
git checkout -b infra/pipeline-14  # Create a branch.
git branch -m infra/pipeline-14 infra/pipeline-15  # Rename a branch.

git status  # Which branch am I currently working on?

git commit -a -m 'Update mypy config' --dry-run  # Commit all changed files. Show what would be committed.
git commit -a -m 'Update mypy config'  # Commit.

git commit -i .gitignore -m 'Update .gitignore' --dry-run  # Commit specified file only. Show what would be committed.
git commit -i .gitignore -m 'Update .gitignore'  # Commit.

git push --set-upstream origin infra/pipeline-14  # For the first time.
git push  # In subsequent times.

Get the short Git version hash

git rev-parse --short HEAD  # cd7ec06

# or

git log -1 --pretty=format:%h  # cd7ec06