Skip to main content

Git

Branchโ€‹

  • git checkout -b <branchname> - create new branch
  • git checkout <branchname> - go to specific branch
  • git branch -a - list all branches including remote
  • git 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:
    1. Do git commit --amend
    2. This will open up the editor. Modify the commit message, save and exit.
    3. Do git push --force-with-lease

Rebaseโ€‹

Scenario: Performed CommitA but forgot to include some files.

Solution:

  1. Perform CommitB by including all files needeed. There is no need to put fancy commit message, since we will use the previous commit message.
  2. Do git rebase -i HEAD~2
  3. 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:

  1. In working branch, stash all changes.
  2. Go to main branch and pull.
  3. 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 branches
  • git diff <branch1>...<branch2> - (three dots) compare the "endpoint" of branch2 with the common ancestor (where branch2 was forked) of the 2 branches
  • git diff --name-only - list filenames of modified files, including mode changes
  • git diff --name-only -G. - list filenames of modified files, ignoring mode changes
  • git diff --sumary - list filenames and mode changes (may include file modification)

Stashโ€‹

  • git stash pop stash@{stash_index} - pop specific index
  • git stash show -p stash@{stash_index} --name-only - show filenames of modified files in stash_index
  • git stash show -p stash@{stash_index} -
  • git stash push -m "with mods for db retention" - include a message/label during stashing
  • git stash apply - apply changes from latest stash stack, but do not remove from stack (not pop)
  • git stash clear - remove everything from stash
  • git stash drop stash@{stash_index} - remove particular stash index from stash
  • git 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 log to get commit hash
  • git show to check which hash to revert to
  • git revert <hash>
  • git cherry-pick <hash>
  • modify files as needed
  • git add changed files
  • git cherry-pick --continue to 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, do git 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 -f if destination exists and you want to overwrite it
  • git restore <filename> - discard changes in working directory