Search This Blog

Tuesday, March 26, 2019

Git Cheat Sheet – Git Commands Testers Should Know

This post is a Git Cheat Sheet with the most common Git commands you will likely use on a daily basis.
If you are a technical tester working alongside developers, you should be familiar with the basic Git commands.
This post contains enough Git knowledge to get you going on a day to day basis as a QA.

Initial Git Setup

Initialize a repo

Create an empty git repo or re-initialize an existing one
$ git init

Clone a repo

Clone the foo repo into a new directory called foo:
$ git clone https://github.com//foo.git foo

Git Branch

How to Create a New Branch in Git

When you want to work on a new feature, you typically create a new branch in Git. As such, you generally want to stay off the master branch and work on your own feature branches so that master is always clean and you can create new branches from it.
To create a new branch use:
$ git checkout -b 

How to List Branches in Git

If you want to know what branches are available in your working directory, then use:
$ git branch
Example output
develop
my_feature
master

How to Switch Branches in Git

When you create a new branch then Git automatically switches to the new branch.
If you have multiple branches, then you can easily switch between branches with git checkout:
$ git checkout master
$ git checkout develop
$ git checkout my_feature

How to Delete Branches in Git

To delete a local branch:
$ git branch -d 
Use the -D option flag to force it.
To delete a remote branch on origin:
$ git push origin :

Git Staging

To stage a file is simply to prepare it for a commit. When you add or modify some files, you need to stage those changes into “the staging area.” Think of staging as a box where you put things in before shoving it under your bed, where your bed is a repository of boxes you’ve previously have shoved in.

Git Stage Files

To stage or simply add files, you need to use git add command. You can stage individual files:
$ git add foo.js
or all files at once:
$ git add .

Git Unstage Changes

If you want to remove a certain file from the stage:
$ git reset HEAD foo.js
Or remove all staged files:
$ git reset HEAD .
You can also create an alias for a command and then use it with Git:
$ git config --global alias.unstage 'reset HEAD'
$ git unstage .

Git Status

If you want to see what files have been created, modified or deleted, Git status will show you a report.
$ git status

Git Commits

It is a good practice to commit often. You can always squash down your commits before a push. Before you commit your changes, you need to stage them.
The commit command requires a -m option which specifies the commit message.
You can commit your changes like:
$ git commit -m "Updated README"

Undoing Commits

The following command will undo your most recent commit and put those changes back into staging, so you don’t lose any work:
$ git reset --soft HEAD~1
To completely delete the commit and throw away any changes use:
$ git reset --hard HEAD~1

Squashing Commits

Let’s say you have 4 commits, but you haven’t pushed anything yet and you want to put everything into one commit, then you can use:
$ git rebase -i HEAD~4
The HEAD~4 refers to the last four commits.
The -i option opens an interactive text file.
You’ll see the word “pick” to the left of each commit. Leave the one at the top alone and replace all the others with “s” for squash, save and close the file.
Then another interactive window opens where you can update your commit messages into one new commit message.

Git Push

After you have committed your changes, next is to push to a remote repository.

First Push

Push a local branch for the first time:
$ git push --set-upstream origin 
After that, then you can just use
$ git push

Push local branch to different remote branch

To push a local branch to a different remote branch, you can use:
$ git push origin :

Undo Last Push

If you have to undo your last push, you can use:
$ git reset --hard HEAD~1 && git push -f origin master

Git Fetch

When you use git fetch, Git doesn’t merge other commits them with your current branch. This is particularly useful if you need to keep your repository up to date, but are working on something that might break if you update your files.
To integrate the commits into your master branch, you use merge.

Fetch changes from upstream:

$ git fetch upstream

Git Pull

Pulling is just doing a fetch followed by a merge. When you use git pull, Git automatically merges other commits without letting you review them first. If you don’t closely manage your branches, you may run into frequent conflicts.

Pull a branch

If you have a branch called my_feature and you want to pull that branch, you can use:
$ git pull origin/my_feature

Pull everything

Or, if you want to pull everything and all other branches
$ git pull

Git Merging and Rebasing

When you run git merge, your HEAD branch will generate a new commit, preserving the ancestry of each commit history.
The rebase re-writes the changes of one branch onto another without creating a new commit.

Merge Master Branch to Feature Branch

$ git checkout my_feature
$ git merge master
Or with rebase option, you use:
$ git checkout my_feature
$ git rebase master

Merge Feature Branch to Master Branch

$ git checkout master
$ git merge my_feature

Git Stash

Sometimes you make changes on a branch, and you want to switch to another branch, but you don’t want to lose your changes.
You can stash your changes. Here’s how you do a stash in Git:
$ git stash
Now, if you want to unstash those changes and bring them back into your working directory use:
$ git stash pop

Thursday, September 27, 2018

Vim Cheat Sheet

Basics

:e filenameOpen filename for edition
:wSave file
:qExit Vim
:q!Quit without saving
:xWrite file (if changes has been made) and exit
:sav filenameSaves file as filename
.Repeats the last change made in normal mode
5.Repeats 5 times the last change made in normal mode

Moving in the file

k or Up Arrowmove the cursor up one line
j or Down Arrowmove the cursor down one line
emove the cursor to the end of the word
bmove the cursor to the begining of the word
0move the cursor to the begining of the line
Gmove the cursor to the end of the file
ggmove the cursor to the begining of the file
Lmove the cursor to the bottom of the screen
:59move cursor to line 59. Replace 59 by the desired line number.
20|move cursor to column 20.
%Move cursor to matching parenthesis
[[Jump to function start
[{Jump to block start

Cut, copy & paste

yCopy the selected text to clipboard
pPaste clipboard contents
ddCut current line
ywYank word
yyCopy current line
y$Copy to end of line
DCut to end of line

Search

/wordSearch word from top to bottom
?wordSearch word from bottom to top
*Search the word under cursor
/\cstringSearch STRING or string, case insensitive
/jo[ha]nSearch john or joan
/\< theSearch the, theatre or then
/the\>Search the or breathe
/\< the\>Search the
/\< ¦.\>Search all words of 4 letters
/\/Search fred but not alfred or frederick
/fred\|joeSearch fred or joe
/\<\d\d\d\d\>Search exactly 4 digits
/^\n\{3}Find 3 empty lines
:bufdo /searchstr/Search in all open files
bufdo %s/something/somethingelse/gSearch something in all the open buffers and replace it with somethingelse

Replace

:%s/old/new/gReplace all occurences of old by new in file
:%s/onward/forward/giReplace onward by forward, case unsensitive
:%s/old/new/gcReplace all occurences with confirmation
:2,35s/old/new/gReplace all occurences between lines 2 and 35
:5,$s/old/new/gReplace all occurences from line 5 to EOF
:%s/^/hello/gReplace the begining of each line by hello
:%s/$/Harry/gReplace the end of each line by Harry
:%s/onward/forward/giReplace onward by forward, case unsensitive
:%s/ *$//gDelete all white spaces
:g/string/dDelete all lines containing string
:v/string/dDelete all lines containing which didn’t contain string
:s/Bill/Steve/Replace the first occurence of Bill by Steve in current line
:s/Bill/Steve/gReplace Bill by Steve in current line
:%s/Bill/Steve/gReplace Bill by Steve in all the file
:%s/^M//gDelete DOS carriage returns (^M)
:%s/\r/\r/gTransform DOS carriage returns in returns
:%s#<[^>]\+>##gDelete HTML tags but keeps text
:%s/^\(.*\)\n\1$/\1/Delete lines which appears twice
Ctrl+aIncrement number under the cursor
Ctrl+xDecrement number under cursor
ggVGg?Change text to Rot13

Case

VuLowercase line
VUUppercase line
g~~Invert case
vEUSwitch word to uppercase
vE~Modify word case
ggguGSet all text to lowercase
gggUGSet all text to uppercase
:set ignorecaseIgnore case in searches
:set smartcaseIgnore case in searches excepted if an uppercase letter is used
:%s/\<./\u&/gSets first letter of each word to uppercase
:%s/\<./\l&/gSets first letter of each word to lowercase
:%s/.*/\u&Sets first letter of each line to uppercase
:%s/.*/\l&Sets first letter of each line to lowercase

Read/Write files

:1,10 w outfileSaves lines 1 to 10 in outfile
:1,10 w >> outfileAppends lines 1 to 10 to outfile
:r infileInsert the content of infile
:23r infileInsert the content of infile under line 23

File explorer

:e .Open integrated file explorer
:SexSplit window and open integrated file explorer
:Sex!Same as :Sex but split window vertically
:browse eGraphical file explorer
:lsList buffers
:cd ..Move to parent directory
:argsList files
:args *.phpOpen file list
:grep expression *.phpReturns a list of .php files contening expression
gfOpen file name under cursor

Interact with Unix

:!pwdExecute the pwd unix command, then returns to Vi
!!pwdExecute the pwd unix command and insert output in file
:shTemporary returns to Unix
$exitRetourns to Vi

Alignment

:%!fmtAlign all lines
!}fmtAlign all lines at the current position
5!!fmtAlign the next 5 lines

Tabs/Windows

:tabnewCreates a new tab
gtShow next tab
:tabfirstShow first tab
:tablastShow last tab
:tabm n(position)Rearrange tabs
:tabdo %s/foo/bar/gExecute a command in all tabs
:tab ballPuts all open files in tabs
:new abc.txtEdit abc.txt in new window

Window spliting

:e filenameEdit filename in current window
:split filenameSplit the window and open filename
ctrl-w up arrowPuts cursor in top window
ctrl-w ctrl-wPuts cursor in next window
ctrl-w_Maximize current window vertically
ctrl-w|Maximize current window horizontally
ctrl-w=Gives the same size to all windows
10 ctrl-w+Add 10 lines to current window
:vsplit fileSplit window vertically
:sview fileSame as :split in readonly mode
:hideClose current window
:­nlyClose all windows, excepted current
:b 2Open #2 in this window

Auto-completion

Ctrl+n Ctrl+p (in insert mode)Complete word
Ctrl+x Ctrl+lComplete line
:set dictionary=dictDefine dict as a dictionnary
Ctrl+x Ctrl+kComplete with dictionnary

Marks

m {a-z}Marks current position as {a-z}
' {a-z}Move to position {a-z}
''Move to previous position

Abbreviations

:ab mail mail@provider.orgDefine mail as abbreviation of mail@provider.org

Text indent

:set autoindentTurn on auto-indent
:set smartindentTurn on intelligent auto-indent
:set shiftwidth=4Defines 4 spaces as indent size
ctrl-t, ctrl-dIndent/un-indent in insert mode
>>Indent
<<Un-indent
=%Indent the code between parenthesis
1GVG=Indent the whole file

Syntax highlighting

:syntax onTurn on syntax highlighting
:syntax offTurn off syntax highlighting
:set syntax=perlForce syntax highlighting

My Profile

My photo
can be reached at 09916017317