• Skip to main content
  • Skip to primary sidebar
  • Skip to footer
  • Home
  • Featured
    • C# Tutorials
      • LinQ Tutorials
      • Facebook C# API Tutorials
    • PHP Tutorials
      • CodeIgniter Tutorials
    • Amazon AWS Tutorials
  • Categories
    • Programming
    • Development
    • Database
    • Web Server
    • Source Control
    • Management
    • Project
  • About
  • Write
  • Contact

CodeSamplez.com

Programming, Web development, Cloud Technologies

You are here: Home / Source Control / Few Important Basic Git Commands Examples

Few Important Basic Git Commands Examples

January 19, 2012 by Rana Ahsan 1 Comment

Useful Git Commands

Before, I have discussed about very basics about git and github for git project hosting. However, none of those tutorials covered the important git commands that is usually needed. I will try to demonstrate some of the most important basic git commands here. So, let’s get started :).

Initialize and configuration:

Here are some initialization commands:

//Initialize git repository on current directory
git init

//Setup default global user name
git config --global user.name "Your Name"

//Setup default global user email
git config --global user.email "your_email@youremail.com"

Add/Remove/Edit Files And Save Changes Locally:

You can add specific file or all new files(which aren’t tracked) with the following commands:

#To Add a single file to repository
git add <filename>

#To add all non tracked files in current repository directory
git add *

You can remove one or more files from current tracked files with the following commands:

#Warning: This will remove the physical file also
git rm <filename>

#remove all the files it did tracked previously and now is being deleted by user. So, it doesn't add anything from untracked files at all
git add -u

To add the modified files to current staging, you can use the above “git add -u” command.

Now, if you don’t care to differentiate from tracked/non tracked files, you should be fine by running a single commands for all operations at once which is as follows:

git add *

very easy. huh? You will just have to remember that, this basic git command will create a new snapshot according to your current physical file structure.

Commit Files:

Committing files is quite easy as follows:

$ git commit -m 'committed some changes'

Connect and communicate with remote server:

Well, we have to work in team and need to share changes among one another. So, it’s a must to know the commands to communicate with a git server.

Note that, I have already discussed about initial setup procedure for server on my earlier git articles. Please refer to them if you don’t have any idea yet. I will demonstrate few useful commands here only.

To clone/checkout the latest version a repository from server, the commands will be as follows:

# For initial setup in your local repository from server
git clone username@host:/path/to/repository

To update your local version to the newest commit from others to server, run the following command:

#get latest version from server and merge with your local version
git pull

This command actually runs to consecutive commands. One is “git fetch”, which usually get the latest version from server. The second one is “git merge” which merges your local version with the new fetched version. If you don’t need merge, you can use only the fetch command specifically instead.

If you want to retrieve a specific version of a file from your local repository, you will need to use “git checkout” command instead of pull/fetch command as follows:

git checkout -- <filename>

To commit your final changes, which you did locally with ‘git commit’ commands, use the following command:

#Upload local commits to server
git push origin master

If you haven’t added your repository to any remote version yet and want to do so now, you can run the following commands:

#If not public, you will may be prompt for authentication
git remote add origin <server_name>

Branching:

You can create/delete/merger a new branch parallel to the original one very easily. You can keep the branch locally only, or you may push it to server also.

The following command will help you to create a new branch and switch to it(locally):

git checkout -b <branch_name>

to switch to another branch or delete a branch, you may use following commands:

#Deletes a branch
git branch -d <branch_name>

#switch to another branch(use 'master' if want to switch back to master branch)
git checkout <branch_name>

to push a branch to server, just change the ‘master’ part from the push command with the corresponding branch name.

See current status/difference of files:

to see in which conditions your files are at this moment you can use the following commands:

git status -s

To see the difference between your file and the repository version, you can use following commands:

# to see details differences
git diff HEAD

#to see summary differences
git diff --stat

Save Temporary Changes:

If you are doing some works, but in the middle, need to switch to something else, you will want to save your current work somehow and come back to it later. Git stashing will help you in this way a lot. it does the exact same thing. The command is as simple as below:

#to save changes
git stash

#to retrieve changes from stash
git stash apply

You can learn more about this command on git stash.

References:

The following resources are also will help you a lot in more details.

  • Git Reference
  • Simple Guide
  • GIT Online Book

Also, if you are a netbeans user, you should be happy to know that, on recent versions(after 7), git plugin is available for netbeans. With it you can have complete control over your git repository right from your IDE. Please checkout my git plugin for netbeans tutorial for details.

Hope this basic git commands tutorial will help you in some extent. Let me know if you have any specific question. Happy coding 🙂

Share If Liked

  • Click to share on Facebook (Opens in new window)
  • Click to share on Twitter (Opens in new window)
  • Click to share on LinkedIn (Opens in new window)
  • Click to share on Pinterest (Opens in new window)
  • Click to share on Reddit (Opens in new window)
  • Click to share on Tumblr (Opens in new window)
  • Click to share on Pocket (Opens in new window)

You may also like

Filed Under: Source Control Tagged With: git

About Rana Ahsan

Rana is a passionate software engineer/Technology Enthusiast.
Github: ranacseruet

Reader Interactions

Trackbacks

  1. Working With Netbeans GIT Plugin | codesamplez.com says:
    November 8, 2012 at 4:15 am

    […] more or less and now want to use the Netbeans git plugin. As a short course, you can refer to my important git commands tutorial.Installing the git plugin:Go To 'Tools'->'Plugins' menu on Netbeans. You should be able to […]

    Reply

Leave a Reply Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 3,774 other subscribers

Follow Us

  • Twitter
  • Facebook

Top Posts & Pages

  • How To Work With JSON In Node.js / JavaScript
    How To Work With JSON In Node.js / JavaScript
  • PHP HTML5 Video Streaming Tutorial
    PHP HTML5 Video Streaming Tutorial
  • How To Work With C# Serial Port Communication
    How To Work With C# Serial Port Communication
  • LinQ Query With Like Operator
    LinQ Query With Like Operator
  • Facebook C# API Tutorials
    Facebook C# API Tutorials
  • Using Supervisord Web Interface And Plugin
    Using Supervisord Web Interface And Plugin
  • Get Facebook C# Api Access Token
    Get Facebook C# Api Access Token
  • Beginning Codeigniter Application Development
    Beginning Codeigniter Application Development
  • How To Use Hotkeys/Keyboard Events In WPF Application Using C#
    How To Use Hotkeys/Keyboard Events In WPF Application Using C#
  • Control HTML5 Audio With Jquery
    Control HTML5 Audio With Jquery

Recent Posts

  • Building Auth With JWT – Part 2
  • Building Auth With JWT – Part 1
  • Document Your REST API Like A Pro
  • Understanding Golang Error Handling
  • Web Application Case Studies You Must Read

Tags

.net angularjs apache api audio auth authenticatin aws c# cloud server codeigniter deployment docker doctrine facebook git github golang htaccess html5 http javascript jwt linq mysql nodejs oop performance php phpmyadmin plugin process python regular expression scalability server smarty socket.io tfs tips unit-test utility web application wordpress wpf

Footer

Archives

Follow Us

  • Twitter
  • Facebook

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 3,774 other subscribers

Copyright © 2023