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 "firstname.lastname@example.org"
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.
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>
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.
The following resources are also will help you a lot in more details.
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