Dealing with Git

Here are some references and thoughts when dealing with git.

Basic Config

Type these for first time configuration only.

git config --global "your_name"
git config --global "your_email"
git config --global color.ui true

Here is a sample global git configuration:

    name = Name
    email = Email
    ui = true
    user = Username

If you want to set special user name for a repo just delete –global

Basic Usage

These are the steps for using git with github or without it in an easy way:


git clone
git clone [email protected]:username/repo.git


git push origin master

If you don’t want to be asked about your username and password for your Github account eveytime you push. Then you shoud

  1. Add new deploy key in your repository on github with your ssh public key
  2. if you clone it with [email protected]:username/repo.git you don’t need to type any thing else. if not then type this:
git remote set-url origin
[email protected]:username/repo.git


git fetch origin
git merge origin/master

If you have forked your repository and wanted to keep tracking with the creator of the repository:

git remote add --track master owner
git fetch owner
git merge owner/master

–track master: just track the master


check available remotes:

git remote -v

Add a remote:

git remote add origin


These are the steps for creating branches, and then merging them

New branch

git checkout -b hotfix


git branch hotfix
git checkout hotfix

Commit changes

git add .
git commit -m “msg”

Merge changes to master

git checkout master
git merge hotfix

Delete the branch (Optional)

git branch -d hotfix

Ignoring files

This is how to ignore files before adding them to the repository. Create this file in the root folder in the repository

vi .gitignore

Type your files and folders

Remove files

These are the ways of removing files from git repository in multiple situation

Keep them in the real drive

If you added files to a repository and after that you want to ignore them first type them in .gitignore, then type:

git rm -r --cached .
git add .
git commit -m ".gitignore is now working"

For specific file replace dot in the first command with file_name

Remove them from repository and real drive:

git rm file_name

Remove files from repository that are deleted from real drive

git status -s | grep " D" | awk '{print $2}' | xargs git rm

Advance Usage:

# Logs
git log --graph
git log --graph --oneline
git log --graph --oneline --decorate --all

Squash Commits to a Single Commit

This is a good reference

To squash four commits into one, do the following:

$ git rebase -i HEAD~4

First one make it pick and the others make them squash. Then quit and save.To change the message of a commit before pushing the changes:

$ git commit --amend

if you have already pushed your changes to the git server and you want to push them again with the squash commits use –force. Example: git push --force origin master

Get changes from master into a branch

use rebase instead of merge to keep the history clean and avoid an extra commit of the merging

git checkout branch_name
git fetch
git rebase origin/master


First Get this file, and Then:

Remove all your local git branches but keep master

git branch | grep -v "master" | xargs git branch -D

Provide better commit messages:

$ vi ~/.git_commit_msg.txt

# If applied, this commit will...

# Explain why this change is being made

# Provide links to any relevant tickets, articles or other resources

$ git config --global commit.template ~/.git_commit_msg.txt

log history

git log --pretty=short -u -L 8,9:path_to_file

Remove Branch from original source

git push origin --delete BRANCH_NAME


comments powered by Disqus