Developers Notes

My Journey as Software Architect

Git CLI Cheatsheet

  • Basic git Init ( on your code root directory run )

git init

git commit -m "Initial Commit"

git remote add origin "<path to git url>"

git push -u origin master

  • Basic git push

git add .

git commit -m "<your commit text>"

git push origin master / git push origin <branch>

  • Checkout to branch

git checkout <branch name>

  • Pull commit from local master to branch

git checkout <branch name>

git fetch master

  • Pull commit from remote master to local branch

git checkout <branch name>

git pull origin master

  • Merge branch to master remote & optional commit

git checkout master

git merge <branch name>

git add .

git commit -m "Combine to Master"

git push

  • Git rebase from master

git checkout master
git pull --rebase
git checkout Mybranch
git rebase master
git push -f origin Mybranch

  • Delete local branch

git branch -D <branch name>

  • To Revert into previous commit

git checkout <test commit hash>

git revert <unwanted commit hash>

git checkout <current branch>

 

  • Remove file from already pushed repo

First commit any outstanding code changes, and then, run this command:

git rm -r --cached .

This removes any changed files from the index(staging area), then just run:

git add .

Commit it:

git commit -m ".gitignore is now working"
  • Delete remote branch

git push <remote_name> --delete <branch_name>

 

In some case, if you  have something like

-you have 6 commit on your local repo

-and you fall behind 4 commit from remote

-and you want to discard your commit, and pull from repo

 

you can use this command

 

to remove the last commit

git reset --hard HEAD^

to remove n number of last commit

git reset --hard HEAD~2

if you want to uncommit , but keep changes

git reset HEAD^

 

If you have some changes on current branch, and want to move it to new branch, run this command

git checkout -b FEATURE

 

Merge Branch Into Master

 

git branch

git branch new-branch

git checkout new-branch

 

...develop some code...

 

$ git add –A

$ git commit –m "Some commit message"

$ git checkout master
Switched to branch 'master'

$ git merge new-branch

 

Remove file from already commited remote

git rm --cached giant_file

 

Tools:

GitSheet
https://gitsheet.wtf/?ref=producthunt

 

References:

On undoing, fixing, or removing commits in git
https://sethrobertson.github.io/GitFixUm/fixup.html

git - Create new branch based on current branch to work on a new feature - Stack Overflow
https://stackoverflow.com/questions/46706821/create-new-branch-based-on-current-branch-to-work-on-a-new-feature
Git: Merge Branch into Master
https://stackabuse.com/git-merge-branch-into-master/
Removing files from a repository's history - GitHub Help
https://help.github.com/en/articles/removing-files-from-a-repositorys-history
How to completely remove a file from a Git repository | iText PDF
https://itextpdf.com/en/blog/technical-notes/how-completely-remove-file-git-repository
Removing Accidentally Committed Files From Remote History - DEV Community 👩‍💻👨‍💻
https://dev.to/jenninat0r/removing-accidentally-committed-files-from-remote-history-3acj