Create Branch: git branch <new-branch-name>
View all branches: git branch -a
Switch Branch: git checkout <new-branch-name>
Create and Switch to a new branch: git checkout -b new_branch
Rename current branch: git branch -m <new-branch-name>
Fetch a Remote Branch into an Existing Local Repo
1.) git fetch origin <branch-name> (fetch the branch)
2.)git checkout -b <branch-name> origin/<branch-name> (create and checkout the branch locally)
3.)git branch (view the new local branch now on your machine)
Delete Branch:
1.) git checkout [base-branch]
2.) git branch -d [branch-to-delete] (local)
3.) git push origin --delete [branch-to-delete](remote)
Merge branch to main:
1.) git checkout main
2.) git merge feature-a (merge)
Commit to your branch:
1.) git add .
2.)git commit -m "commit message"
3.) git push -u origin my-feature-branch
Removes all references to branches that have been deleted from the remote repository.
1.) git remote prune origin
Cleans Working directory [Ensure that no untracked files are present in your working directory]
1.) git clean -fd
Show commit history: git log --oneline
Incorporate your feature branch changes into development
1.) git pull development
2.) git branch feature-branch
3.) Make some changes to your feature branch
4.) git add .
5.) git commit -m "added some changes"
6.) git push -u origin feature-branch
7.) git checkout development
8.) git pull (pull latest changes)
9.) git checkout feature-branch
10.) git rebase development
11.) Resolve merge conflicts (if applicable)
12.) Run app to ensure everything is fine
13.) git push -f (push integrated development changes to your remote feature branch)
Merge or Rebase your features back into development
14.) git checkout development
15.) git merge/rebase feature-branch (merge the updated code back into development)
16.) git push
14 - 16 can also be done via GitHub via Pull request to review changes before the merge
How to Abort the rebase and restore the branch to its original state.
git rebase --abort
Pull: Fetch and download content from a remote repository and immediately update the local repository to match that content.
Commands:
git pullgit pull origin <branch-name>git pull --rebase- if there are changes on branch that have been rebased then use this
Stash: Temporarily save your changes without committing them to a branch
Commands:
git stash-> save changes without committing them to a branchgit stash push -m "Your comment here"-> stash changes with a messagegit stash push -u -m "Your comment here"-> stash tracked and untracked files with a messagegit stash list-> list all stashesgit stash apply-> Reapply the most recent stashgit stash apply stash@{n}-> Reapply a specific stash from listgit stash drop-> Remove latest stashgit stash drop stash@{n}-> Remove specific stash from listgit stash clear-> clear all stashesgit stash branch <branch-name>-> Create a new branch from a stashgit restore .-> This will reset your working directory to the last committed state.
You have uncommitted changes in a local branch and want to switch to another branch without passing over those changes
Step 1: You are on branch-A with uncommitted changes
git stash # Save changes and clean the working directory
git checkout branch-B # Switch to another branch (e.g., branch-B)
Step 2: Work on branch-B and then return to branch-A
git checkout branch-A # Switch back to the original branch
git stash apply # Restore the stashed changes on branch-A
git reset --hard-> resets your working directory, staging area, and current branch to a specific commit(latest commit by default)git reset --hard [commit-hash]-> Resets to the specified commit in the commit history
Fetch the branch branch_name from the remote (origin) and creates a corresponding local branch with the same name.
git fetch origin branch_name:branch_name
Your remote branch has been rebased and you want changes from rebased remote branch on your local
1.) git fetch origin - Updates your local information about the remote branches without changing your working branch
2.) git reset --hard origin/your-branch-name - Forces your local branch to match the remote branch exactly, discarding any local changes or commits that differ from the remote branch
Define: to take a single commit from one branch and adding it as the latest commit on another branch
Commands:
git cherry-pick <commit-hash>-> cherry pick a specific commit- Process for cherry picking:
git checkout parent-branch-> checkout parent branchgit pull origin parent-branch-> Ensure you're up to dategit checkout -b new-branch-> Create and checkout the new branchgit checkout <other-branch>-> checkout branch you want to pick commits fromgit log --oneline-> view all commits on the this branch (candidates/commits for cherry picking)git cherry-pick <commit-hash>orgit cherry-pick <commit-hash-1> <commit-hash-2> <commit-hash-3>- ``git push origin ```
Issue #1
After performing git pull you get this error:
cannot lock ref 'refs/remotes/origin/xxxxx/xxxx'....
Solution: git remote prune origin
Issue #2
After performing a git push -f or git push -u origin your-feature-branch you get this:
the upstream branch of your current branch does not match the name of your current branch
Explaination:
Your local and remote branch have different names
2 possible solutions
1.) rename your local branch to same name as remote branch
2.) delete the remote branch and push again
Issue #3
You wish to sync the updated remote branch with your local branch. After performing a git pull origin you get this:
You have divergent branches and need to specify how to reconcile them.
Explaination:
The branch you're trying to pull from has changes that your local branch doesn't have, and your local branch also has
commits that the remote branch doesn't have.
Solution
1.) git fetch origin -> Get all the latest updates from the remote repository without merging them into your local branch
2.) git reset --hard origin/your-branch-name -> reset your local branch to exactly match the remote branch (origin/your-branch-name).