Skip to content

FronkTP/git-cheatsheet

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 

Repository files navigation

Git Cheatsheet

This is a repo where I put every git commands I've come across for future referencing. This is done so that it only includes the commands I use, not the complex commands I don't understand.

Get started

Initialise the existing directory as a git repository

  git init

Clone an entire repository from url

  git clone <url>

First few steps

Add a file

git add <file-name> <file-name>

Add all files

git add .

Make a commit

git commit -m 'message'

Make a commit (and open text editor to write message)

git commit

Connect to a remote repo

git remote add origin <url>

Check if the remote has been added correctly

git remote -v

Push for the first time

git push -u origin master

Making changes later on

  1. Add files
  2. Make a commit
  3. Push to remote repo
git push

Collaborate

Create a new local branch and switch to it

git checkout -b <new-branch-name>

Create a new local branch from a remote branch and switch to it

git checkout -b <new-branch-name> origin/<branch-name>

Switch to another branch

git checkout <branch-name>

List all local branches

git branch

List all remote branches

git branch -r

List all branches

git branch -a

Fetch changes (but don't change any of your local branches) to all local branches

git fetch

Fetch changes only to a specific local branch

git fetch origin <branch-name>

Merge changes from the tracking remote branch into your current branch

git merge

Merge from another local branch into your current branch

git merge <branch-name>

Merge from another remote branch into your current branch

git merge origin <branch-name>

Merge the changes from one local branch (branch2) into another local branch (branch1) without leaving your current branch (main).

git checkout <branch-name-1> && git merge <branch-name-2>

Fetch and merge changes from the tracking remote branch into your current branch

git pull

Show line-by-line changes between your current branch and another branch

git diff <another-branch-name>

Show the difference of what is in one branch (branch1) but not in another branch (branch2)

git diff <branch-name-2>...<branch-name-1>

Edit history

Reset the last commit and keep the staged changes

git reset --soft HEAD~1

Reset the last commit and unstages the changes

git reset HEAD~1

Deletes the last commit and all changes in it

git reset --hard HEAD~1

If the commit was already pushed, you’ll need to force push after resetting (DANGEROUS!)

git push --force

Temporary commits

Save all changes

git stash

Write the changes from top of stash stack

git stash pop

Discard the changes from top of stash stack

git stash drop

List stack-order of stashed file changes

git stash list

About

A repo that contains the necessary git commands.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published