Basic Git commands

Basic Git commands

Here is a list of some useful git commands.

Initialize empty Git repository

git init

Stage a file

git add filepath

Stage all files

git add .

Commit file changes

git commit -m "commit message"

Check for staged and unstaged files

git status

Push committed changes to master branch

git push

Push committed changes to a particular branch

git push origin branchname

Force push committed change

git push -f

Create a new branch

git checkout -b branchname

Switch to an existing branch

git checkout branchname

Delete a branch locally

(first ensure that you are not on the branch you intend to delete)

to delete branch only if it has already been pushed and merged with the remote branch

git branch -d localBranchName

delete the branch only if you want to force the branch to be deleted, even if it hasn't been pushed or merged yet

git branch -D localBranchName

Delete a branch remotely

(first ensure that you are not on the branch you intend to delete)

git push origin --delete remoteBranchName

Update current branch with changes from a remote repository

git pull

Clone a repository

git clone <url>

Manage connections to remote repositories

show URLs of remote repositories

git remote -v

create a new connection to a remote repository

git remote add <shortname> <url>

disconnect the remote from the local repository

git remote remove <name>

rename a remote connection

git remote rename <old-name> <new-name>

Fix last commit message

git commit --amend -m "correct commit message"