Git Operations

  1. How to remove local untracked files from the current Git branch To remove directories, run git clean -f -d or git clean -fd.
    To remove ignored files, run git clean -f -X or git clean -fX.
    To remove ignored and non-ignored files, run git clean -f -x or git clean -fx.

  2. reset to an absolute commit SHA1 value

1
 git reset 5d125c
  1. Git submodule
1
git submodule add https://github.com/fi3ework/hexo-theme-archer.git themes/archer
  1. Change branch name
1
2
3
git branch -m master main
git fetch origin
git branch -u origin/main main
  1. How do I delete a Git branch locally and remotely?
1
2
3
4
5
6
$ git branch -d branch_name
$ git branch -D branch_name

git push -d <remote_name> <branch_name>
# Note that in most cases the remote name is origin. In such a case you'll have to use the command like so.
$ git push -d origin <branch_name>
  1. Squash my last X commits together using Git
    Use git rebase -i <after-this-commit> and replace “pick” on the second and subsequent commits with “squash” or “fixup”, as described in the manual.

  2. How to modify existing, unpushed commit messages? Amending the most recent commit message

1
git commit --amend -m "New commit message"

Changing the message of a commit that you’ve already pushed to your remote branch If you’ve already pushed your commit up to your remote branch, then - after amending your commit locally (as described above) - you’ll also need to force push the commit with:

1
git push <remote> <branch> --force
  1. Adding an existing project to GitHub using the command line
1
2
3
4
5
6
7
8
9
$ git init -b main
$ git add .
$ git commit -m "First commit"
$ git remote add origin  <REMOTE_URL> 
# Sets the new remote
$ git remote -v
# Verifies the new remote URL
# Push the changes in your local repository to GitHub.
$ git push -u origin main