Git
Branchโ
git checkout -b <branchname>- create new branchgit checkout <branchname>- go to specific branchgit branch -a- list all branches including remotegit branch- list all local branches
Pushโ
git push --set-upstream origin <branchname>- when you have created a local branch and want to push the branch to remote
Commitโ
git reset --soft HEAD~1- undo last commit but local modifications remain in uncommitted state (i.e., your changes are still there)git reset --hard HEAD~1- undo last commit and remove local modifications (i.e., goodbye, changes)git commit --amend- edit last commit message (not yet pushed)- To amend pushed commit message:
- Do
git commit --amend - This will open up the editor. Modify the commit message, save and exit.
- Do
git push --force-with-lease
- Do
Rebaseโ
Scenario: Performed CommitA but forgot to include some files.
Solution:
- Perform CommitB by including all files needeed. There is no need to put fancy commit message, since we will use the previous commit message.
- Do
git rebase -i HEAD~2 - In the editor that will come out, indicate and save:
pick CommitA
fixup CommitB
Scenario: Created a branch from main, someone else pushed changes to main, how to get those changes to current working branch.
Solution:
- In working branch, stash all changes.
- Go to main branch and pull.
- Go to working branch and do
git rebase main. Do this only if no changes have been pushed yet. Otherwise, use force with lease.
Edit previously pushed changesโ
โฏ git commit -m 'f'
[<branchname> 66312e1] f
โฏ git rebase -i HEAD~2
Successfully rebased and updated refs/heads/<branchname>.
โฏ git log
โฏ git show e00a7a1
โฏ git push --force-with-lease
Diffโ
- Check diff between two branches how to compare two branchesโ๏ธ
git diff <branch1>..<branch2>- (two dots) compare "endpoints" of the two branchesgit diff <branch1>...<branch2>- (three dots) compare the "endpoint" of branch2 with the common ancestor (where branch2 was forked) of the 2 branchesgit diff --name-only- list filenames of modified files, including mode changesgit diff --name-only -G.- list filenames of modified files, ignoring mode changesgit diff --sumary- list filenames and mode changes (may include file modification)
Stashโ
git stash pop stash@{stash_index}- pop specific indexgit stash show -p stash@{stash_index} --name-only- show filenames of modified files in stash_indexgit stash show -p stash@{stash_index}-git stash push -m "with mods for db retention"- include a message/label during stashinggit stash apply- apply changes from latest stash stack, but do not remove from stack (not pop)git stash clear- remove everything from stashgit stash drop stash@{stash_index}- remove particular stash index from stashgit stash list --name-status- show list of stashes along with modified files and their status
Starting a new git repositoryโ
- Inside project directory, do
git init, then add and commit. The files will now be in a local repository. - In github, create a new repo. Then locally, do
git remote add origin git@github.com:<user>/<repo_name>.git
git branch -M main
git push -u origin main
Example Cherry-pickingโ
git logto get commit hashgit showto check which hash to revert togit revert <hash>git cherry-pick <hash>- modify files as needed
git addchanged filesgit cherry-pick --continueto continue cherry-picking
Example git mergeโ
- Original branch: master
git checkout -b <new_branch>- Do modifications and commit.
- Go to master:
git checkout master - Do merge:
git merge <new_branch> - If merge is okay, and
<new_branch>can be deleted, dogit branch -d <new_branch> - If there are conflicts, resolve them and ensure all have been resolved by finding
"<<<<"and">>>>". - Do
git commit. This will have a default commit message of "Merge branch<new_branch>".
Fetch vs Pullโ
Git Fetch gets all the changes from the remote repository to the local repository, without making any changes to the current working directory. This is done with git fetch then to merge the changes to the current directory, do git merge origin/<branch name>. Git Pull gets all the changes from the remote repository to the current working directory and automatically tries to merge or rebase them to the current working directory.
Deleting a branchโ
- To delete a local branch:
git branch -d <branch name> - To delete a remote branch:
git push origin -d <branch name>
Othersโ
git mv -f <source> <dest>- move or rename files in git, use-fif destination exists and you want to overwrite itgit restore <filename>- discard changes in working directory