Git Commands

Git is a distributed source control repository. To get details knowledge on git visit official website.

Setting Up Git

At first if you want to use git you need to setup the basic git configuration. This is a one time only tasks and hence is important to set proper git configurations.

Git comes with a tool called git config that lets you get and set configuration variables that control all aspects of how Git looks and operates. These variables can be stored in three different places:

  1. /etc/gitconfig file: Contains values for every user on the system and all their repositories. If you pass the option --system to git config, it reads and writes from this file specifically.
  2. ~/.gitconfig or ~/.config/git/config file: Specific to your user. You can make Git read and write to this file specifically by passing the --global option.
  3. config file in the Git directory (that is, .git/config) of whatever repository you’re currently using: Specific to that single repository.

 

Your Identity

Command
Description
git config --global user.name "<UserName>"
Given username has been set as global variant for all git commands you’ll use later.
git config --global user.email <Email_Id>
Given email will be set as global variant for all git commits.

Note: If you pass the --global option, then Git will always use that information for anything you do on that system. The given information will be set as default for any git changes in that system.

Your Editor

Command
Description
git config --global core.editor <Text_Editor>
Setup default text editor that you can use to type git commands/messages.

Note: If not set it will use by default whatever is available.

Check your Settings

Command
Description
git config --list
Shows a list of your configuration settings

For more detail visit: Git Setup

Git Help

Command
Description
git help
Basic help command for git
git help <information_about>

eg:

git help config
Specific help guide for specific git commands

 

Git Basics Commands

Create Git Repository

Command
Description
git init
Initialized empty Git repository in specified location.
git clone <url> "(optional_directory_name)"

Cloning an Existing Repository

To get a copy of existing git repository. We clone the git project into our desired location with git clone. 

Recording Changes to Git Repository

Command
Description
git status
This is the main tool that helps us determine the state of files in our repository. Different status of files are as follows:

Staged: These files have been added in your commit snapshots.

UnStaged: These the files that have previous snapshots and has been modified but not staged to be commit.

Untracked: These are the newly added files that you didn’t have in the previous snapshot (commit); Git won’t start including it in your commit snapshots until you explicitly tell it to do so.

git add --all

or

git add <file_location>
Add modified files and changes in your working directory to your index. To add all changes at once use

--all
git commit

or

git commit -m "<Commit_Message>"
Simply commit the staged files by using

git commit

or else add commit message

git commit -m "First Commit"

Note: do follow the standards to write commit messages.

git rm

Removing Files

git rm

To simply remove the files from your working directory and remote git repo.

git rm --cached <filename>

To remove git file that you no longer want git to track for commit yet keep the file in your local working directory.

Pushing Changes

Command
Description
git pull
Before pushing the changes it is highly recommended to make a git pull to assure your changes are made on latest project.

git pull

This pulls all the commits made on remote repo into our working directory.

git merge <branch>

Once you pull the latest changes from the remote the changes should be properly merged with your local directory.
To merge another branch into your active branch (e.g. master), we use

git merge &lt;branch&gt;

In both cases pull and merge git tries to auto-merge changes, but most of the times this is impossible due to conflicts. In this case we should manually edit conflict files, stage those files with

git add <filename>

preview changes for safety with

git diff <source_branch> <target_branch>

and finally merge.

git push origin master
To send the changes to your remote repository, we use this command.
You can change master to whatever branch we want to push your changes to.
git remote add origin <repo_url>
If you have not cloned an existing repository and want to connect your repository to a remote server, you need to add it with this code.
Now you are able to push your changes to the selected remote server

Viewing Changes To the Repo

Command
Description
git log
View the detail information of the commits you have done till now in the repo.

git log --oneline

View single line information about the commit logs.

git log -p -2

-p shows the difference introduced in each commit
-2 limits the output to only the last two entries

Branching

Command
Description
git checkout -b feature_x
Create a new branch named “feature_x” and switch to it using

git checkout
git checkout master
Switch branch back to master
git branch -d feature_x
Delete the branch “feature_x”
git branch -D <branch_name>
Forcefully delete the branch
git push origin <branch>
A branch is not available to others unless you push the branch to your remote repository. Use git push to push new branch to remote.
git push origin :<branch_name>
Delete remote branch
git branch -m <new_name>
Rename the current branch
git branch -m <old_name> <new_name>
Rename the mentioned branch name

Tagging

Normally tagging is used to mark release points. Tagging is done to mark specific points as being important in history.

Command
Description
git tag
List all the tags currently present in the project.
git show <tag_name>
Show detail of tag.
git tag -a <version_code> -m <tag_message>
Annotated tag that is marked with tag message and contains detail message.
git tag <version_code>
Lightweight tagging
git push origin <tag_name>
Share your tag to the remote

Some Special Cases

Remove Untracked files

Command
Description
git clean -f
Remove untracked files forcefully
git clean -df
Remove untracked files and directories
git checkout <filelocation>
Remove single untracked file

Stashing

Stashing is needed when we want to switch branch without commit current changes.

Command
Description
git stash
The current state of your working directory ie. your modified tracked files and staged changes are taken and saved on a stack of unfinished changes that you can reapply at any time.
git stash pop
Bring back your stashed changes to current working directory.

Undoing Things

Command
Description
git reset HEAD <branch>
If you instead want to drop all your local changes and commits, fetch the latest history from the server and point your local branch we use git reset HEAD.

IMPORTANT

While git reset can be a dangerous command if you call it with --hard, in this instance the file in your working directory is not touched. Calling git reset without an option is not dangerous – it only touches your staging area.

git checkout -- <file>
If you want to revert your changes use this command.

IMPORTANT

It’s important to understand that git checkout -- [file] is a dangerous command. Any changes you made to that file are gone – you just copied another file over it. Don’t ever use this command unless you absolutely know that you don’t want the file.

Learn more here

gitignore

Sometimes we have updated the gitIgnore files after it has been pushed to the remote. Now we want to update our project according to the updates in gitIgnore file.

First:

update the gitignore file by opening it in any text editor. Following is example for Linux system from terminal:

Command
Description
nano .gitignore
opens

.gitignore 

file in edit mode in terminal

Add the path of file you want to ignore in

 nano .gitignore 

and save it.

Second:

Command
Description
git rm -r --cached <filename> 
This removes any changed files from the index(staging area)

Write  .  in case of all files

Third:

Add modified files and changes in your working directory to your index. Set an commit message.

If you want to undo

git rm --cached <filename>

, use git add filename.

Advertisements

One thought on “Git Commands

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s