git commands

init

checkout

  • [branch name] : Switch to a branch
  • - : Switch to the branch last checked out
  • -- [file-name.txt] : Discard changes to a file
  • -- . : Discards changes in the working directory for all files and restores them to the state of the last commit.
  • -b [branch name] : Create a new branch and switch to it
  • -b [branch name] origin/[branch name] : Clone a remote branch and switch to it

config

  • credential.helper store : git will store these credentials in a plain-text file (typically located in the user’s home directory, such as ~/.git-credentials)
  • --global --list : Get global config
  • --global user.name "your_username" : Set globally Username
  • --global user.email "your_email_address@example.com" : Set globally Email id
  • --global http.proxy http://127.0.0.1:8889 : Set globally proxy fot clone,push…etc

remote

  • add origin ssh://git@github.com/[username]/[repository-name].git : Add a remote repository
  • set-url origin ssh://git@github.com/[username]/[repository-name].git : Set a repository’s origin branch to SSH

clone

  • --recurse-submodules[=<pathspec>] : go into each subdirectory that has a .git directory (a submodule) and pull any changes from the remote repository

status

add

  • [file-name] : Add a file to the staging area
  • -A: Add all new and changed files to the staging area

commit

  • -m "[commit message]" : Commit changes

rm

  • --cached : Use this option to unstage and remove paths only from the index. Working tree files, whether modified or not, will be left alone.
  • -r : Allow recursive removal when a leading directory name is given.

branch

  • -a : List all branches (the asterisk denotes the current branch)
  • [branch name] : Create a new branch
  • -d [branch name] : Delete a branch
  • -D [branch name] : Delete a branch forcefully
  • -m [old branch name] [new branch name] : Rename a local branch

push

  • origin [branch name] : Push a branch to your remote repository
  • -u origin [branch name] : Push changes to remote repository (and remember the branch)
  • origin --delete [branch name] : Delete a remote branch

pull

  • --dry-run : Show what would be done, without making any changes.

restore

  • --source=HEAD --staged --worktree -- . : equivalent to the git checkout -- . and discards changes in the working directory for all files.

merge

  • [branch name]: Merge a branch into the active branch
  • [source branch] [target branch] : Merge a branch into a target branch

stash

  • clear : Remove all stashed entries

reset

  • --hard origin/[branch-name] : Any changes to tracked files in the working tree since <commit> are discarded.

clean

  • -n, --dry-run : Don’t actually remove anything, just show what would be done.
  • -f, --force : it will refuse to delete files or directories unless given -f or -i.
  • -i, --interactive : Show what would be done and clean files interactively.
  • -X : Remove only files ignored by Git. This may be useful to rebuild everything from scratch, but keep manually created files.
  • -x : This allows removing all untracked files, including build products.

log

  • --summary : View changes (detailed)
  • --oneline : View changes (briefly)

diff

  • [source branch] [target branch] : Preview changes before merging

revert

  • commitid : Revert commit changes

trash

Soft alternative to git clean. Moves all unstaged files to the .trash directory for later review. First to have this command work you need to add .trash directory to the local or global .gitignore. If you don’t do that git trash will try to remove the .trash folder as well.

echo ".trash" >> .gitignore

Now add the trash git shortcut to your global aliases list:

git config --global alias.trash '!mkdir -p .trash && git ls-files --others --exclude-standard | xargs mv -f -t .trash'

And you’re done. Now you can run git trash in your root of git repository and all unstaged files will be moved to the .trash subdirectory.