Skip to content

How 2 Git

0xhelloworld-dev edited this page Nov 29, 2023 · 1 revision

Creating and Switching Branches

List branches:

  • git branch

Create new branch:

  • git branch <new-branchname>

Switch to new branch:

  • git checkout <new-branchname>

Save Branch Changes:

  • git stash

Pull remote branch:

  • git checkout <branchyouwanttoPull>
  • git pull

Modifying the branch:

Check the changes of the branch:

  • git status

Stage changes:

  • git add <filenames>

Commit changes:

  • git commit -m “<message>”

Push changes to branch:

  • git push -u origin <branch-name>

Save current state of branch:

  • git stash

Rebasing

Appends current branch commits to the newest version of main:

  • git checkout -b jonathan-branch
  • git rebase -i main
  • git stash temporarily shelves (or stashes) changes you've made to your working copy so you can work on something else, and then come back and re-apply them later on. Stashing is handy if you need to quickly switch context and work on something else, but you're mid-way through a code change and aren't quite ready to commit.
  • By default git stash will only stash changes that have been staged for commit -
    • will not stash untracked files/new files that have never been staged
  • Stash uncommitted changes with context:
    • git stash save "message goes here"
  • Include untracked files when stashing:
    • git stash -u
  • Save specific file to stash
    • git add path/to/file/to/stash
    • git stash -m "message goes here" path/to/file/to/stash
  • List stashes
    • git stash list
  • Remove from stash from list and apply stash to branch
    • git stash pop
    • Apply specific stash
      • git stash pop stash@{2}
  • Apply stash to branch, still keep in stash
    • git stash apply

Rebasing workflow

The following will append changes in jonathan-branch to the master branch.

  1. Ensure local master is in sync with remote master
    • git checkout master
    • git pull
  2. Rebase feature branch against master
    • git checkout jonathan-branch
    • git rebase -i master
  3. Resolve Conflicts
  4. Push changes to PR
    • git push -u origin jonathan-branch
    • git push -f origin jonathan-branch Compare local branch to remote branch:
    • git diff jonathan-branch origin/jonathan-branch

Revert to commit:

  • git log
  • git reset --hard b42a84f3aef5caff5be821347da89238d3ef1f9f