Git Commands

git config

Sets configuration values for your user name, email, gpg key, preferred diff algorithm and more.

Ex : git config –global user.name “MyName”

Ex : git config –global user.email “user@domain.com”

git init

Initializes a git repository – creates the initial ‘.git’ directory in a new or in an existing project.

Ex : git init

git clone

Makes a Git repository copy from a remote source. Also adds the original location as a remote so you can fetch from it again and push to it if you have permissions.

Ex: git clone git@github.com:user/test.git

git add

Adds files changes in your working directory to your index.

Ex: git add

git rm

Removes files from your index and your working directory so they will not be tracked.

Ex : git rm filename

git commit

Takes all of the changes written in the index, creates a new commit object pointing to it and sets the branch to point to that new commit

Ex : git commit -m “file changed” filename

git status

Shows you the status of files in the index versus the working directory. It will list out files that are untracked (only in your working directory), modified (tracked but not yet updated in your index), and staged (added to your index and ready for committing).

Ex : git status

git branch

Lists existing branches, including remote branches if ‘-a’ is provided. Creates a new branch if a branch name is provided.

Ex : git branch -a

Ex : git branch

git checkout

Checks out a different branch – switches branches by updating the index, working tree, and HEAD to reflect the chosen branch

Ex : git checkout filename

Ex : git checkout newbranch

git merge

Merges one or more branches into your current branch and automatically creates a new commit if there are ngno conflicts.

git merge newbranchversion

git reset

Resets your index and working directory to the state of your last commit. Used for reverting the commit, which is not pushed to remote.

Ex :

git reset --soft HEAD^     # use --soft if you want to keep your changes
git reset --hard HEAD^     # use --hard if you don't care about keeping the changes you made

To Un-stage a commit:-

git reset <filename>

git revert

The git revert command undoes a committed snapshot. But, instead of removing the commit from the project history, it figures out how to undo the changes introduced by the commit and appends a new commit with the resulting content. This prevents Git from losing history, which is important for the integrity of your revision history and for reliable collaboration.

git revert <commit-number>

–> Generate a new commit that undoes all of the changes introduced in <commit>, then apply it to the current branch.

git cherry-pick

What git cherry-pick does, basically, is take a commit from somewhere else, and “play it back” wherever you are right now. Because this introduces the same change with a different parent, Git builds a new commit with a different ID.

git cherry-pick <commit-number>

git rebase

Rebasing is the process of moving a branch to a new base commit, rebasing really is just moving a branch from one commit to another. But internally, Git accomplishes this by creating new commits and applying them to the specified base—it’s literally rewriting your project history. It’s very important to understand that, even though the branch looks the same, it’s composed of entirely new commits.

git rebase <base>

Rebase the current branch onto <base>, which can be any kind of commit reference (an ID, a branch name, a tag, or a relative reference to HEAD).

git stash

Temporarily saves changes that you don’t want to commit immediately. You can apply the changes later.

Ex : git stash
Saved working directory and index state “WIP on master: 84f241e first commit”
HEAD is now at 84f241e first commit
(To restore them type “git stash apply”)

git tag

Tags a specific commit with a simple, human readable handle that never moves.

Ex : git tag -a v1.0 -m ‘this is version 1.0 tag’

git fetch

Fetches all the objects from the remote repository that are not present in the local one.

Ex : git fetch origin

git pull

Fetches the files from the remote repository and merges it with your local one. This command is equal to the git fetch and the git merge sequence

Ex : git pull origin

git push

Pushes all the modified local objects to the remote repository and advances its branches.

Ex : git push origin master

git remote

Shows all the remote versions of your repository.

Ex : git remote

git log

Shows a listing of commits on a branch including the corresponding details.

Ex : git log

git show

Shows information about a git object.

Ex : git show

git ls-tree

Shows a tree object, including the mode and the name of each item and the SHA-1 value of the blob or the tree that it points to.

Ex : git ls-tree master^{tree}
100644 blob e69de29bb2d1d6434b8b29ae775ad8c2e48c5391    README

git cat-file

Used to view the type of an object through the SHA-1 value.

Ex : git cat-file -t e69de29bb2d1d6434b8b29ae775ad8c2e48c5391
blob

git grep

Lets you search through your trees of content for words and phr

Ex : git grep “www.siteground.com” — *.html

git diff

Generates patch files or statistics of differences between paths or files in your git repository, or your index or your working directory.

Ex : git diff

Taking patches using git diff

Ex: git diff Gemfile > gemchanges.diff

git apply

Applies the patches taken using git diff

Ex : git apply <patch-file>

gitk

Graphical Tcl/Tk based interface to a local Git repository.

git instaweb

Runs a web server with an interface into your local repository and automatically directs a web browser to it.

Ex : git instaweb –httpd=webrick
git instaweb –stop

git archive

Creates a tar or zip file including the contents of a single tree from your repository.

Ex : git archive –format=zip master^ README >file.zip

git gc

Garbage collector for your repository. Optimizes your repository. Should be run occasionally.

git fsck

Does an integrity check of the Git file system, identifying corrupted objects.

git prune

Removes objects that are no longer pointed to by any object in any reachable branch.

Commands for creating new branch in local which is in remote already (ex.., railsfactory)

git stash and pop

git stash save will temporarily save the changes and remove it, whereas git pop will again revert back the changes previously done.

git stash save “work in progress”

git stash pop