Git Commands

Avinash Kethireddy
4 min readJun 7, 2021

What is Git ?

Git is software for tracking changes in any set of files, usually used for coordinating work among programmers collaboratively developing source code during software development. Its goals include speed, data integrity, and support for distributed, non-linear workflows.

You can download git here.

Basic Commands:

git --version
git config --global user.name "<user-name>"
git config --global user.email "<user-email-ID>"
git config --global --list
git config --local --list
git help <verb>
git <verb> --help
git init # For initialising a repository with git
rm -rf .git # To stop tracking
touch .gitignore # put all unwanted files which do not want to git

Save changes and pushing:

git status
git add -A # Adds files to staging area
git commit -m "Message"
git pull origin master # If remote is set
git push origin master

Create a new git repo with existing code :

git init           # In the folder. Also create a new repo in GitHub
git add -A
git commit -m "message"
git remote add origin <link_to_repo>
git push -u origin master

Branches :

To Create branch -git branch                 # To check the current branch
git branch my_branch # To initialise a branch
git checkout my_branch # To change the branch
git checkout -b my_branch # Single command for above two operations
Merging Branches -git checkout master
git pull origin master
git branch --merged
git merge my_branch
git push origin master
Deleting a Branch - git branch --merged # To check if everything is merged correctly
git branch -d my_branch # Deleted locally
git branch -D my_branch # Delete branch forcefully, if not merged
git branch -a # To check
git push origin --delete my_branch

Git revert :

git revert <hash-of-commit>

Additional commands :

git add . # Stage all the changes including untracked and .dot files# List out the differences 
git diff # In working directory
git diff --staged # In staging area
git diff hash1 hash2 # Between two commits via hashes
# Discard the changes in a working directory
git checkout <filename> # For a single file
git checkout -- . # All tracked files
git clean -df # All untracked files
# Remove files from staging area and put them back into working directory
git reset <filename> # For a single file
git reset # To remove everything
# Logs
git log # To check the various commits
git log -p # Log with patches
git log -2 # For last 2 commits
git log --stat # To see the changes
git log --oneline # All commit messages in single line
# gitignore
git rm --cached <file name> # ignore file that is already checked in
git rm --cached -r <folder name> # ignore folder that is already checked in
# Remote
git remote # List of all remotes
git remote -v # Be a little more verbose
git remote add <remote name> <url> # To set a remote

TOTAL RECAP :

To see local branches, run this command:

  • git branch

To see remote branches, run this command:

  • git branch -r

To see all local and remote branches, run this command:

  • git branch -a

Create a New Branch

  • Run this command (replacing my-branch-name with whatever name you want):
  • git checkout -b my-branch-name
  • You’re now ready to commit to this branch.

Switch to a Branch In Your Local Repo

  • Run this command:
  • git checkout my-branch-name

Switch to a Branch That Came From a Remote Repo

To get a list of all branches from the remote, run this command:

  • git pull

Run this command to switch to the branch:

  • git checkout — track origin/my-branch-name

Push to a Branch

If your local branch does not exist on the remote, run either of these commands:

  • git push -u origin my-branch-name
  • git push -u origin HEAD

NOTE: HEAD is a reference to the top of the current branch, so it’s an easy way to push to a branch of the same name on the remote. This saves you from having to type out the exact name of the branch!

If your local branch already exists on the remote, run this command:

  • git push

Merge a Branch

You’ll want to make sure your working tree is clean and see what branch you’re on. Run this command:

  • git status

First, you must check out the branch that you want to merge another branch into (changes will be merged into this branch). If you’re not already on the desired branch, run this command:

  • git checkout master

NOTE: Replace master with another branch name as needed.

Now you can merge another branch into the current branch. Run this command:

  • git merge my-branch-name

NOTE: When you merge, there may be a conflict. Refer to Handling Merge Conflicts (the next exercise) to learn what to do.

Delete Branches

To delete a remote branch, run this command:

  • git push origin — delete my-branch-name

To delete a local branch, run either of these commands:

  • git branch -d my-branch-name
  • git branch -D my-branch-name

NOTE: The -d option only deletes the branch if it has already been merged. The -D option is a shortcut for — delete — force, which deletes the branch irrespective of its merged status.

--

--