Search

26 February, 2018

GIT Essential Tips


GIT has become one of those tools in e business that you can't ignore anymore. So I suggest you learn to use the basics of the idea called GIT. For my tester friends, as you enter the automation phase, git is going to come around sooner or later. So watch out.



Important everyday command with GIT

1. How to revert to an old commit both on local and remote

git reset --hard HEAD~1

git push -f origin master

2. How to get all branches of a git project

git fetch --all

3. How to delete a branch from local computer

git branch -d name

4. How to delete a remote branch

git push origin --delete branch

5. How to shift changes from one branch to another

git stash
git checkout branch
git stash pop

6. How to create a remote branch from a new local branch

git push -u origin new_branch

Note: -u means, --set-upstream. Refer this.

7. How to see all the existing remote branches

git branch -r

8. How to see all branches (remote & local) 

 git branch -a

9. How to fetch a specific remote branch

This involves multiple steps. Do as follows.

1) First get a list of all remote branches (git fetch -r)
2) Create a branch of that exact remote branch name (git checkout same_branch_name as remote)

10. How to see the diff between two branches

git diff branch_name


11. How to see the diff with names of files only (no details )between two branches

git diff branch_name --name-only


12. How to see last commit details?

git show

13. How to see who changed a file?

git blame filename

14. How to undo a merge?

git merge --abort

15. How to see, what files were changed or created in the last commit ?

git log -p -1 --name-only

16. How to see, what files were changed in a specific commit?

git show --name-only commit_number

(e.g 6021acc151c2b02c364af5fef2bfbbe73c880d38)


17. Remove last commit from branch.

git reset --hard HEAD

18. Remove last to last commit from branch.

git reset --hard HEAD^

19. Make the branch base same as latest master.

git rebase master

20. Retrieve deleted branch from remote

git fetch origin BranchName


21. Force checkout (switch branch ignoring all changes).

git checkout -f other_Branch


22. Solve Merge Conflict (accept your local content or remote)

git checkout --ours filename # For accepting local changes
git checkout --theirs filename # For accepting remote changes

23. Save work without committing and switch to another branch.

In general, if you want to change branch when you have uncommitted changes, git doesn't allow you to do so. For that , you can use something called "stash".

git stash

To recover the stashed files, use :

git stash pop

To drop the stash (you don't want these changes):

git stash drop


24. Merge a specific commit from one branch to another.
 
Sometimes , it might happen that, we only need a SPECIFIC COMMIT from a branch into another branch. Although, this is not a good thing to do, but GIT makes it possible .


Steps:

Find the hash_number for the commit in source branch. (You can use "git log" ) .
Switch to target branch ( Use "git checkout target_branch" ) .
git cherry-pick hash_number .


25. How to create a branch and switch to it in a single step.

git checkout -b your_branch

26. How to see all the git configs set.

git config --list

27. How to see if you need to rebase your branch before pushing changes?

When you created your working local branch from master, you were at the same base commit as master. Now after a few day, probably the master branch has moved ahead of you since others might have pushed new commits into it while you were working .

-----master------------------------my_branch....

After a few day's,


Day 6 --------master--------------------------------------
Day 5 -----commit-new----------------------------------------
Day 4 -----commit-new----------------------------------------
Day 3 -----commit-new----------------------------------------
Day 2 -----commit-new----------------------------------------
Day 1 -----master------------------------my_branch....

A quick way to do it:

$hash1=$(git show-ref --heads -s master)

$hash2=$(git merge-base master add-inventory-arindam)


$ [ "${hash1}" = "${hash2}" ] && echo "OK" || echo "Rebase is required"


28. How to see what changed after you pulled in from remote and saw some files changed?

git diff branch_name@{1} branch_name file_name
Note the {1}

Another way to do it , is use the commits message that looks like :
Updating 647a02393..08c607cff


29. Diff after file has been added to staged area?

If you execute "git diff" after adding your file to the staged area (after git add command), you will not see and results. In order to see the changes made, you need to execute:

git diff --staged filename

30. Move a file from staged area to unstaged area.

If you decided to not commit a file, AFTER you have already moved the file to staged area (by git add filename), you can do so:

git reset HEAD filename

31. Rename a local branch.

If you want to change your local branch name, it is possible.

git branch -m new_name.

32. See what is in the stash.

Sometimes we just want to know, what's in the stash before we pop it out. This can be helpful in many situations.

git stash show
git stash list

To see the diff too,

git show stash


33. Change the commit message for the last (not published) commit in local branch.

git commit --amend -m new_message

#This will change the message of the last commit.

34. Copy a file from one branch to another branch.

While you are in the branch in which you want the file to be.

git checkout source_branch somefile.py

35. Rename a file

git mv file_oldname file_newname


36. Find branch with pattern

git branch --list '*fix_*'      # Search in local branches

                git branch --list -a '*fix_*'  # Search in remote & local


11 February, 2018

Python Decorators with simple examples


Everyone who has used python , has sooner or later come across decorators . That's that thing sitting on a function which magically does something special . It looks like :


@mydecorator
def my_function


Today we are going to learn , how to write decorators of our own . How this works: