Basic Git commands

Git installs as a normal package on Ubuntu:

sudo apt-get install git

Configuring Git user is an optional step:

git config --global user.email "you@example.com"
git config --global user.name "Your Name"

Type the following command to create an empty repository wherever your code is:

cd ~
git init

Now all the initialization is done.  Add something to the repository:

git add www
commit -m "first commit"

The basic Git commands are:

List files in the repository git ls-files
Undo local changes git checkout <filename>
List changed files those you need to commit git status <directory>
Commit changes git commit –m “message” <directory>
Show commit history for a branch git log -3 master..
Show files in a commit git diff-tree –no-commit-id –name-only -r <commit>
Disabling SSL cert check. git config —global http.sslVerify «false»
Delete the most recent commit, keeping the work you’ve done git reset –soft HEAD~1
Delete the most recent commit, destroying the work you’ve done git reset –hard HEAD~1
View the change history of a file gitk [filename] gitk –follow [filename]
Viewing Unpushed Git Commits git log origin/master..HEAD
List of files to be pushed git diff –stat –cached [remote/branch]
Show both local and remote branches git branch -a
Show remote branches git branch -r
Publish a local branch git push -u origin <branch-name>
Show the remote address of a repo git remote -v
Apply a patch git apply –ignore-space-change –ignore-whitespace patch.diff

Probably one of the most interesting git commands is cherry-pick:

  1. What does cherry-picking a commit with git mean?

Git visual tools:

sudo apt-get install qgit
sudo apt-get install melt

Adding a submodule:

git submodule add https://github.com/dmitriano/Awl.git lib/Awl
#Then after cloning:
git submodule init
git submodule update

Renaming the local and remote branch dev-data-race with android-1.0.15:

git branch -m dev-data-race android-1.0.15
git push origin :dev-data-race
git push --set-upstream origin android-1.0.15

Deleting remote branch dev-sanitizer:

git branch -d dev-sanitizer
git push origin --delete dev-sanitizer

Tagging:

git tag -a v2.2.13 -m "Version 2.2.13 in Google Play."
git describe --tags
git tag
git show --summary v2.2.13
git push origin v2.2.13
git push origin --tags
git checkout v2.2.13
git checkout -b version2.2.13 v2.2.13
git log --pretty=oneline
git tag -a v2.2.13 9fceb02
git show refs/tags/v2.5.31#162:src/file.cpp
git tag -a v2.6.0 -m "Version 2.6.0 in Google Play, APK 181-184."
git push --delete origin v2.6.0
git tag --delete v2.6.0
git tag -l "v2.7*"
git push -d origin $(git tag -l "v2.7*")
git tag -d $(git tag -l "v2.7*")

Creating a repository on the server.

git init --bare tradebot.git

Pushing a newly created repository on the client.

git remote add origin https://git.developernote.com/tradebot.git
git push origin master

Checkout file from a different branch:

git ls-tree master src/LinesGame/LinesGameQt
git checkout master -- src/LinesGame/LinesGameQt/icons.qrc

Find and restore a deleted file or folder:

git rev-list -n 1 HEAD -- src/LinesGame/LinesGameQt/platform
git checkout 41264a71364dc8dbab96eec67e2b9d25332b2ca4^ -- src/LinesGame/LinesGameQt/platform

Save credentials:

git config --global credential.helper store

Looks like when I do

git config --global credential.useHttpPath true

in GitBash, Windows credential manager saves full repository URL, see Git credential helper – update password.

Fix your project to use a remote with credentials built in:

git remote set-url origin https://scuzzlebuzzle:<MYTOKEN>@github.com/scuzzlebuzzle/ol3-1.git

Update remote branches:

git fetch --prune --all

Delete a tag:

git tag --delete 3.0.17
git push --delete origin 3.0.17

14 Responses to Basic Git commands

  1. dmitriano says:

    Truncating git history (https://passingcuriosity.com/2017/truncating-git-history/):
    git checkout –orphan temp e41d7f633c45c46bd42e97cecf93204191d9e4c9
    git commit -m “Truncate history”
    git rebase –onto temp e41d7f633c45c46bd42e97cecf93204191d9e4c9 master

  2. superadmin says:

    clean all the modules recursively:
    git submodule foreach –recursive “git clean -dfx”

  3. dmitriano says:

    How do I configure git to ignore some files locally?
    https://stackoverflow.com/questions/1753070/how-do-i-configure-git-to-ignore-some-files-locally
    The .git/info/exclude file has the same format as any .gitignore file

  4. dmitriano says:

    https://stackoverflow.com/questions/53066369/fast-forward-merge-is-not-possible-to-merge-this-request-first-rebase-locally

    Starting on your newBranch:

    git checkout master to get back on the master branch

    git pull origin master to get the most up-to-date version of the master branch

    git checkout newBranch to get back on your newBranch

    git rebase origin/master -i to perform an interactive rebase.

  5. dmitriano says:

    https://stackoverflow.com/questions/7203515/how-to-find-a-deleted-file-in-the-project-commit-history
    Find a file in the commit history:
    git log –all –full-history somefolder/somefile.cpp

  6. dmitriano says:

    https://stackoverflow.com/questions/424071/how-do-i-list-all-the-files-in-a-commit
    How do I list all the files in a commit?
    git diff-tree –no-commit-id –name-only bd61ad98 -r

  7. dmitriano says:

    https://stackoverflow.com/a/75450029/9261322
    Completely remove the changes of a specific file from the history
    Say we have history like this:

    $ git log
    commit c4e983a7f506f71e01d2e1e4cddf2f768e2c2548 (HEAD -> main)
    Author: REDACTED
    Date: REDACTED

    3

    commit 3e616c11fa1ce71157e4b654e4e3742f97c6e0e6
    Author: REDACTED
    Date: REDACTED

    2

    commit 7655a6d373c26db5d30086a6e7e23e755c960924
    Author: REDACTED
    Date: REDACTED

    1

  8. dmitriano says:

    https://stackoverflow.com/questions/34332687/how-can-i-know-in-git-if-a-branch-has-been-already-rebased-onto-master
    How can I know in git if a branch has been already rebased onto master?
    git log –oneline –cherry master…some-branch
    git log –oneline –cherry development…GPR-123

  9. dmitriano says:

    Undo a Git merge that hasn’t been pushed yet
    https://stackoverflow.com/a/2389423

    With git reflog check which commit is one prior the merge (git reflog will be a better option than git log). Then you can reset it using:

    git reset –hard commit_sha
    There’s also another way:

    git reset –hard HEAD~1

  10. dmitriano says:

    How I removed some changes that I committed by mistake:

    git checkout -b GPR-123a
    git reset origin/development
    git commit -a -m “GPR-123: Zakolhozil chto-to”
    git push -u origin GPR-123a

  11. dmitriano says:

    https://stackoverflow.com/questions/25356810/git-how-to-squash-all-commits-on-branch

    Another way to squash all your commits is to reset the index to master:

    git checkout yourBranch
    git reset $(git merge-base master $(git branch –show-current))
    git add -A
    git commit -m “one commit on yourBranch”

  12. dmitriano says:

    git push -d origin bug-588
    error: unable to delete 'bug-588': remote ref does not exist
    error: failed to push some refs to 'ssh://git@gitlab-my-company.com:2222/my-team/my-project.git'

    The branch is deleted already on the remote and the local is not aware of it. Run
    git fetch --prune --all
    and verify is the remote branch still there.

  13. dmitriano says:

    How to see which commit a git submodule points at
    https://stackoverflow.com/questions/20655073/how-to-see-which-commit-a-git-submodule-points-at

    git ls-tree HEAD

    in the submodule directory.

Leave a Reply

Your email address will not be published. Required fields are marked *