Keywords: git, commands, pull, branch

Base Commands

1, pull files from remote (update)

git pull origin master

2, Add file

git add folder1/file1 folder2/file2
git commit
git push origin master

If you are having problems with untracked files, this 3-line script will help you:

git rm -r --cached .
git add -A
git commit -m "fix"

Then just:

git push

Origin:
https://stackoverflow.com/a/55888099/1645289

3, Remove file

#git rm --cached file1.txt    #remove file from remote, but keep file in local.
git rm file1.txt    #remove file from both remote and local.
git commit -m "delete file aaa"
git push origin master

Common Commands

1, abort merge

git merge --abort

2, switch branch

git checkout master

or create local branch and switch to it if HEAD detached:

git checkout -b master remotes/origin/master --

Or:

git checkout -b new_branch 0d1d7fc32

3, clone only a single branch

git clone [url] -b [branch-name] --single-branch

4, Get the current branch name in Git?

git branch | grep \* | cut -d ' ' -f2

5, Hard reset of a single file (discard local file changes)

git checkout HEAD -- my-file.txt

6, Discard all local change:

git reset --hard origin/master

says: throw away all my staged and unstaged changes, forget everything on my current local branch and make it exactly the same as origin/master.

7, Change connection from SSH to HTTPS

git remote set-url origin https://...
git remote set-url --push origin https://...

You can view the configured remotes with git remote -v, which should now show your updated URLs.

Reference:
https://stackoverflow.com/questions/30683399/how-to-change-a-connection-to-github-from-ssh-to-https

Sync Fork

Steps:

1, add remote forwarded to upstream(father repository)

git remote add upstream https://github.com/ORIGINAL_OWNER/ORIGINAL_REPOSITORY.git

2, fetch from upstream

git fetch upstream

3, switch to branch you want to merge, e.g. master:

git checkout master

If upstream has a branch which has the same as origin, you must designate the full name of your origin branch, e.g.:

git checkout remotes/origin/master

4, merge upstream/master to your local master branch.

git merge upstream/master

5, push to your remote fork

git push

Reference:
How to sync branch forked on git
https://jinlong.github.io/2015/10/12/syncing-a-fork/

Rollback Commits

Hard delete unpublished commits
# This will destroy any local modifications.
# Don't do it if you have uncommitted work you want to keep.
git reset --hard 0d1d7fc32

# Alternatively, if there's work to keep:
git stash
git reset --hard 0d1d7fc32
git stash pop
# This saves the modifications, then reapplies that patch after resetting.
# You could get merge conflicts, if you've modified things which were
# changed since the commit you reset to.

How do I revert a Git repository to a previous commit?
https://stackoverflow.com/a/4114122/1645289

Undo published commits with new commits
# This will create three separate revert commits:
git revert a867b4af 25eee4ca 0766c053

# It also takes ranges. This will revert the last two commits:
git revert HEAD~2..HEAD

#Similarly, you can revert a range of commits using commit hashes (non inclusive of first hash):
git revert 0d1d7fc..a867b4a

# Reverting a merge commit
git revert -m 1 <merge_commit_sha>

# To get just one, you could use `rebase -i` to squash them afterwards
# Or, you could do it manually (be sure to do this at top level of the repo)
# get your index and work tree into the desired state, without changing HEAD:
git checkout 0d1d7fc32 .

# Then commit. Be sure and write a good message describing what you just did
git commit

How do I revert a Git repository to a previous commit?
https://stackoverflow.com/a/4114122/1645289

Other ways

1st way:

//revert back 3 commits
git revert HEAD~3

2nd way:

//This will revert everything from the HEAD back to the commit hash, meaning it will recreate that commit state in the working tree as if every commit after `0766c053` had been walked back.
git revert --no-commit 0766c053..HEAD
git commit

This is a safe and easy way to rollback to a previous state. No history is destroyed, so it can be used for commits that have already been made public.
Origin:
https://stackoverflow.com/a/21718540/1645289

3rd way:

git revert --no-commit hash1 hash2
git commit -m "Message"

Revert

Re-clone
GIT=$(git rev-parse --show-toplevel)
cd $GIT/..
rm -rf $GIT
git clone ...
  • ✅ Deletes local, non-pushed commits
  • ✅ Reverts changes you made to tracked files
  • ✅ Restores tracked files you deleted
  • ✅ Deletes files/dirs listed in .gitignore (like build files)
  • ✅ Deletes files/dirs that are not tracked and not in .gitignore
  • 😀 You won’t forget this approach
  • 😔 Wastes bandwidth
Clean and reset
git clean --force -d -x
git reset --hard
  • ❌ Deletes local, non-pushed commits
  • ✅ Reverts changes you made to tracked files
  • ✅ Restores tracked files you deleted
  • ✅ Deletes files/dirs listed in .gitignore (like build files)
  • ✅ Deletes files/dirs that are not tracked and not in .gitignore
Clean
git clean --force -d -x
  • ❌ Deletes local, non-pushed commits
  • ❌ Reverts changes you made to tracked files
  • ❌ Restores tracked files you deleted
  • ✅ Deletes files/dirs listed in .gitignore (like build files)
  • ✅ Deletes files/dirs that are not tracked and not in .gitignore
Reset
git reset --hard
  • ❌ Deletes local, non-pushed commits
  • ✅ Reverts changes you made to tracked files
  • ✅ Restores tracked files you deleted
  • ❌ Deletes files/dirs listed in .gitignore (like build files)
  • ❌ Deletes files/dirs that are not tracked and not in .gitignore

Origin:
How do I revert all local changes in Git managed project to previous state?
https://stackoverflow.com/a/42903805/1645289

Miscellaneous

How to exit out of git commit commenting

Shortcut:

Ctrl + X

上帝等待着人类在智慧中获得新的童年。──泰戈尔(Rabindranath Tagore)