Tuesday 13 August 2019

Git for Beginners and Intermediate level

Yeah, we have seen this title in lot many other posts. Trust me, all are same. Yet different in their approach trying to share the knowledge in most simplistic and summarised manner.


courtesy: git

I could have shown all the following commands with a nice black background and explaining each command, but this is not about learning git.
This is all about knowing the things that are surely needed in your daily git interactions. 

How am I sure about it? 

Cause I have gone through them in the beginning of my career. 
And ever since, I was able to handle all my daily transactions smoother.
No need to depend on any IDE, SourceTree or anything else.

What makes it so special?

Following are the list of git commands, like my own cheat sheet grouped together while going through git transactions happening in real time environment. Total age of this document is 8 years and it is still getting updated.

Tip: Be patient, try each unknown command. Read till the end to find the best things hidden.


git init — initializes git repo in your local dir
 git remote add origin 
https://github.com/username/project.git — creates a remote which you use to contact with source repo
 git pull origin master — gets data from master branch of your source
 git pull — gets data from all branches of your sources.

For Submodules

$ git submodule update — init
 $ git submodule foreach git checkout -t origin/develop
 $ git checkout -t origin/develop
 $ git submodule foreach pull/push origin <branchName>

To configure Git

git config — global user.name “Your Name Here”
 git config — global user.email “your_email@example.com”

To Edit git config file

git config — global — edit

For Branching

git branch <branchname> — creates a branch
 git checkout <branchname> — switches you to this branch
 git checkout -b <branchname> — creates and Switched to the new branch
git status — To know about the status of your local repo.
 git add -a — To add everything from workspace
git commit -m “Initial commit”
 or git commit -am “Initial Commit”
 git commit — amend — to push with last commit message

# If you want to edit last push commit message (Strictly not to be tried on other's repo)

 git commit — amend -m “new commit message”
git push origin +<branchname>:<branchname>
git push -u origin international
 git push
 git branch -d <branchname> — deletes a branch locally.
 git push origin :<branchname> — deletes a remote branch
 git pull origin <branchname>
 git merge <branchname> — merges branch with master
git rev-parse HEAD — To know the current HEAD

# If you want to get some specific commit from pull requests or any other branches, do cherry-pick:

 git cherry-pick abc0123


# If you need to do multiple cherry-pick, then start from old to new.

git cherry-pick a
git cherry-pick b
git cherry-pick c
git cherry-pick d
...

# For RPC Error

if error: RPC failed; result=22, HTTP code = 411
 The default file post size for Git has been exceeded.

 git config http.postBuffer 524288000 [set to 500 MB]


git branch -m old-branch-name new-branch-name — rename local branch
 git branch -m <new name> — rename the current branch
 git push origin :old-branch-name — delete remote branch with old name
 git push origin new-branch-name — create remote renamed branch

For Tags

 git tag -a <tagname> -m “tag message”
 git commit -am “Initial Commit”
 git push origin <tagname>
 git pull origin <tagname>
 git show <tagname>
 git tag -d <tagname>

# Delete a tag in cloud

 git tag -d 12345
 git push origin :refs/tags/12345

FOR UTILITIES:

 git status
 git log
 git remote (-v) show
 git branch (-v)
 git mv oldname newname
 git remote rm <remote name> — to delete a remote

GIT TRICKS:

 git tag -a v3.2.9 -m “updating version to 3.2.9” — to apply a tag for current head
 git push origin — tags — to push tags to current branch.
 git merge — abort — To Stop merging
 git reset — hard master@{10} — Revert to the tenth commit in the list
 git checkout -b branchX master@{‘one month ago’} — Start a new branch from the commit that the master pointed to one month back.
 git reset — hard 60510a. — Jump to specific checkout

SOFT RESET:

git reset — soft HEAD^ — To revert last commit.The above undo the last commit and keeps all the changes you made.
Means, you could still see all the changes made in that commit.
Use case: You dont want this commit in that branch, but the changes you made in the last commit are needed for another branch/feature.

HARD RESET:

git reset — hard HEAD^ — To undo all your local changes.The above undo the last commit and deletes all the changes you made.
Means, you can’t see the changes anymore and you can’t get them back.
Use case: You don’t want this commit in that branch, and the changes you made are not needed anymore.
Note: Check twice before applying ‘reset’ with ‘hard’. You can’t undo it once done.

git stash — To make changes saved and place aside while pulling or pushing
 git pull
 git stash pop — To apply the saved changes after you pull from Source repo.

REMOVING REMOTE

 git remote -v
 # View current remotes
# origin git@github.com:user/repo.git (fetch)
 # origin git@github.com:user/repo.git (push)
 # destination git@github.com:forker/repo.git (fetch)
 # destination git@github.com:forker/repo.git (push)
 git remote rm destination
 # Remove remote
git remote -v
 # Verify removal
# origin git@github.com:user/repo.git (fetch)
 # origin git@github.com:user/repo.git (push)

# To work with multiple accounts of git or github

#to delete a commit from source (Github)

 #http://stackoverflow.com/questions/10817906/delete-a-commit-from-github
 git push -f origin HEAD^:master

# Git cherrypick multiple commits

# Git archive or export zip

 git archive HEAD — format=zip > archive.zip
 git archive HEAD | gzip > archive.tar.gz

# To delete every branch except the one that you currently have checked out:

 for b in `git branch | grep -v \*`; do git branch -D $b; done

# To delete all other branch except ‘master’

 git branch | grep -ve “ master$” | xargs git branch -D

# To delete all the local branches except ‘master’

 git branch | grep -v “main” | xargs git branch -D

# Use orphan branch

 git checkout — orphan <your new branch>
 git rm -rf .
 <do your work>
 git add your files
 git commit -m ‘Initial commit’



Note: Feel free to ask if you are stuck anywhere.
 ** Please try to experiment with your sample repositories before you work with real code repo’s.

-Anilrajkumar Battini

PS:Be careful with anything that deletes, Double check before pressing return.