Git

« Go back to Cheatsheets

Table of Contents

Getting Started

1
2
3
4
5
# initialize git in the path
  git init

# clone a existing repo
  git clone git@github.com:<github-username>/<repository-name>.git

Global User Configuration

1
2
3
4
# global config
  git config --global user.name <github-username>
  git config --global user.email <your_email_id@domain.com>
  git config --global color.ui true

Create New Git Project

Before going to terminal, create new repository in github website & note down the <repository-name>

1
2
3
4
5
6
7
8
9
# steps
  mkdir <new-git-project>
  cd <new-git-project>
  git init
  touch README
  git add README
  git commit -m "first commit message"
  git remote add origin git@github.com:<github-username>/<repository-name>.git
  git push origin master

Add files to Existing Repo

Navigate to your file location on terminal and add remote

1
2
cd <existing-git-repo>
git remote add origin git@github.com:<github-username>/<repository-name>.git

Generate SSH

1
2
3
4
5
6
# generating SSH
  cd `./ssh`
  ssh-keygen -t rsa -C "your_email_id@domain.com"

# copying SSH Key
  pbcopy < ~/.ssh/id_rsa.pub

Stash

Stashing is the term git uses for saving your changes before taking pull.

1
2
3
4
5
6
7
8
9
10
11
# save / stash your changes
  git stash save "<stash-name>"

# list of stashed changes
  git stash list

# retrieve / put back stashed changes
  git stash apply stash@{index} # where index is the position of stash shown in stash list

# delete stashed/saved changes
  git stash drop "<stash-name>"

Note: <stash-name> is needed if you work on multiple saves otherwise you can skip and type git stash to save and git stash apply or git stash pop to retrieve.

Branch

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# create a new branch
  git checkout -b <branchname>
  git push origin <branchname> --set-upstream

# show branches
  git branch

# show local & remote branches
  git branch -a

# get a remote branch
  git fetch origin
  git checkout --track origin/<branchname>

# merge branch
  git merge <branch-name>

# list merged branches
  git branch -a --merged

# delete local remote-tracking branches
  git remote prune origin

# delete local branch
  git branch -D <branchname>

# delete remote branch
  git branch -r -d <branchname>

# delete local & remote branch
  git push origin --delete <branch-name>

Merge & Rebasing

Branches are good for developing features to your software, the below steps walk you through creating, rebasing and merging.

Creation

1
2
3
4
5
6
7
8
9
10
# from master branch create your feature branch
  git checkout -b <feature-branch>

# checkout/navigate to your branch
  git checkout <feature-branch>

# make changes, commit and push
  git add -A
  git commit -m "new feature updates"
  git push origin <feature-branch>

Rebasing — Rebase to make sure we sync our feature branch with master

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# naviagate/checkout to master
  git checkout master

# pull to get latest code update
  git pull origin master

# navigate back & rebase
  git checkout <feature-branch>
  git rebase master

# incase of conflict, make changes and just add (don't commit)
  git add -A
  git rebase --continue

# once the rebase is completed, do force push
  git push origin <feature-branch> --force

Squash — all you branch commits to one commit with meaningful message

1
2
3
4
5
6
7
8
9
10
11
# Method 1
  git reset origin/<branch-you-branched-from>
  git add --all
  git commit -m "new feature name"
  git push origin <branch-name>

# Method 2
  git checkout master
  git merge --squash <feature-branch> # at this point everything is merged, not commited
  git add -A
  git commit -m "Feature" # push as single commit

Merge your branch to master

1
2
3
4
5
6
7
8
9
# checkout/navigate to master branch
  git checkout master

# merge your code to master. PS. using this will auto-close PR
  git merge <feature-branch>
  git push origin master

# delete your branch
  git branch -D <feature-branch>

Commit

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# delete last remote commit
  git reset --hard HEAD~1
  git push origin HEAD --force

# delete local commits
  git reset --hard HEAD~1

# rollback latest remote commit
  git reset --hard HEAD^

# rollback to a specific commit
  git reset --hard <commit-sha>

# amend last commit message
  git commit --amend --author="<github-username>"

Multiple github accounts

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# add new account for company
  ssh-keygen -t rsa -C "company_email_id@domain.com"
  ~/.ssh/id_rsa_company

# create config
  touch ~/.ssh/config
  vim config

# Config file content should have
  # default account
  Host github.com
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_rsa

  # company account
  Host github-COMPANY
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_rsa_company

# Do it on github website - add ssh keys to github account

# add origin for projects
  git remote add origin git@github-COMPANY:<github-username>/<repository-name>.git

Reference — Nettuts, Arnaudrinquin

Remote

Adding a remote

To add a new remote, navigate to project path in terminal.

The git remote add command takes two arguments:

  • A remote name, for example, origin
  • A remote URL, for example, https://github.com/user/repo.git
1
2
3
4
5
# Set a new remote
git remote add origin https://github.com/user/repo.git

# Push to repo
git push -u origin master

View remote

1
2
3
4
git remote -v
# returns existing remote
origin  https://github.com/user/repo.git (fetch)
origin  https://github.com/user/repo.git (push)

Remove remote

1
2
3
git remote remove origin
# or
git remote rm origin

Avoid git push requires username, password everytime

A common mistake is cloning using the default (HTTPS) instead of SSH. You can correct this by going to your repository, clicking the ssh button left to the URL field and updating the URL of your origin remote like this:

1
git remote set-url origin git@github.com:username/repo.git

Move git files with commits from old to new repo

Navigate to old repo path eg. cd /works/old-repo/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# remove old remote
  git remote rm origin

# make sure remote is empty
  git remote -v

# add new repo remote
  git remote add origin git@github.com:<github-username>/<new-repo-name>.git

# push your changes to new repo with commits
  git push -u origin --all

# add origin for projects
  git remote add origin git@github-COMPANY:<github-username>/<repository-name>.git

Comments