git init # initialize directory as git repository
git clone curl> # copying git repository from hosted url to local machine
git status # show modified files in current directory
git log # view your commit history
git add -A # add changed files into your next commit (stage)
git commit -m •your message' # commit your changes
git pull origin main # get up-to-date changes from the main branch
git push origin main # push your changes to the main branch
git branch # list all local branches on the machine
git merge develop # will merge develop branch into the current
git switch # To switch between the branches
git branch <branch-narne> # will create new branch
git checkout B # switch from current to B
git branch -m B # will rename current branch to B
git branch -d A # delete specified branch(A)
git rm # remove file from project and stage removal
git stash # save modified and staged changes
git stash list #It gives the list of stashed code, we get stash-id from it
git stash apply # It will apply all those changes in your code, but it will not delete it from stash list
git stash drop #It will remove the stash code of that stash-id
git stash pop #It will bring all those changes in your code and delete it from stash list
git stash clear #It will remove all stash code from the stash list NOTE: GIT uses SHA1 algo for hashing the files
git diff A B # show what is on A but not on B
git rebase <main> # put commits of current branch ahead of main
git revert # It will undo all changes made by that specific commit
git reset # It will remove all the changes made by commits done after that commit id.
- git reset head –hard It will reset all commits and have changes that master has
git remote –v # Gives the repo link
pip3 install git-sim
to visualise git cmds.
Squashing the Commits
• git rebase -i HEAD ~
• Then pick the commit you want, and rest will be squash
• Then keep only commit message you want
• git push origin --force
Resolving merge conflicts/ Rebase branch (2 ways)
• Method 1
o git pull --rebase origin master
• Method 2
o git fetch –all
o git rebase origin/master
• Below steps are same for both method
o Resolve merge conflicts and save the file
o git add .
o git rebase --continue
• If still merge conflicts are coming resolve them and continue git add . and git rebase --continue, till all merge conflicts are resolved o git rebase –abort To abort the rebasing
o git push origin --force Pro tip: Squash all commits, so that you can resolve merge conflicts in one go.
• It is used when we want to apply commit changes from one branch to another branch.
• It is used for the bug fixes, where you want to apply that commit to fix the bug in multiple branches
• It used to duplicate the commit from one branch to another
• In one of the branch A, one commit is made to fix a bug
• Now in another branch B, you want that commit.
• With help of git log, get the commit id from Branch A, copy it
• Now go to branch B, in which you want that commit changes from A branch.
• git cherry-pick • It will commit and apply changes on branch B, from that commit.
• If you would like to make some further modifications,
• you can also instruct Git to only add the commit's changes to your Working Copy - without directly committing them:
• git cherry-pick --no-commit
Squahing all commits
Remember that there's always risk in doing things like this, and its never a bad idea to create a save branch before starting.
Start by logging
git log --oneline
Scroll to first commit, copy SHA
git reset --soft <#sha#>
Replace <#sha#> w/ the SHA copied from the log
Make sure everything's green, otherwise run git add -A
git commit --amend
Amend all current changes to current first commit