CODINGTHOUGHTS

A blog about C#, Python, Azure and full stack development

Git Crib Sheet

Welcome to my Git crib sheet: A collection of handy git commands in one place.

Creating a repo

git init
git add .

Checking repo details

git status
git branch -l                                   # list all branches
git branch -lr                                  # list all branches with remote
git branch -a                                   # list all branches with remote (same as -lr)
git remote -v                                   # list all remote repositories

Viewing commit history

git log                                         # list all commits
git log --stat                                  # list all commits with a diffstat (details of insertions and deletions to files)
git log --oneline                               # list all commits in a compact format, one line per commit

Committing changes

git commit -a                                   # using the -a switch adds any untracked changes before committing
git commit -m "Fixed buffer overflow bug"       # the -m switch lets you provide a message
git commit -am "Fixed buffer overflow bug"      # you may combine the -a and -m switches

Pulling / Pushing

git pull                                        # pull changes from remote repository
git push                                        # push commited changes to remote repository

Branching / Merging

git branch --list                               # lists all local branches
git branch --list --remotes                     # lists all remote branches
git branch <branchname>                         # create a new branch with name <branchname>
git checkout <branchname>                       # switches to an existing branch
git branch -m <oldbranchname> <newbranchname>   # renames a branch
git branch -c <oldbranchname> <newbranchname>   # copies a branch
git branch -d <branchname>                      # deletes a branch

Merging

git checkout main
git merge <branchname>                          # merges a branch into the current branch

Tags

git tag                                         # list all tags
git tag <name>                                  # creates a new tag with name <name>
git tag -a <name> -m "message text"             # creates an annotated tag, includes additional metadata such as user name and date
git tag -a <name> <commit hash>                 # creates a tag on an old commit

Stashing

git stash                                       # saves all changes to a temporary stash if not ready to commit
git stash show                                  # shows stash contents
git stash pop                                   # restores your stashed changes

Posted

in

by

Tags:

Comments

Leave a Reply