Thursday, July 17, 2014

Basic Git Command

Install

# Installing git
$ apt-get install git

# Installing ui version of git
$ sudo apt-get install gitk

Configure

# Set your user name and e-mail address
--global option, because then Git will always use that information for anything you do on that system.
$ git config --global user.name "Ratul"
$ git config --global user.email 13ratul@gmail.com

# Configuring default editor
$ git config --global core.editor emacs

# Color console
$ git config --global color.diff auto
$ git config --global color.status auto
$ git config --global color.branch auto

# List all the settings Git
$ git config --list

# Check what Git thinks a specific key’s value
$ git config user.name

# Three ways to get the manual page (manpage) help for any of the Git commands
$ git help <verb>
$ git <verb> --help
$ man git-<verb>

Uses (Local)

# If you’re starting to track an existing project in Git, you need to go to the project’s directory and type
$ git init

# Few git add commands that specify the files you want to track
# Also used for Staging Modified Files
$ git add *.c
$ git add README

# For ignoring folder or files -

  1. create a file named '.gitignore'
  2. write the name of those file, folder or git regular expression

$ cat .gitignore

# Determine which files are in which state
$ git status

# To see what you’ve changed but not yet staged, type git diff with no other arguments
$ git diff

# Compares your staged changes to your last commit:
$ git diff --cached

# Committing Your Changes
$ git commit

# You can type your commit message inline with the commit command
$ git commit -m "my commit messages"

# Don’t have to run 'git add' command
$ git commit -a -m ’added & committed new files’

# Forget to add some files, or you mess up your commit message. If you want to try that commit again
$ git commit --amend

#Remove file from your tracked files (more accurately, remove it from your staging area) and then commit.
$ git rm fileName

# Rename or move a file in Git
# Equivalent to: $mv README.txt README | $git rm README.txt | $git add README
$ git mv fileNameOld fileNameNew

# Viewing the Commit History
$ git log

# Unstaging a Staged File
$ git reset HEAD fileName

# Unmodifying a Modified File
$ git checkout -- fileName

Uses (Remote)

Adding remote repository 

  1. first create a repository in github or others
  2. then, Adding Remote Repositories
  3. git remote add [shortname] [url]:
  4. use the string of [shortname] on the command line to get the whole URL

$ git remote add tst https://github.com/Asraf-Uddin-Ahmed/vbb.git

# To see which remote servers you have configured
$ git remote
$ git remote -v

Cloning or make copy of remote project
git clone URL
$ git clone git://github.com/schacon/ticgit.git

Pushing to Remotes
# Using -f not a good parctice
$ git push vbb master
$ git push -f vbb master

Fetch
# Get data from remote projects
# The command goes out to that remote project and pulls down all the data from that remote project that you don’t have yet but NOT MERGE THEM
git fetch [remote_name]
$ git fetch tst

Pull
# Get data from remote prjects & merge with desired branch
git pull [remote_name] [branch_name]
$ git pull test master

Removing Remotes
$ git remote rm vbb

Renaming Remotes
$ git remote rename tst test

# See more information about a particular remote
$ git remote show vbb

No comments:

Post a Comment