Setting Up Git and Essential Version Control Operations

Cloning a Remote Repository

To obtain a project from a remote host, navigate to your desired local directory, right-click, and open your terminal (e.g., Git Bash). Execute the clone command with the repository URL:

git clone https://gitlab.company.com/dev/core-api.git

To clone a specific branch instead of the default main branch, utilize the --branch flag:

git clone --branch feature-login https://gitlab.company.com/dev/core-api.git

You will be prompted for authentication. Most modern platforms require a Personal Access Token (PAT) instead of a standard password. If authentication fails, try clearing or adjusting yourr credential configuration before retrying:

git config --system --unset credential.helper
git config --global http.emptyAuth true

If the repository clones successfully but the checkout fails, enable support for extended file paths:

git config --system core.longpaths true

Caching Credentials

To avoid entering your username and token on every interaction, enable credential storage:

git config --global credential.helper store

After executing this, perform a fetch or pull. Once you enter your credentials one last time, they will be saved locally for future operations.

Synchronizing and Switching Branches

To fetch the latest changes from the remote server, run:

git pull

You can switch branches directly within IDEs like Visual Studio 2022 (via the bottom-right status bar) or Visual Studio Code (via the bottom-left status bar). Alternatively, use the command line. To view all available local and remote branches:

git branch --all

To navigate to a different branch, its recommended to use switch instead of the legacy checkout command:

git switch feature-login

Staging, Committing, and Tracking

When creating new files, Git does not automatically track them. You must explicitly stage them for commit. In IDEs, this often requires right-clicking the file and selecting "Add to Source Control". From the terminal, you can stage all modifications:

git add .

To view the commit history for the current branch:

git log --oneline

To verify the configured remote repository URLs:

git remote --verbose

Recovering Uncommitted Deleted Files

If you accidentally delete a file that was never committed, it will not appear in standard Git recovery logs. You can attempt to recover it from Git's internal object store.

1. Locate the Object

Navigate to the .git/objects directory. Sort the subfolders by modification date to match the approximate time the file was deleted. For instance, if a folder is named a1, enter it.

2. Inspect the Object

Find the file inside that folder (e.g., a file named b2c3d4). Open your terminal here and combine the folder name and filename to form the full hash. Use cat-file to inspect its contents:

git cat-file -p a1b2c3d4e5f67890123456789abcdef12345678

Review the terminal output. If the content matches your deleted file, proceed to the next step. If not, continue checking other timestamped objects.

3. Restore the Object

Redirect the object's content to a new file at your desired location to complete the recovery:

git cat-file -p a1b2c3d4e5f67890123456789abcdef12345678 > /c/workspace/recovered_script.js

Troubleshooting Common Errors

Filename Too Long

Windows has a default path length limit that can cause Git to fail. To bypass this restriction globally:

git config --global core.longpaths true

Expired Access Token

You may encounter an error such as: HTTP Basic: Access denied. The provided password or token is incorrect or your account has 2FA enabled... This indicates your Personal Access Token has expired. You must generate a new one from your Git provider's settings (note that tokens are usually only visible once upon creation).

To update your stored credentials, you can either:

  • Open the Windows Credential Manager, find the git:https://... entry, and update the password with your new token.
  • Reset the credential helper via terminal, which will prompt you for the new token on the next action: ``` git config --system --unset credential.helper
    
    After entering your new credentials, ensure they are saved by reapplying the store command, or by letting your IDE's Git extension cache them automatically: ```
    git config --global credential.helper store
    

Tags: Git Version Control Git Bash Visual Studio VS Code

Posted on Tue, 19 May 2026 12:06:48 +0000 by trparky