Skip to main content

Basic Git Command

Git Worktree

Git Multiple Repo

Before doing a PR - clean up own branch

Simply stay in uglyCommitBranch and do an interactive rebase: clean your commits there, and the force push that branch: your pull-request (if it existed before the rebase, a PR made from that branch) will update itself.

If there was no pull request yet, you still can push --force as long as nobody else was working on uglyCommitBranch ( since it is your fork).

That being said, if you want to stay in newFeature branch, then:

git merge --squash uglyCommitBranch

Git delete local branch and remote

Delete a branch with :

git branch -d <branch>

Delete a branch remotely:

git push <remote> --delete <branch>

A new start?!

Reset all commit from the start

git reset $(git commit-tree HEAD^{tree} -m "A new start")

Variation: New Repo from a Project Template

This is useful to create the "initial commit" in a new project using another repository as the template/archetype/seed/skeleton. For example:

cd my-new-project

git init

git fetch --depth=1 -n

git reset --hard $(git commit-tree FETCH_HEAD^{tree} -m "initial commit")

Hard reset

git fetch origin

git reset --hard origin/integration

Remove commit

git rm --cached <file_to_remove_from_commit_<commit_id>_which_added_file>

git reset --soft HEAD~1

View git aliases

git config --get-regexp ^alias\.

git config --global alias.alias "config --get-regexp ^alias\."

git config --global status

Cherry Pick

git fetch ssh://[email protected]:7999/repo_to_get_it_from.git branchToPickFrom && git cherry-pick 02a197e9533

git fetch [branch URL] [Branch to cherry-pick from] && git cherry-pick [commit ID]

This command deletes branch references to remote branches that do not exist.

git remote prune origin

To see local branches, run this command:

git branch

To see remote branches, run this command:

git branch -r

To see all local and remote branches, run this command:

git branch -a

Update fork of GitHub

Option 1 - GitHub CLI

To update the remote fork from its parent, use the gh repo sync subcommand and supply your fork name as argument.

$ gh repo sync owner/cli-fork

If the changes from the upstream repository cause conflict then the GitHub CLI can't sync. You can set the -force flag to overwrite the destination branch.

Option 2 - Command Line

Before syncing one's fork with an upstream repository, one must configure a remote that points to the upstream repository in Git.

  1. Open Git Bash.

  2. Change the current working directory to your local project.

  3. Fetch the branches and their respective commits from the upstream repository. Commits to BRANCHNAME will be stored in the local branch upstream/BRANCHNAME.

$ git fetch upstream
> remote: Counting objects: 75, done.
> remote: Compressing objects: 100% (53/53), done.
> remote: Total 62 (delta 27), reused 44 (delta 9)
> Unpacking objects: 100% (62/62), done.
> From
> * [new branch] main -> upstream/main
  1. Check out your fork's local default branch - in this case, we use main.
$ git checkout main
> Switched to branch 'main'
  1. Merge the changes from the upstream default branch - in this case, upstream/main - into your local default branch. This brings your fork's default branch into sync with the upstream repository, without losing your local changes.
$ git merge upstream/main
> Updating a422352..5fdff0f
> Fast-forward
> README | 9 -------
> | 7 ++++++
> 2 files changed, 7 insertions(+), 9 deletions(-)
> delete mode 100644 README
> create mode 100644

If one's local branch didn't have any unique commits, Git will instead perform a "fast-forward":

$ git merge upstream/main
> Updating 34e91da..16c56ad
> Fast-forward
> | 5 +++--
> 1 file changed, 3 insertions(+), 2 deletions(-)

Git Clone Sub-folder

  1. Navigate to the folder where you'd like to clone the subdirectory.
  2. Open cmd and run the following commands.
git clone --filter=blob:none --sparse %your-git-repo-url%
git sparse-checkout add %subdirectory-to-be-cloned%
cd %your-subdirectory%

Voila! Now you have cloned only the subdirectory that you want!

Explanation - What are these commands doing really?

git clone --filter=blob:none --sparse %your-git-repo-url%

In the above command,

  • --filter=blob:none => You tell git that you only want to clone the metadata files. This way git collects the basic branch details and other meta from remote, which will ensure that your future checkouts from origin are smooth.
  • --sparse => Tell git that this is a sparse clone. Git will checkout only the root directory in this case.

Now git is informed with the metadata and ready to checkout any subdirectories/files that you want to work with.

git sparse-checkout add gui-workspace ==> Checkout folder

git sparse-checkout add gui-workspace/assets/logo.png ==> Checkout a file

Create branch from

git checkout -b new-branch from-branch

Multiple credential setup .gitconfig

name = lejencodes
email = [email protected]
[includeIf "gitdir:~/workspace/gitlab-coffeecodes/"]
path = ~/workspace/gitlab-coffeecodes/.gitconfig
helper = cache
helper = /usr/bin/git-credential-manager-core
credentialStore = cache
cacheOptions = --timeout 300
recurse = true
[credential ""]
useHttpPath = true
autocrlf = input
spellcheck = false

List Git Config

git config --list


git config --list --show-origin

To see where that setting is defined (global, user, repo, etc...)