Frequently used git commands
Amit Nadiger
Polyglot(Rust??, C++ 11,14,17,20, C, Kotlin, Java) Android TV, Cas, Blockchain, Polkadot, UTXO, Substrate, Wasm, Proxy-wasm,AndroidTV, Dvb, STB, Linux, Engineering management.
Bitbucket configuration in Windows :
Referance https://www.atlassian.com/git/tutorials
Install Git:
We need to install Git on your Windows machine. We can download the installer from the official Git website: https://git-scm.com/download/win
If you don't already have an SSH key pair, you can generate one using the Git Bash terminal that comes with Git:
ssh-keygen -t rsa -b 4096 -C "[email protected]"
Open the Git Bash terminal again and configure your Git user details:
git config --global user.name "Your Name" git config --global user.email "[email protected]"
To test if your SSH connection to Bitbucket is working, run the following command in Git Bash:
ssh -T [email protected]
If the connection is successful, you'll see a message confirming your authentication.
git clone <repository-ssh-url>
Now you should have successfully configured your SSH key and Git to work with Bitbucket on your Windows machine. You can use Git Bash to interact with your repositories using the command line.
Debugging the issues related to SSH keys
Check Your SSH Key Pair:
Check that you've added the correct public key (id_rsa.pub) to your Bitbucket account. Ensure that you haven't modified the key content in any way. Also, make sure that you're using the correct private key (id_rsa) when trying to connect.
Check SSH Agent:
Ensure that your SSH agent is running and has your private key loaded. You can use the following command to add your private key to the agent:
ssh-add ~/.ssh/id_rsa
Verify SSH Agent Status:
After adding the key, you can verify that it's added to the agent by running:
ssh-add -l
This command should list the fingerprints of the keys currently managed by the agent.
Check Environment Variables:
Make sure that the necessary environment variables related to the SSH agent are correctly set. You should see output similar to the following after running eval $(ssh-agent -s):
Agent pid 12345
Ensure ssh-agent Persistence:
Some systems may not persist the SSH agent across sessions or restarts. You might need to re-run the eval $(ssh-agent -s) command each time you start a new terminal session. To make the agent persistent, you can add the command to your shell profile script (e.g., .bashrc, .zshrc).
Check SSH Configuration:
Verify that your SSH configuration in ~/.ssh/config is correct. The entry for Bitbucket should include the correct hostname, IdentityFile, and any other necessary settings:
Host bitbucket.org HostName bitbucket.org IdentityFile ~/.ssh/id_rsa AddKeysToAgent yes
Retry SSH Connection:
After checking and confirming your SSH key configuration and agent, try connecting to Bitbucket again using the ssh -T [email protected] command.
Restart SSH Agent:
Sometimes, restarting the SSH agent can help resolve authentication issues. Run the following commands:
ssh-agent -k # Kill existing SSH agent (if running) eval "$(ssh-agent -s)" # Start a new SSH agent ssh-add ~/.ssh/id_rsa # Add your private key to the agent
Below are some of the frequently used commands :
Create a New Branch:
To create a new branch based on the current branch:
git checkout -b new-branch-name
Switch to a Branch:
To switch to an existing branch:
git checkout branch-name
List Branches:
To list all branches (local branches):
git branch
List Remote Branches:
To list remote branches (branches on the remote repository):
git branch -r
Show Current Branch:
To show the current branch:
git branch
Create a New Branch from Another Branch:
To create a new branch based on an existing branch:
git checkout -b new-branch-name existing-branch-name
Checkout a Specific Commit:
To check out a specific commit (detached HEAD state):
git checkout commit-hash
To commit changes in the working directory:
git commit -m "Commit message"
Stage and Commit in One Step:
To stage and commit changes in one step:
git commit -am "Commit message"
Pull Changes from Remote:
To pull changes from the remote repository into your current branch:
git pull origin branch-name
领英推荐
git pull:
git pull
The git pull command is a combination of git fetch and git merge. It fetches changes from the remote repository and automatically merges them into your local branch. It's useful to keep your local branch up-to-date with the remote.
Push Changes to Remote:
To push your committed changes to the remote repository:
git push origin branch-name
The git push command is used to send your committed changes to a remote repository. It updates the remote repository with your new commits.
git push
Merge Branches:
To merge another branch into your current branch:
git merge branch-name
Rebase Branches:
To rebase your current branch onto another branch:
git rebase branch-name
Undo Last Commit (Local):
To undo the last commit while keeping changes in the working directory:
git reset HEAD~1
Discard Local Changes:
To discard local changes in the working directory:
git checkout -- file-name
View Commit History:
To view commit history:
git log
View Changes in Working Directory:
To see the changes in the working directory:
git diff
View Staged Changes:
To see the changes staged for commit:
git diff --staged
Add Files to Staging:
To add specific files to the staging area:
git add file-name
Add All Changes to Staging:
To add all changes to the staging area:
git add .
Retrieve updates from a remote repository - git fetch:
The git fetch command is used to retrieve updates from a remote repository without applying any changes to your local working directory or branch. It fetches new commits, branches, and other objects from the remote repository and updates your remote-tracking branches (e.g., origin/main).
git fetch
upstream branches - git branch -vv:
The git branch -vv command shows a detailed list of your local branches along with information about their remote counterparts (upstream branches). It displays the remote tracking branch associated with each local branch, as well as the last commit on that branch.
git branch -vv
Set the upstream tracking branch to current local branch.
This command is used to set the upstream tracking branch for your current local branch. An upstream branch helps Git determine where to push changes and from where to fetch updates.
git branch --set-upstream-to=origin/remote-branch
For example:
git branch --set-upstream-to=origin/main
git merge:
The git merge command is used to integrate changes from one branch into another. It combines the changes from the source branch into the target branch. Merging can result in a merge commit, which has multiple parent commits.
git merge
git commit:
The git commit command creates a new commit with the changes you've staged (added to the index). Each commit represents a snapshot of your project at a specific point in time.
git commit
Recommended Order of Usage:
Below is the screen in gitpub :
You have an empty repository
To get started you will need to run these commands in your terminal.
Configure Git for the first time
git config --global user.name "Amit Nadiger"
git config --global user.email "[email protected]"
Working with your repository
I just want to clone this repository
If you want to simply clone this empty repository then run this command in your terminal.
git clone ssh://[email protected]/path-yyy/branch-zzz.git
My code is ready to be pushed
If you already have code ready to be pushed to this repository then run this in your terminal.
cd existing-project
git init
git add --all
git commit -m "Initial Commit"
git remote add origin ssh://[email protected]/path-yyy/branch-zzz.git
git push -u origin HEAD:develop
My code is already tracked by Git
If your code is already tracked by Git then set this repository as your "origin" to push to.
cd existing-project
git remote set-url origin ssh://[email protected]/path-yyy/branch-zzz.git
git push -u origin --all
git push origin --tags
How to configure the github:
Execute the below commands in the Linux terminal
1. // Create the Keys.txt file
$ vim Keys.txt
2. // Generate the public and private keys;
$ ssh-keygen -t ed25519 -C "[email protected]" -f /home/amit/Keys.t
xt
3. Generating public/private ed25519 key pair.
// Enter the passphrase which acts as password
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
// Then you will see below kind of output:
Your identification has been saved in /home/amit/Keys.txt
Your public key has been saved in /home/amit/Keys.txt.pub
The key fingerprint is:
SHA256:lQ4XXP4MOPUhrnjDNs1m2x0RghPCO+f6tdipIEpTAmQ [email protected]
The key's randomart image is:
+--[ED25519 256]--+
| E .o.o*... |
| o .oX o...|
| . . D = .. |
| . M * + .|
| . .K % = o. |
| o o * o ..|
| o . ...... .|
| . o.... + o |
| . .+.- |
+----[SHA256]-----+
// Below command is used to start the SSH agent and set up the environment // variables for the current shell session.
4. $ eval "$(ssh-agent -s)
Agent pid 20168
5. $ ssh-add /home/amit/Keys.txt
Enter passphrase for /home/amit/Keys.txt:
Identity added: /home/amit/Keys.txt
([email protected])
---------------------------------------------------------------------------
6. //=====> Add the public key to key to git hub account in below path
Path : https://github.com/settings/keys
//Once above step is success, Then only below step will be success
------------------------------------------------------
7. // Below is for Check SSH Connection:
// Test your SSH connection to GitHub to see if it's working:
$ ssh -T [email protected]
Hi NadigerAmit! You've successfully authenticated, but GitHub does not provide shell access.
From here onwards the actual process of modifying the source file and pushing them to git will start.
// 1. Modify the source code
// 2. Check the git status :
$git status
//3. Add the modified file to git commit
$git add wallet/src/main.rs
//4. Commit the changes :
$git commit -m "Test commit"
5. // Update Git Remote URL: Example below :
$git remote set-url origin [email protected]:NadigerAmit/Tuxedo-experimentation.git
6. // Push the code to remote repository
$git push
Enumerating objects: 9, done.
Counting objects: 100% (9/9), done.
Delta compression using up to 4 threads
Compressing objects: 100% (5/5), done.
Writing objects: 100% (5/5), 438 bytes | 219.00 KiB/s, done.
Total 5 (delta 4), reused 0 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (4/4), completed with 4 local objects.
To github.com:NadigerAmit/Tuxedo-experimentation.git
5eb9b98..c60dead main -> main
Thanks for reading till end . Please comment if you have any .