Skip to content

Instantly share code, notes, and snippets.

@nareshganesan
Last active March 28, 2021 09:20
Show Gist options
  • Save nareshganesan/fb121ea0ea0328dede18 to your computer and use it in GitHub Desktop.
Save nareshganesan/fb121ea0ea0328dede18 to your computer and use it in GitHub Desktop.
# install git scm
sudo apt-get install git-core
# configure git user details , can be system wide, user specific or repo specific
# user name
git config --global user.name "John Doe"
# user email
git config --global user.email johndoe@example.com
# list all config details
git config --list
# Create a new git repo.
mkdir project-name; cd project-name
git init
# Alternately, Cloning a existing git repository.
git clone http://git-repo-url.git
# or
git clone git@github.com:org-name-or-username-name/project-name.git
# Yay, we have created a local git repo now.
# let see if we have something, we want to commit to repository.
git status
# List all git-ignored files
git ls-files . --ignored --exclude-standard --others
# List all untracked files
git ls-files . --exclude-standard --others
# you see a bunch of lines of result for the above command.
# Staging: A place where you stage your code before actually commiting your file to repository.
# Add new files to staging
git add path/filename
# add README.md & .gitignore
# Note: We can remove changes/files from staging area using the below command.
git reset path/filename
# or
git checkout path/filename
# Repo: Actual place where the files will be commited.
# Files are actually commited to local repository.
git commit -m "messgage for commit"
# The following notes are necessary to understand the next git command.
#Note: finding current branch name.
git branch
# Note: finding remote branches
git branch -r; # prints the remote branch list.
# Note: finding the remote server alias name
git remote -v # prints the remote-name
# We need to tell the remote repository that we have more incoming changes to be pushed.
git push remote-name remote-branch-name
# Yay, we have learned the basics.
# 1. Creating a project.
# 2. Checking status of project.
# 3. Checking the current local branch name.
# 4. Adding changes to staging area.
# i. removing changes or files from staging area.
# 5. Commiting files to local respository.
# 6. Pushing files from local respository to remote repository.
# you are working on a couple of features and so you have couple of branches for individual feature.
git checkout -b new-branch-name branch-name-to-branch-from
# git checkout -b test origin/test
# suddenly you have concept on the your other feature, but you have some un commited changes in your current feature branch.
# git stash
# stashing any un-finished changes for use later. Always name your stash for your reference.
git stash save --include-untracked "message relevant to un-finished changes"
# git checking out your other branch
git checkout branch-name
# Implement the new concept you thought for this feature. Make the changes.
git add path/filename
git push origin branch-name
# Now, we can switch back to the old branch we were initially working, and start from where we left.
# We can get our half baked changes we wrote for this feature from stash.
# checking our git stash.
# Lists all our stash list.
git stash list
# Find the correct stash corresponding to our current branch.
git stash apply stash@{0} # the stach number anything you want refer git stash list
# After applying the stash and committing it, we can delete the particular stash
# whatever stash reference number in your stash list.
git stash drop stash@{0}
# The above process will be really work wonders, when we are working alone. :)
# what if we are working on a team.
# Lets see how we can work in single repository with couple of other developers.
# Below diagram is confusing but mind you, it will be really helpful once you fully understand the concept.
# Note: https://camo.githubusercontent.com/f011896cab0a6e086954a10d3a5132d57ca69468/687474703a2f2f662e636c2e6c792f6974656d732f3369315a336e3154316b3339327231413351306d2f676974666c6f772d6d6f64656c2e3030312e706e67
# Note: https://gist.github.com/nareshganesan/46413289deae452df407
# GIT Pull request & merging
# Merging branches
# 1. https://git-scm.com/book/en/v2/Git-Branching-Basic-Branching-and-Merging
# 2. https://git-scm.com/book/en/v2/Git-Tools-Advanced-Merging#_advanced_merging
# Note: https://help.github.com/articles/using-pull-requests/
# Any merge should always be done using git pull request.
# https://github.github.com/training-kit/downloads/github-git-cheat-sheet.pdf
# Advanced concepts:
# Chaging GIT remote from https to ssh (assuming your remote is named 'origin')
function https_to_ssh {
REPO_URL=`git remote -v | grep -m1 '^origin' | sed -Ene's#.*(https://[^[:space:]]*).*#\1#p'`
if [ -z "$REPO_URL" ]; then
echo "-- ERROR: Could not identify Repo url."
echo " It is possible this repo is already using SSH instead of HTTPS."
exit
fi
USER=`echo $REPO_URL | sed -Ene's#https://github.com/([^/]*)/(.*).git#\1#p'`
if [ -z "$USER" ]; then
echo "-- ERROR: Could not identify User."
exit
fi
REPO=`echo $REPO_URL | sed -Ene's#https://github.com/([^/]*)/(.*).git#\2#p'`
if [ -z "$REPO" ]; then
echo "-- ERROR: Could not identify Repo."
exit
fi
NEW_URL="git@github.com:$USER/$REPO.git"
echo "Changing repo url from "
echo " '$REPO_URL'"
echo " to "
echo " '$NEW_URL'"
echo ""
git remote set-url origin ${NEW_URL}
echo "Success"
}
# git log pretty format with user and date
git log --pretty=format:"%h%x09%an%x09%ad%x09%s"
# git access token credential store for linux
# https://git-scm.com/book/en/v2/Git-Tools-Credential-Storage
git config --global credential.helper 'store --file ~/.git-credentials'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment