git

Git Stash Commands

Most common commands that I use. Self-explanatory.

Save and List Stash

  • git stash save “Some message”
  • git stash list # stash is available to all branches.
  • git stash show stash@{0}
  • git stash show -p stash@{0} #detailed view

Apply stash

  • git stash pop stash@{0}
  • git stash apply stash@{0}

Delete Stash

  • git stash drop stash@{0}
  • git stash clear
git

.gitignore file

Ignoring files in git using .gitignore
======================================
create a .gitignore file and put it in the root of a git project.

Typically ignore:
-Compiled source code
-Packages and compressed files
-logs and databases that changes very often.
-OS system generated files.
-images, pdfs, videos

Example of a common .gitignore file from github.

# Compiled source #
###################
*.com
*.class
*.dll
*.exe
*.o
*.so

# Packages #
############
# it’s better to unpack these files and commit the raw source
# git has its own built in compression methods
*.7z
*.dmg
*.gz
*.iso
*.jar
*.rar
*.tar
*.zip

# Logs and databases #
######################
*.log
*.sql
*.sqlite

# OS generated files #
######################
.DS_Store
.DS_Store?
._*
.Spotlight-V100
.Trashes
ehthumbs.db
Thumbs.db

 

Visit https://github.com/github/gitignore for great info.

git

Changes to Git – Amend

1. Undo changes in the working directory
git checkout — unmodifiedFileName # — implies that we are not checking out a branch. Stay on the current branch and check out the unmodified file.

2. Undo changes in the staging area
git reset HEAD –stagedFileName

3. Undoing commits
— git can edit only the last commit with the amend command.
git commit –amend -m “message”

git commit –amend -m “we can also just update the last commit message”

git

Most Common Git Commands

git branch -b new_feature
git branch –no-merged # show the branches that are not merged yet.
git merge –no-ff new-feature -m “merge new-feature branch”
git branch -D new-feature # delete the working branch after merge is completed

 

Working with GitHub

create a GitHub repo hello-remote

git clone <URL for hello-remote>
git clone https://github.com/hydrogeologist/hello-remote.git
git status

add a demo file in local clone, demo.txt

git add demo.txt
git commit -m “added a demo file”

git push -u origin master # now upload the file to the GitHub.

git fetch origin # use this to update the local clone of the remote repository. This let us know how many commits have been made since the last pull
git remote -v # shows the branch on the remote location.

git pull # use this command to update the local repo. (Behind the scenes, git pull is actually a git fetch followed by a git merge)

Coll flag for git diff:

git diff –color-words

Try this feature.
git branch –merged : shows all of the branches that are merged.

Take a look at the diff between branches:
git diff master..branch_name

Rename a branch in Git
git branch -m old_branch_name new_branch_name
git branch –move old_branch_name new_branch_name

Delete Branches
git branch -d branch_name
git branch –delete branch_name

if there are changes in the branch that is not merged yet, we must use D
git branch -D branch_name # forced delete

git

Viewing Git Logs

Some common git log comments that I use heavily.

# view git logs
git log

#limit number of logs to show
git log -n 1 # last commit
git log -n 5 # last 5 commits
git log –since=2017-06-01
git log –until=2017-06-01

git log –author=”Ankan Basu”

git log –grep=”init” # look for changes in the commit message
git log –grep=”bug” # log for commit messages with bug fixes.

 

git log –oneline

git log –format=oneline

git log –graph

git log –oneline –graph –all –decorate

git

Sample initial git set up commands

This is a sample initial git set up commands I use for my projects.

git config –system
git config –global
git config # this is for project basis configuration.

 

#Configure initial set up
git config –global user.name “Ankan Basu”
git config –global user.email “email@gmail.com”

# check configuration
git config –list

# set up the default text editor for git
git config –global core.editor “vim”

# use colors with git
git config –global color.ui true