Frequently used git commands

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

Generate SSH Key Pair:

If you don't already have an SSH key pair, you can generate one using the Git Bash terminal that comes with Git:

  • Open Git Bash from the Start menu.
  • Run the following command to generate a new SSH key pair:

ssh-keygen -t rsa -b 4096 -C "[email protected]"         

  • Replace "[email protected]" with your actual email address.
  • Choose a location to save the key pair. The default location is usually C:\Users\YourUsername\.ssh\.

Add SSH Key to Bitbucket:

  • Log in to your Bitbucket account.
  • Go to your Bitbucket settings.
  • Under "Security," find the "SSH keys" section.
  • Click on "Add key."
  • Open the public key file you generated (id_rsa.pub) using a text editor.
  • Copy the contents of the public key and paste it into the "Key" field on Bitbucket.
  • Give your key a recognizable label.
  • Click "Add key."

Configure Git:

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]"         

Test SSH Connection:

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.

Clone a Repository:

  • On Bitbucket, go to the repository you want to clone.
  • Click on the "Clone" button and select "Clone with SSH."
  • Copy the SSH URL of the repository.
  • In Git Bash, navigate to the directory where you want to clone the repository and run the following command:

git clone <repository-ssh-url>         

  • Replace <repository-ssh-url> with the URL you copied.

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         

Commit Changes:

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:

  1. git fetch: Fetch updates from the remote repository to see if there are any new commits or branches.
  2. git branch -vv: Use this command to see the relationships between your local branches and their remote counterparts.
  3. git branch --set-upstream-to=origin/remote-branch: Set the upstream tracking branch for your local branch if necessary.
  4. Make changes to your code in your local branch.
  5. git add: Stage the changes you want to commit.
  6. git commit: Create a new commit with the staged changes and a meaningful commit message.
  7. git fetch: Fetch updates from the remote repository again to ensure your local information is up to date.
  8. git pull: If your local branch is behind the remote, pull changes to update your local branch.
  9. git push: Push your committed changes to the remote repository.
  10. git merge: Merge changes from one branch to another if needed.



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 .

要查看或添加评论,请登录

Amit Nadiger的更多文章

  • List of C++ 17 additions

    List of C++ 17 additions

    1. std::variant and std::optional std::variant: A type-safe union that can hold one of several types, useful for…

  • List of C++ 14 additions

    List of C++ 14 additions

    1. Generic lambdas Lambdas can use auto parameters to accept any type.

    6 条评论
  • Passing imp DS(vec,map,set) to function

    Passing imp DS(vec,map,set) to function

    In Rust, we can pass imp data structures such as , , and to functions in different ways, depending on whether you want…

  • Atomics in C++

    Atomics in C++

    The C++11 standard introduced the library, providing a way to perform operations on shared data without explicit…

    1 条评论
  • List of C++ 11 additions

    List of C++ 11 additions

    1. Smart Pointers Types: std::unique_ptr, std::shared_ptr, and std::weak_ptr.

    2 条评论
  • std::lock, std::trylock in C++

    std::lock, std::trylock in C++

    std::lock - cppreference.com Concurrency and synchronization are essential aspects of modern software development.

    3 条评论
  • std::unique_lock,lock_guard, & scoped_lock

    std::unique_lock,lock_guard, & scoped_lock

    C++11 introduced several locking mechanisms to simplify thread synchronization and prevent race conditions. Among them,…

  • Understanding of virtual & final in C++ 11

    Understanding of virtual & final in C++ 11

    C++ provides powerful object-oriented programming features such as polymorphism through virtual functions and control…

  • Importance of Linux kernal in AOSP

    Importance of Linux kernal in AOSP

    The Linux kernel serves as the foundational layer of the Android Open Source Project (AOSP), acting as the bridge…

    1 条评论
  • AOSP

    AOSP

    Android System Development AOSP stands for the Android Open Source Project. Its the foundation of the Android operating…

社区洞察

其他会员也浏览了