Skip to content

Instantly share code, notes, and snippets.

@Hansimov
Last active April 14, 2023 07:41
Show Gist options
  • Save Hansimov/23bb639911d474c7ab5b93d32bcbd99d to your computer and use it in GitHub Desktop.
Save Hansimov/23bb639911d474c7ab5b93d32bcbd99d to your computer and use it in GitHub Desktop.
Collection of useful git commands in daily work

List some useful git commands.

@Hansimov
Copy link
Author

Hansimov commented Jun 30, 2021

Git show commit info with specified hash:

git show <Hash> -s --pretty="%H%n%aN%n%ae%n%ci"

<Full Hash>
<Author Name>
<Author Email>
<committer Date>

@Hansimov
Copy link
Author

Hansimov commented Jun 30, 2021

Git log latest 10 commits:

git --no-pager log --pretty=oneline -10

<Hash1> (HEAD, origin/main, origin/HEAD) <Commit Message>
<Hash2> (tag: xxxx) <Commit Message>
<Hash3> <Commit Message>
...



More detailed version:

git log limit max number of commits:
git --no-pager log -5

commit <hash1> (HEAD -> main)
Author: <author1> <email1>
Date:   <date1>
<message1>

commit <hash2> (tag: <tag>, origin/main, origin/HEAD)
Author: <author2> <email2>
Date:   <date2>
<message2>

commit <hash3> (tag: <tag>)
Author: <author3> <email3>
Date:   <date3>
<message3>

...

@Hansimov
Copy link
Author

Hansimov commented Jun 30, 2021

Git get latest commit hash of remote repo:

git ls-remote | head -1

<hash>

@Hansimov
Copy link
Author

Hansimov commented Jun 30, 2021

Git get HEAD commit hash:

git rev-parse HEAD

<hash>

@Hansimov
Copy link
Author

Hansimov commented Jul 9, 2021

Git get latest tags name in current branch:

git --no-pager log --simplify-by-decoration --decorate --pretty=oneline -10 | grep -F -m1 '(tag: '

<hash> (tag: <tag1>, <tag2>) <commit_message> (#<num>)


(Not recommended) Another solution:

git describe --tags

<tag name>
# if multiple tags, choose the last one;
# and may also attach some other info at the tail of the original tag name

@Hansimov
Copy link
Author

Hansimov commented Nov 4, 2021

Git delete multiple branches with one line command:

git branch -D `git branch | grep -E 'main_10029.*'`

or (recommend)

git branch | grep 'main_10029.*' | xargs git branch -D

(Do not miss the . before * in the grep regex)

Deleted branch main_1002909 (was 94****7e).
Deleted branch main_1002920 (was a0****9b).
Deleted branch main_1002925 (was d3****38).
...
Deleted branch main_1002985 (was d6****17).

@Hansimov
Copy link
Author

Error: Entry '...' not uptodate. Cannot merge.
Solution:

git rm --cached <filename>


@Hansimov
Copy link
Author

Hansimov commented Feb 24, 2022

Git log to console (no pager),

git --no-pager log --pretty='format:%Cgreen[%h] %Cblue[%ai] %Creset[%an]%C(Red)%d %n %Creset%s %n' -n3

[xxxxxxxx] [2022-02-23 13:00:13 -0800] [author1] (HEAD -> main_12346, tag: 12346, origin/main, origin/HEAD, main)
  [Fix] Fix a bug (#678)

[yyyyyyyy] [2022-02-23 12:22:22 -0800] [author2] (tag: 12345)
  [Enhance] Enhance a feature (#677)

[zzzzzzzz] [2022-02-23 09:09:09 +0100] [author3] (tag: 12344)
  [Add] Add a component (#676)

@Hansimov
Copy link
Author

Hansimov commented May 18, 2022

Revert specific files to an old commit

git restore --source=c5f567 file1_to_restore file2_to_restore

Revert to 1 commit before c5f567:

git restore --source=c5f567~1 file1_to_restore file2_to_restore

Use git status to check whether the files are reverted


Reset/Drop previous commit(s):

--soft will leave all changed files as "changes to be committed":

git reset --soft HEAD~1

--hard will discard all changes in the working tree and delete all untracked files:

git reset --hard HEAD~1


@Hansimov
Copy link
Author

Hansimov commented May 23, 2022

Git sync forked repo with original repo

(1) Fork original repo to user repo in the web page

(2) Git clone user repo to local

git clone https://github.com/<user_name>/<repo_name>.git <local_repo_name>

(3) Add original repo to upstream

git remote remove upstream
git remote add upstream https://github.com/<original_owner_name>/<repo_name>.git

(4) Use following command to see info of origin and upstream:

git remote -v

(5) Create local branches from origin

git checkout -f -b main origin/main
git checkout -f -b dev origin/dev

(6) Sync origin (local and remote) with upstream

git fetch upstream
git checkout <branch_name>
git merge upstream/<branch_name>
git push origin <branch_name>

@Hansimov
Copy link
Author

Hansimov commented Jun 15, 2022

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


@Hansimov
Copy link
Author

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.


@Hansimov
Copy link
Author

Hansimov commented Aug 9, 2022

Git pull force overwrite local changes:

git fetch --all
# git branch backup-changes
git reset --hard origin/main

@Hansimov
Copy link
Author

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

@Hansimov
Copy link
Author

Hansimov commented Feb 1, 2023

Gist remove/squash some revisions:

References:

NOTE: Do not forget to remove the # after rebase in order to add a valid commit message after squashing

@Hansimov
Copy link
Author

Git change author of previous commit:

git commit --amend --author="Author Name <email@address.com>" --no-edit

References:

@Hansimov
Copy link
Author

Hansimov commented Feb 21, 2023

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:

@Hansimov
Copy link
Author

Git pull submodule:

git submodule update --init --recursive  # (First time)
git submodule update --recursive --remote

References:

@Hansimov
Copy link
Author

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:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment