Skip to content

Instantly share code, notes, and snippets.

@baspiotr
Forked from prograhammer/git-cheat-sheet.md
Created January 6, 2020 19:04
Show Gist options
  • Select an option

  • Save baspiotr/5b88c831d7cbc45def82b025550da95a to your computer and use it in GitHub Desktop.

Select an option

Save baspiotr/5b88c831d7cbc45def82b025550da95a to your computer and use it in GitHub Desktop.
Git cheat sheet for some useful Git commands and command scenarios.

Git Cheat Sheet

Single Git Commands

Initialize a git repo in the current directory

git init

Add a remote called "origin"

git remote add origin https://github.com/your-username-here/your-repo-here.git

Add all untracked changed files to staging, ready to be committed

git add -A

Commit with a message

git commit -m "some message here"

Push from local branch "master" up to remote branch "origin"

git push origin master

Pull from remote branch "origin" down to local branch "master"

git pull origin master

Force push "master" branch to remote "origin" forcing the remote to accept your changes

git push --force origin master

Removes untracked files (not those ignored). Such as those .orig files that are left after resolving conflicts

git clean -f -n    // do this command first, with "n" flag to see what would be removed
git clean -f       // this command actually removes the files

View remotes

git remote -v

View current branch you are on

git branch -l

Switch to a branch

git checkout your-branch-here

Rebase the current branch off of some other branch (most of the time "some-other-branch" will be "master")

git branch -l                  // shows you are currently on some-branch
git rebase some-other-branch   // now rebase off of some-other-branch

Common Git Scenarios

Push some code changes up to github repo

git init
git remote add origin https://github.com/your-username/your-repo.git
git add -A
git commit -m "updated my package"
git push --force origin master

Setup Git to use SSH key (perhaps for Bitbucket or Github)

# go to ssh directory (or create it if it doesn't exist)
cd ~/.ssh

# Generate SSH key
ssh-keygen -t rsa -C "enter some random label here"

# Prompt "Enter file in which to save the key (/root/.ssh/somekey):"
[Enter your key name. In this case we call it "somekey"]

# Prompt "Enter passphrase (empty for no passphrase):"
[Enter a passphrase]

# See your public ssh key
cat ~/.ssh/somekey.pub

# somekey.pub is your public key
# somekey is your private key

# Enter this command to see if the agent is running
ps -e  | grep [s]sh-agent
[9060 ??         0:00.28 /usr/bin/ssh-agent -l]

# If the agent isn't running, start it manually with the following command:
ssh-agent /bin/bash

# Load your new identity into the ssh-agent management program using the ssh-add command.
ssh-add ~/.ssh/somekey
[Enter passphrase for /root/.ssh/id_rsa: 
Identity added: /root/.ssh/somekey]

# Use the ssh-add command to list the keys that the agent is managing.
ssh-add -l
[2048 7a:9c:b2:9c:8e:4e:f4:af:de:70:77:b9:52:fd:44:97 /root/.ssh/somekey (RSA)]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment