
Before, I discussed the very basics of git and GitHub for git project hosting. However, none of those tutorials covered the important git commands that are usually needed. I will try to demonstrate some of the most important basic git commands here. So, let’s get started. :).
Initialize and configure:
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"
Code language: PHP (php)
Add/Remove/Edit Files And Save Changes Locally:
You can add specific files 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 *
Code language: PHP (php)
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
Code language: PHP (php)
You can use the above “git add—u” command to add the modified files to the current staging.
Now, if you don’t care to differentiate between tracked/non-tracked files, you should be fine by running a single command for all operations at once, which is as follows:
git add *
It’s very easy, huh? You 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'
Code language: JavaScript (javascript)
Connect and communicate with the remote server:
Well, we have to work in teams and share changes with one another. So, it’s a must to know the commands to communicate with a git server.
Note that I have already discussed the initial setup procedure for the server in my earlier git articles. Please refer to them if you don’t have any idea yet. I will demonstrate a few useful commands here only.
To clone/checkout the latest version of a repository from the server, the commands will be as follows:
# For initial setup in your local repository from server
git clone username@host:/path/to/repository
Code language: PHP (php)
To update your local version to the newest commit from others to the server, run the following command:
#get latest version from server and merge with your local version
git pull
Code language: PHP (php)
This command actually runs two consecutive commands. One is “git fetch,” which usually gets the latest version from the server. The second one is “git merge,” which merges your local version with the new fetched version. If you don’t need to merge, you can use the fetch command only specifically instead.
If you want to retrieve a specific version of a file from your local repository, you will need to use the “git checkout” command instead of the pull/fetch command as follows:
git checkout -- <filename>
Code language: HTML, XML (xml)
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
Code language: CSS (css)
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>
Code language: PHP (php)
Branching:
You can very easily create/delete/merge a new branch parallel to the original one. You can keep the branch locally only, or you may push it to the server as well.
The following command will help you to create a new branch and switch to it(locally):
git checkout -b <branch_name>
Code language: HTML, XML (xml)
To switch to another branch or delete a branch, you may use the 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>
Code language: PHP (php)
To push a branch to the server, change the ‘master’ part from the push command to 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 the following commands:
# to see details differences
git diff HEAD
#to see summary differences
git diff --stat
Code language: PHP (php)
Save Temporary Changes:
If you are doing some work but in the middle and need to switch to something else, you will want to save your current work and return to it later. Git stashing will help you a lot in this way. It does the same thing. The command is as simple as below:
#to save changes
git stash
#to retrieve changes from stash
git stash apply
Code language: CSS (css)
You can learn more about this command on git stash.
References:
The following resources will also help you a lot with more details.
Also, if you are a NetBeans user, you should be happy to know that a git plugin is available for NetBeans in recent versions(after 7). With it, you can have complete control over your git repository right from your IDE. Please check out my git plugin for the NetBeans tutorial for details.
I hope this basic git commands tutorial will help you to some extent. Let me know if you have any specific questions. Happy coding 🙂
Discover more from CODESAMPLEZ.COM
Subscribe to get the latest posts sent to your email.
Leave a Reply