Jotting one man's journey through software development, programming, and technology
◀️ Home
cd /path/to/your/directory
git clone https://github.com/Locaria/CAP_Quality_Checks.git
git checkout -b <new_branch_name> origin/<remote_branch_name>
*Replace
For example, if you want to create a branch called feature/new-feature based on origin/main:
git checkout -b feature/new-feature origin/main
git switch -c <new_branch_name>
git push -u origin <new_branch_name>
This sets up tracking for the branch so future git pull and git push commands automatically use the correct remote branch.
git fetch --all
Fetches changes from all remotes in your repository.
git fetch --all
If your repository has multiple remotes (e.g., origin, upstream, or others), it updates all of them. Commonly used when working with forks or multiple remote repositories.
git fetch origin
Fetches changes only from the origin remote.
git fetch origin
This is the most common scenario when you have just one remote repository (origin is usually the default remote name).
To pull the latest changes from the remote repository:
git pull
By default, this command pulls from the branch you’re currently on and updates your local branch with the changes from the remote.
git remote add origin https://github.com/example/repository.git
git push -u origin main
git add .
git commit -m "Your commit message"
git stash
git fetch origin
git push origin <branch_name>
Push your local commits to the remote repository.
git merge origin/<updates/branch_name>
This will merge your teammate’s changes into your local branch and create a merge commit if there are new commits.
git checkout branch_name
git rebase main
Rebase takes all the commits from example-branch that are not in main, temporarily removes them, updates feature-branch to match main, and then re-applies your commits on top of the updated main. This can help resolve commit issues, if remote and local are not in synchronization. During rebasing, if there are conflicts, Git will pause the process and you’ll need to resolve the conflicts manually.
git branch -a
git branch -r
git branch
git branch -d <branch_name>
git branch -D <branch_name>
Useful command to delete all local branches that are not in remote (GitHub) anymore. Handle with care though, it will also delete any local changes you made in those branches, if any.
git switch <branch_name>
git rm -r --cached <folder_name/>
Even if you add it to .gitignore, Git will still track the folder if it was committed before. To untrack it run the previous command
find . -name EXAMPLE.FILE -print0 | xargs -0 git rm -f --ignore-unmatch
git add .gitignore
git commit -m "Remove EXAMPLE.FILE files and update .gitignore"
git push
Useful if we accidentally pushed and synched a file we don’t want to sync. Make sure to add the respective file to .gitignore after untracking or it will be tracked again on the next add+commit.
for branch in $(git branch -r | grep -v '\->'); do
git branch --track "${branch#origin/}" "$branch" 2>/dev/null || true
git checkout "${branch#origin/}"
git reset --hard "$branch"
done
This script ensures that all local branches are synchronized with the remote branches by resetting them to the remote state. Be careful, this will discard all local changes that have not yet been pushed to remote.
for branch in $(git branch -r | grep -v '\->'); do
git branch --track "${branch#origin/}" "$branch" 2>/dev/null || true
git checkout "${branch#origin/}"
git pull --rebase
done
This script tracks all remote branches by creating corresponding local branches. For each branch, it switches to the local version and then pulls changes from the remote branch using rebase, which integrates the latest remote changes while preserving local commits.
git fetch --all --prune
for branch in $(git branch | sed 's/^..//'); do
if ! git show-ref --verify --quiet refs/remotes/origin/$branch; then
echo "Deleting local branch $branch as it no longer exists on remote"
git branch -D $branch
fi
done
Handle with care, as this will delete all your local branches and changes that don’t exist on remote. Make sure to save your existing work before running this.
git reset HEAD~1
*It moves the changes from the most recent commit back to the staging area without deleting them. This allows you to modify or recommit the changes as needed. You can modify the number in git reset HEAD~