Git Useful Commands
Installing Git:
To install git on a RHEL based system:
$ sudo yum -y install git
Configure name and email:
After installing git, you need to configure name and email.
$ git config --global user.name "firstname lastname"
$ git config --global user.email example@example.com
Authenticate with private SSH key:
A good practice is to authenticate with a remote git server using ssh keys, you can generate ssh keys with ssh-keygen and answer the questions:
$ ssh-keygen
After generating the ssh key you need to copy the public key contents to the remote git server, you can find your public key at:
~/.ssh/id_rsa.pub
Clone a repository (git clone):
To clone a remote repository:
$ git clone <remote repository url>
What it does:
- Download a local copy of the remote repository, including all past changes.
Create a local repository (git init):
To create a new local repository:
$ git init
Get the status of a repository (git status):
$ git status
This command will tell you information about:
- Changes not staged for commit
- Changes staged, but not committed
- Current branch
Stage files for the next commit (git add):
The add command stages changes for the next commit Single file:
$ git add <file>
All files:
$ git add .
Commit files (git commit):
$ git commit -m "commit message"
- commit adds changes to the local copy of the repository, not in the remote.
- files that have not be staged with
git add
will not be commited
Push changes to remote repository (git push):
$ git push
git push
without parameters will push changes to the remote repository that is associated with the current local branch
Checkout to another branch (git checkout):
$ git checkout <branch name>
- Any changes now on will be staged on this repository
-b
parameter creates a new local repository and check it out immediately.
Tags (git tag):
Tag your latest commit:
$ git tag sometext HEAD
List tags:
$ git tag
Push tags to remote repository:
$ git push --tags
Merge (git merge):
Many times we will need to merge a branch with the master
# Be sure that we work on the master branch
$ git checkout master
# Create a new branch and checkout into
$ git branch new-branch
$ git checkout new-branch
# Create some new files
# vi file.txt etc...
# Stage files and commit to new-branch
$ git add .
$ git commit –m ”a commit message"
# Checkout to master branch
$ git checkout master
# Merge new-branch with master branch
$ git merge new-branch