How to clone remote git repo?

# basic command sytax
git clone <remote-url> <project-alias>

# usage of command
git clone git@github.com:phpcodebooster/git-tutorial.git local-folder-name

How to initialize local git repository?

# initialize new git repo locally
git init

How to add/update remote git url?

# To add a new remote repository to initialized local repo
$ git remote add origin https://github.com/user/repo.git

# to verify the remote git repo
$ git remote -v

# update existing url with new url
$ git remote set-url origin https://github.com/user/repo.git

How to check git file status?

# To check status of files
$ git status

How to stage/add files to git?

# Stage/Add file changes to git
$ git add <file-name>

# Undo/Unstage file changes
$ git reset <file-name>

How to update local repo with remote changes in git?

# Update local with remote changes
# bring new refs/branches to local
$ git fetch

# Get all recent changes from remote
# And merge any changes required
$ git pull

How to update remote git repo with local changes?

# Push all new recent changes to remote
$ git push [alias] [branch]

# example
$ git push origin master

How to delete remote git file?

# Deletes the file from the working 
# directory and stages the deletion
$ git rm [file]

# Removes the file from version 
# control but preserves the file locally
$ git rm --cached [file]

# Commit file changes after file has been staged
$ git commit -m "[descriptive message]"

How to rename a git file?

# Changes the file name and prepares it for commit
$ git mv [file-original] [file-renamed]

How to check history in git?

# show all recent logs
$ git log

# show last two logs
$ git log -p -2

# format logs in nice one line
$ git log --pretty=oneline

# show logs with custom format
$ git log --pretty=format:"%h - %an, %ar : %s"

# show logs by author
$ git log --pretty="%h - %s" --author=sandip

# show changes from specific commit
$ git show [commit]

How to undo git changes for a commit?

# Undo all changes for given commit
# However have them locally
$ git reset [commit]

How to discard git history to specifc commit?

# Discards all history and changes back to the specified commit
$ git reset --hard [commit]

How to stash changes in git?

# Store all the modifications to file
# Temporarily with git stash
$ git stash

# Shows all stashed changes by index
$ git stash list

# Apply most recent stashed changes
$ git stash pop

# Discard recently applied stashed changes
$ git stash drop

How to checkout a git branch?

# List all local branches
$ git branch

# list all remote branches
$ git branch -r

# find specific remote branch
$ git remote -r | grep "some keyword"

# Create a new local branch
$ git branch 

# Switch/Checkout existing branch
$ git checkout 

# Create a new branch and switch to 
# That newly created branch
$ git checkout -b 

# Delete a specified branch
$ git branch -d ​

How to merge a branch in git?

# To merge hotfix branch to master
# First switch to master branch
# And then merge hotfix branch to master
$ git checkout master
$ git merge hotfix

How to configure git email and user name?

# Update your user name for given git repo
$ git config --global user.name "Sandip Patel"

# Update your user email for given git repo
$ git config --global user.email "sandip@demo.com"

Related Posts