Git Commands and Tutorials
Date posted : 21.09.2018
In this article we learn about the overview of git commands and step by step tutorials.
Basic Git Tutorial:
This tutorial provides a list of the most common GIT commands and a short description to explore lot in it.
Git at three States:
- Commited (stored in local database).
- Modified (file changed but not commited to database).
- Staged (modified file is marked to go into the next commit snapshot).
Step 1: Git Installation in Linux:
For Debian Linux based environment use this command line.
● apt-get install git
For Centos based environment use this command line.
● yum install git
Step 2: Git Configuration:
# git config --global user.name "Name Surname"
# git config --global user. Email email@example.com
Step 3: Getting Git Repository:
- To clone existing repository from server 2:
If you want to get a copy of an existing Git repository,Use git clone [URL].
git clone user@server:/path.git
# git clone username@/10.1.1.1:/usr/local/blog01.git
- To start tracking new/edited filename(add single file at a time).
# git add filename
- Let’s start tracking all changed/new files(add multiple files at a time).
# git add . (git add -A)
- Save changes to the local repository.
# git commit -m 'Commit message’
Step 4: Recording changes to the repository:
# git status
Step 5: Recording changes to the repository:
- Tracking our files.
- To Begin tracking a new file (directory).
# git add
Step 6: Recording changes to the repository:
- Staging modified files.
- To Stage the file, add this content to the next commit.
Step 7: Viewing staged and unstaged changes:
- To view what changes but not yet get staged ,So that diff command is used.
Step 8: Recording changes to the repository:
- To commit your changes to the local repository.
# git commit
Step 9: To Viewing the commit history:
# git log
Step 10: Working with remotes:
- Fetch: To fetch all the information you don’t have from remote repository, no automatic merging occurs here.
- Merge: To automatically merge data from remote with your repository data.
- Pull: To fetch and merge automatically.
- pull = fetch + merge
- Push:To push your version to the server.
$ git push origin master
Git won’t allow to push to the remote which is ahead of your version:
At first needed to be fetch. Otherwise show a error as follows,
![rejected] master -> master (non-fast-forward)
error: failed to push some refs to 'http://gituser/XYZ.git'.
hint: Updates were rejected because the tip of your current branch is behind.
hint: its remote counterpart. Merge the remote changes (e.g. 'git pull')
hint: before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
Still now we have practiced some basic git commands.We hope it will be useful for the Beginners. Write us in the comment box for any suggestions. Thanks in advance.