List some useful git commands.
-
-
Save Hansimov/23bb639911d474c7ab5b93d32bcbd99d to your computer and use it in GitHub Desktop.
Git drop some commits in the branch:
git rebase -i HEAD~N
The ~N
means rebase the last N
commits (N
must be a number, for example HEAD~10
).
Then, you can edit the file that Git presents to you to delete the offending commit.
On saving that file, Git will then rewrite all the following commits as if the one you deleted didn't exist.
Use push -f
to force the push and replace the remote branch with your local one.
- git - How do I delete a commit from a branch? - Stack Overflow
Git pull force overwrite local changes:
git fetch --all
# git branch backup-changes
git reset --hard origin/main
- version control - How do I force "git pull" to overwrite local files? - Stack Overflow
Auto complete git branch names in (t)csh
Download git-completion.bash
and git-completion.tcsh
:
curl https://raw.githubusercontent.com/git/git/master/contrib/completion/git-completion.bash -o ~/.git-completion.bash
curl https://raw.githubusercontent.com/git/git/master/contrib/completion/git-completion.tcsh -o ~/.git-completion.tcsh
Add following lines to ~/.cshrc
:
set autolist=ambiguous
source ~/.git-completion.tcsh
- terminal - git auto-complete for branches at the command line? - Ask Different
- git/git-completion.tcsh at master · git/git
- git/git-completion.bash at master · git/git
Gist remove/squash some revisions:
References:
- git - How to delete a specific revision of a github gist? - Stack Overflow
NOTE: Do not forget to remove the #
after rebase in order to add a valid commit message after squashing
Git change author of previous commit:
git commit --amend --author="Author Name <[email protected]>" --no-edit
References:
- git - How to change the commit author for a single commit? - Stack Overflow
Git update submodules to latest commit:
git submodule foreach git pull
or submodule is in detached state:
git submodule foreach git pull origin main # (master)
References:
- Update Git submodule to latest commit on origin - Stack Overflow
Git pull submodule:
git submodule update --init --recursive # (First time)
git submodule update --recursive --remote
References:
- Pull latest changes for all git submodules - Stack Overflow
Remove contributor from GitHub repo page:
- On GitHub web page, change a branch name (e.g.,
main
-->main1
).- It updates the contributor list on the GitHub repo dashboard.
- Then change it back (
main1
-->main
).
References:
- git - Removing contributor from github.com? - Stack Overflow
Git rebase to target branch, and use target's changes if conflicts (i.e., files both modified in two branches).
git rebase <target-branch> -X theirs
Else use ours:
git rebase <target-branch> -X ours
Resolve Git merge conflicts in favor of their changes during a pull - Stack Overflow
Git - git-rebase Documentation