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.
Initialise the existing directory as a git repository
git initClone an entire repository from url
git clone <url>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 commitConnect to a remote repo
git remote add origin <url>Check if the remote has been added correctly
git remote -vPush for the first time
git push -u origin master- Add files
- Make a commit
- Push to remote repo
git pushCreate 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 branchList all remote branches
git branch -rList all branches
git branch -aFetch changes (but don't change any of your local branches) to all local branches
git fetchFetch changes only to a specific local branch
git fetch origin <branch-name>Merge changes from the tracking remote branch into your current branch
git mergeMerge 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 pullShow 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>Reset the last commit and keep the staged changes
git reset --soft HEAD~1Reset the last commit and unstages the changes
git reset HEAD~1Deletes the last commit and all changes in it
git reset --hard HEAD~1If the commit was already pushed, you’ll need to force push after resetting (DANGEROUS!)
git push --forceSave all changes
git stashWrite the changes from top of stash stack
git stash popDiscard the changes from top of stash stack
git stash dropList stack-order of stashed file changes
git stash list