Starting with Git, using the command line
1) Git Configuration git config –global
1. Setup Git (after Install Git step)
$ git config --global user.name "Full Name"
$ git config --global user.email "email@addres.com"
This needs to be done only once, the first time after install Git (before using it). Use your full name and email address
2) Import existing local files git init, git add [directories + files], git commit {-m commit_msg}, git push <remote_name>
Following are steps for Adding an existing local project to GitHub
1. Change the current working directory to local project.
$ cd my-project
2. Initialize the local directory as a Git repository.
$ git init
TO create git repo from current directory. Initialize empty Git repository.
3. Add the files in new local repository. This stages them for the (first) commit.
$ git add .
TO add all files & sub-directories. It stages the changes.
BEFORE git add
TO undo the changes in a file, use ‘git checkout FILE_NAME’
TO undo the changes in all files, use ‘git checkout’
AFTER git add, BEFORE git commit
TO un-stage changes in a file, use ‘git reset HEAD FILE_NAME’
TO un-stage changes in all files, use ‘git reset’
4. Commit the files that are staged in your local repository.
$ git commit -m "First commit"
TO commit the tracked changes and prepares them to be pushed to a remote repository.
TO remove this commit and modify the file, use 'git reset --soft HEAD~1' and commit and add the file again.
$ git status
TO check current state of repository.
$ git log {OPTIONS} {FILE_NAME}
TO view project’s history. It lists all commits to the repository
5. Perform “Adding a remote repo” if not done.
6. Push the changes in local repository to GitHub
$ git push {-u} my-origin master
TO push changes to the remote repository specified as the my-origin
3) Adding a remote repo git remote add <remote_name> <remote_repository_URL>
1. Set the new remote (Run command in the directory where local repository is stored at)
$ git remote add my-origin git@github.com:username/new_repo OR
$ git remote add my-origin git@github.com:/new_repo.git OR
$ git remote add my-origin https://github.com/username/new_repo OR ask userid-pwd
$ git remote add my-origin https://github.com/username/new_repo.git OR ask userid-pwd
remote_repository_URL is location where local repository will be pushed.
TO adds the URL for the remote repository where your local repository will be pushed.
2. Verify the new remote URL
$ git remote -v
Generating and Adding SSH key
4) Description of few git commands
1. $ git add
git works with the changes, not the files.
Git concentrates on the changes to a file, not the file itself. A ‘git add file’ command does not tell git to add the file to the repository, but to note the current state of the file for it to be committed later.
2. $ git status
It checks the status and reports if anything to add or commit, meaning the repository stores the current state of the working directory, and there are no changes to record.
Use to keep monitoring the states of both the working directory and the repository.
3. $ git log {OPTIONS} {FILE_NAME}
OPTIONS: provided in the git-log instruction
--pretty=oneline Single line predefined format
--pretty=format:"%h %cd %s (%an)" Single line format
--pretty=format:"%h %ad | %s%d [%an]" Most suitable
--pretty="..." Defines the output format.
%h Hash of the commit
%ad Commit date
%s Commit comment
%d Commit decorations (e.g. branch heads or tags)
%an Author name
%cd
--graph To display commit tree in an ASCII graph layout
--date=short Keeps the date format short and nice
--max-count=2
--since='7 days ago' Review the changes made within the last week
--until='5 minutes ago'
--author=<author name>
--all
4. git config –global
It can be used to setup aliases and shortcuts for git commands
git config --global alias.co checkout
git config --global alias.ci commit
git config --global alias.st status
git config --global alias.br branch
git config --global alias.hist "log --pretty=format:'%h %ad | %s%d [%an]' --graph --date=short"
git config --global alias.type 'cat-file -t'
git config --global alias.dump 'cat-file -p'
OR
Add the following to the ‘.gitconfig’ file in your $HOME directory.
[alias]
co = checkout
ci = commit
st = status
br = branch
hist = log --pretty=format:\"%h %ad | %s%d [%an]\" --graph --date=short
type = cat-file -t
dump = cat-file -p
OR
Command aliases through shell ‘.profile’
alias gs='git status '
alias ga='git add '
alias gb='git branch '
alias gc='git commit'
alias gd='git diff'
alias go='git checkout '
alias gk='gitk --all&'
alias gx='gitx --all'
5. git diff
6. git branch
7. git checkout
8. git cat-file
References:
- “starting a git repo”
- http://kbroman.org/github_tutorial/pages/init.html