Skip to content

Instantly share code, notes, and snippets.

@cheenamalhotra
Created September 16, 2019 05:52
Show Gist options
  • Save cheenamalhotra/881c639ffb94001e7ce85542dfd62f0b to your computer and use it in GitHub Desktop.
Save cheenamalhotra/881c639ffb94001e7ce85542dfd62f0b to your computer and use it in GitHub Desktop.
List of useful git commands when working with branches and forks

Summary of most useful 'git' commands

Setting up name and e-mail address

git config --global user.name "First Last"
git config --global user.email "myname@org.com"

Create a repository

git init

Output:

$ git init
Initialized empty Git repository in /Users/cheenam/GitSamples/hello/.git/

Adding files to the repository

git add file.txt 
git commit -m "First Commit"

Output:

$ git add file.txt 
$ git commit -m "First Commit"
[master (root-commit) f0ab4d3] First commit
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 file.txt

Check 'status' of the repository

git status

Output:

$ git status
On branch master
nothing to commit, working tree clean

Update file.txt with some content and perform status check

Output:

$ git status
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   file.txt

no changes added to commit (use "git add" and/or "git commit -a")

Staging Changes

git add file.txt

Output:

$ git add file.txt
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        modified:   file.txt

Committing staged changes

git commit -m "Changes for file.txt"

View history

git log

One line history

git log --pretty=oneline

Control what to see in logs

git log --pretty=oneline --max-count=2
git log --pretty=oneline --since='5 minutes ago'
git log --pretty=oneline --until='5 minutes ago'
git log --pretty=oneline --author=<your name>
git log --pretty=oneline --all

Ultimate log format

git log --pretty=format:"%h %ad | %s%d [%an]" --graph --date=short

Working with Git Repositories

Cloning a git repository

git clone https://github.com/<account>/<repository>

Adding remotes

git remote add upstream https://github.com/<account>/<repository>

Ideal git environment

git clone https://github.com/<my_account>/<my_forked_repository>
git remote add upstream https://github.com/<master_org>/<my_parent_repository>
git remote add xyzaccount https://github.com/<xyzaccount>/<repository_name>

Verify Remotes

git remote
git remote -v

Fetch remote

git fetch <remote>

Pushing to Remotes

git push origin master

Inspect a Remote

git remote show origin

Renaming Remotes

git remote rename <current_name> <new_name>

Remove Remote

git remote remove <remote_name>

List Tags

git tag
git tag -l "v1.*"

Create Tag (Lightweight)

git tag <tag_name>

Create Tag (Annotated)

git tag -a <tag_name> -m <tag_description>

Deleting Tag

git tag -d <tag_name>
git tag -delete <tag_name>

Deleting Remote Tag

git push <remote> --d <tag_name>
git push <remote> --delete <tag_name>

Checkout Tag on HEAD

git checkout <tag_name>

Checkout Tag in a Branch

git checkout -b <branch_name> <tag_name>

Push/Share Tag to Remotes

git push <remote> --tags

Creating a branch

git branch testing

Switching branches

git checkout testing

Push branch to remote

git push <remote> <branch_name>

Tracking Branches

git checkout --track <remote>/<branch_name>

Fetch branches and view all tracking branches

git fetch --all
git branch -vv

Deleting remote branches

git push <remote> --delete <branch_name>

Rebasing current_branch to master

git checkout <current_branch>
git rebase <master>
-- All new changes from <current_branch> will get copied (duplicate changesets) to <master> with a new <current_branch> HEAD
-- Useful for generating cleaner histories in running branches and ensuring branch commits are always ahead of 'master'.

Merging current_branch to master

git checkout <master>
git merge <current_branch>
git commit -m "My Merged commit"
-- All new changes from <current_branch> will be merged (using same changesets) to <master> without changing <current_branch> HEAD

Squash and Merge current_branch to master (Recommended)

git checkout <master>
git merge --squash <current_branch>
git commit -m "My Squashed commit"
-- All new changes from <current_branch> will be merged in a single commit to <master> wuthout changing <current_branch> HEAD
-- Always recommended when merging a big change to 'master'

Git Workflows

Refer here: https://git-scm.com/docs/gitworkflows for full documentation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment