GIT CHEAT SHEET

Git Cheat Sheet

March 30, 2021 . Mostafa Lotfy

On This Page

Anything between < > is a variable part of the command. It changes according to the naming of your files and branches.

Configure


 $ git --version         # Check if git is installed on your system and check its version.


 $ open ~/.gitconfig     # Open `.gitconfig`--a hidden file at the home directory--and edit username and email.


 $ open .git/HEAD       # Rename a branch by switching to it,  open the `HEAD` file in the `.git` hidden folder, and edit the line `ref/heads/master`. Change the last part of the line, which is the current branch's name.

Working Locally


 $ git init              # Turn any local folder into a git repo.


 $ git status            # Check untracked, modified, or added files.


 $ git log               # list of all commits.


 $ git add <file>        # Stage a file to make it ready to be commited.


 $ git add <file1> <file2>       # Stage multiple files to commit them together.


 $ git add .             # `.` or `*` to stage all modified and untracked files.


 $ git commit -m "commit message" <file>             # Commit a specific file from the staged files. And add a commit message.


 $ git commit -m "commit message" <file1> <file2>    # Commit mutliple files.


 $ git commit -m "write a commit message"            # Commit all stagged files at once and add a commit message.


$ git commit --amend -m "new commit message"		# Use this to overwrite your very last commit with a new one when you make a mistake. Not jsut a mistake with the message you can add new files you forgot to add. Don't use this on published commits, unless you work on a private repo.

Working with Remotes


 $ git clone <url>       # Clone a repository to a local folder from a url. Example: `$ git clone https://github.com/mstflotfy/mstflotfy.github.io`


 $ git remote            # Get a list of all the names of remote repos you've specified.


 $ git remote --v        # Get a list of all remotes with their URLs.


 $ git remote add <origin> <url>     # Add a new remote repository.


 $ git push <origin> <branch>        # Push new commits from local branch to a remote repo. Example: `$ git push origin master`.


 $ git push -u <origin> <branch>     # Push commits and set a specific remote as the default remote to push and pull from.


 $ git push      # Push to the default remote.


 $ git push -f <origin> <branch>   # Force push commits. Changes made to the remote will not be saved. Will create issues if other pushing repos are active.  


 $ git fetch     # Fetch changes from the default remote without committing them. Can use `$ git diff` afterward to look at changes.


 $ git pull      # Pull commits from default remote to the local branch and commit them.


 $ git remote show <origin>      # Show more info about a specific remote.


 $ git remote remove <origin>    # Remove a remote by deleting the reference to it.

Wroking with branches


 $ git branch            # Show all local branches.


 $ git branch --all      # Show branches, including remotes.


 $ git branch <new-branch-name>  # Create a new branch. Example: `$ git branch testBranch`


 $ git switch <branch-name>      # Switch to a specific branch. Example: `$ git switch main`


 $ git diff <branch1> <branch2>       # Show the difference between two branches. Example: `$ git diff master origin`


 $ git merge <branch>        # Merge branches. Merge changes from named branch into the current branch.


 $ git branch -d <branch>    # Delete a branch.


 $ git checkout <commitHash>         # Cheackout an old commit in a headless branch. Then git will walk you through what to do.

Handling conflicts

Help & Docs

Official Git Documentation

 $ git --help <verb>     # Docs for a specific command. Example: `$ git --help add`


 $ git <verb> -h         # Get a short list of some options for a command.

Other Cheat Sheets

Previous: git & GitHub Tutorial | Next: Markdown (This post is coming soon)