Bala's Blog

Git

January 8 2017
314 words, ~2 min. read
git 

.gitconfig

git config --global user.name "Balachandher Srinivasan"
git config --global user.email "bala@sbchand.net"
# checkout as-is; commit UNIX style
git config --global core.autocrlf Input
git config --global core.excludesfile "~/.gitignore_global"
git config --global push.default simple
git config --global color.status "auto"
git config --global color.branch "auto"
git config --global color.interactive "auto"
git config --global color.diff "auto"
git config --global url."https://".insteadOf git://
git config --global http.followRedirects true
git config --global http.proxy "http://proxy.sbchand.net:8080"

.gitignore_global

# backup
*.*~

# IDEA
*.iml

# Eclipse
.project
.classpath
.settings/

# Java
*.class
*.jar
*.war
*.ear

Create local repository

git init --bare

Create a remote repository

git init                                # initialize
...                                     # add contents
git add .                               # add 
git commit -m "Initial commit"          # commit
git remote add origin <remote location> # add remote as origin
git remote -vv                          # confirm remote 
git push origin master                  # push to remote master

Clone remote repository

git clone <remote location>

Branch

git branch sb-local-branch
git checkout sb-local-branch

or

git checkout -b sb-local-branch

  1. To view current branch, git branch
  2. To view all branches, git branch -a
  3. To show upstream, git branch -vv
  4. To show remote, git remote show origin
  5. To delete local branch, git branch -D <local-branch-name>
  6. To delete remote branch, git push origin --delete <remote-branch>

Fetch a Pull Request (PR) as a local branch

To fetch a pull request, say 101, and automatically create a local branch pr-101,

git fetch origin pull/101/head:pr-101

Push changes to remote

git checkout -b sb-local master    # create branch from master
...                                # make changes
git add .                          # add
git commit -m "Updated"            # commit
git push origin sb-local:sb-remote # push changes from sb-local to sb-remote

Reset

  1. To reset a single file, git checkout <file>
  2. To reset everything, git reset --hard HEAD

Stash

# ensure to add files (to index) before you stash

git stash save "solution-1"
git stash list
git stash show -p stash@{0}
git stash apply stash@{0}