[VersionControl]Git Commands In Common
Keywords: git, commands, pull, branch
Commands
Common 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
4, abort merge
git merge --abort
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
7, Change connection from SSH to HTTPS
git remote set-url origin https://...
git remote set-url --push origin https://...
Reference:
https://stackoverflow.com/a/30683599/1645289
Clone only one branch without history
git clone --depth 1 --branch <branch> url
example:
git clone --depth 1 --branch 5.2 git@github.com:EpicGames/UnrealEngine.git
Origin:
https://stackoverflow.com/a/24107526/1645289
Clone only one file or subdirectory
git clone --filter=blob:none --sparse %your-git-repo-url%
git sparse-checkout add %subdirectory-to-be-cloned%
cd %your-subdirectory%
sparse-checkout
example:
git sparse-checkout add gui-workspace ==> Checkout folder
git sparse-checkout add gui-workspace/assets/logo.png ==> Checkout a file
Origin:
https://stackoverflow.com/a/73254328/1645289
Fork
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
Origin: How to sync branch forked on git
https://jinlong.github.io/2015/10/12/syncing-a-fork/
Branches
Show Branch
Show local branches:
git branch
Show local branches and remote branches:
git branch -a
Switch Branch
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
clone only a single branch:
git clone [url] -b [branch-name] --single-branch
Get the current branch name:
git branch | grep \* | cut -d ' ' -f2
Merge Branches
Merging changes from master into my branch.
Make sure master is up to date first, and stash changed files, then execute git checkout custom_branch && git rebase master
:
git checkout master
git pull
git checkout custom_branch && git rebase master
Origin:
https://stackoverflow.com/a/41045625/1645289
Delete Branch
Delete branch (delete local and remote brance at once):
git fetch --prune
git push origin :my_branch
Or, delete branch in local firstly, then delete branch in remote:
git fetch --prune
git checkout master
git branch -d my_branch
git push origin --delete my_branch
git branch --all
Origin:
https://stackoverflow.com/a/59441804/1645289
cherry-pick
Merge single commits:
git cherry-pick commit_id
Merge multiple commits:
git cherry-pick commit_01 commit_02 commit_03
Merge a range of commits:
# NOT including the commit_01
git cherry-pick commit_01..commit_09
If there’re some conflicts where merge commitment, need to add or remove conflict files and continue after the conflict resolved.
git add/rm <conflict file path>
git cherry-pick --continue
Reference: https://stackoverflow.com/a/69472178/1645289
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)