Git Commands

to clone a repository:

git clone my_git_address

Branches

list local branches:

git branch

list remote branches:

git branch --remote

Checkout and track a remote branch:

git checkout --track origin/xyz

Push a local branch to a new remote branch name

git push --set-upstream origin <remotebranchname>

delete local git branch

git branch -d <local name>

delete remote branch

git push origin --delete <remote/branch-name>

check out all remote branches to a local branch:

for remote in `git branch -r | grep -v /HEAD`; do git checkout --track $remote ; done

from here

Status

Sometimes you want to see what has changed. git status can be used to see which files have changed since the last commit. Otherwise, you can use git diff to more closely inspect file changes line by line

git status
git diff
git diff --color=always <commit hash> | less -r

Pushing

Push a quick commit:

If you created a local branch and need to link it to a remote:

git push --set-upstream origin <localbranchname>
git add *
git commit -m "commit message"
git push

or combined:

git add * && git commit -m "update" && git push

log

git log

use this to find the commit hash for prior commits. you can use this to reset

reset

reset to head

git reset

reset to specific commit

git reset <hash>

reset index to head and reset files

git reset --hard

clean

interactive clean

git clean -i

force the removal of all untracked files

git clean -dxf

restore

git restore <path/to/file>

restore all files of the type .c

git restore '*.c'

Submodules

create

git submodule add <repo_address> <your/local/path>

from here

to pull submodules

git submodule update --init --recursive
git submodule update --recursive --remote

cd into the proper subdirectory ensure you are attached to a branch:

git branch

if not check one out

git checkout [branchname]

feetch all remote tags

git fetch --all --tags

check out tag to a new branch

git checkout tags/<tag> -b <branch>

example

git checkout tags/Ubuntu-5.13.0-21.21 -b Ubuntu-5.13.0-21.21

ignore

echo “docs/lectures/lecture.html” » .gitignore

External references

Remove files from git tree without deleting

https://stackoverflow.com/questions/1143796/remove-a-file-from-a-git-repository-without-deleting-it-from-the-local-filesyste

git rm --cached path/to/the/file

Stash changes

from here

git stash

git stash --include-untracked

git stash pop

git merge

git checkout master
git merge featureBranch

pull and merge upstream changes into forked branch

git clone path/to/my_repo.git
cd my_repo
git remote add upstream path/to/upstream_repo.git
git fetch upstream

git pull upstream master
or
git rebase upstream/master

git push --force
git push origin main

from here and here

Adding a second remote

git remote add <name-of-new-remote> https://github.com/OWNER/REPOSITORY.git
git remote -v
git push <name-of-new-remote> <branch>

https://docs.github.com/en/get-started/getting-started-with-git/managing-remote-repositories

git push --set-upstream <name-of-new-remote> <branch>
git branch --track <name-of-new-remote> <branch>

push all branches and tags from one remote to another

assuming your original remote is called “origin”

git push <name-of-new-remote> --tags "refs/remotes/origin/*:refs/heads/*"

https://stackoverflow.com/questions/37884832/git-push-all-branches-from-one-remote-to-another-remote

Gitignore

To check what gitignore rule is causing a particular path to be ignored, run git check-ignore:

git check-ignore -v path/to/check

from here

Merge Unrelated branches

https://stackoverflow.com/questions/1425892/how-do-you-merge-two-git-repositories

cd path/to/project-a
git filter-repo --to-subdirectory-filter project-a

cd path/to/project-b
git remote add project-a /path/to/project-a
git fetch project-a --tags
git merge --allow-unrelated-histories project-a/master # or any branch you want to merge
git remote remove project-a

merge with squash

git merge --squash <tag or branch>

for example

git merge --squash wip