Git: A Starter Guide

 

 

Git is a free and open-source version control system that tracks changes in a file system by supporting data integrity for multiple parallel workflows on different computers. It is used for Source Control Management (SCM), where changes can be monitored and managed over time. Some git repository services include GitHub, GitLab, and BitBucket.

Developers have a local copy of the repository's development history. Changes are imported as branches are merged and all changes are reflected in all linked repositories. This distributed development workflow supports non-linear development with rapid branching and merging as various users access the data.

 

Installation

git clone https://github.com/git/git

 

 

Launch

 

 

Configure Your Git Profile

 

Git Config | Configure Your Username & Email | Learn Git

 

git config --<git_configuration_level> <configuration_key> "<configuration_key_value>" ...

 

 

git config --global user.name "<your_name>"

 

git config --global user.email "<your_email>"

 

git config --global init.defaultBranch <default_branch_name>

 

 

git help config

 

git config -h

 

git config --<git_configuration_level> --unset <configuration_key>

 

 

Setting Up Your Local Repository

cd <local_repo_file_path>

 

 



nano readme.txt

 



Initialize Your Local Repository as a Git Repository

git init

 



Local Repository Contents:

pwd

 

ls



ls -a or ls -la

 



Terminology

 

 

List Files in Working Directory for Commit

git status

 



 

 

Add File to Repository

git add readme.txt

 

git add -A or git add --all

 

git add .

 

 



git status

 



git restore --staged <file_name>

 

 

Rename Files in Repository

git mv "<old_file_name>" "<new_file_name>"

 

 

Commit Changes to File(s) in Repository

git commit -m "<meaningful_message_about_committed_change>"

 

 

 

 

git commit -a

 

NOTE: This will not work with files that have not previously been added

 

git commit -m "<meaningful_message_about_committed_change>" --amend

 

git status

 

 



Track Commit History

git log

 

or

 

git reflog

Exit terminal command with ‘q’

 

git log --oneline

Exit terminal command with ‘q’

 

git log -p

 Exit terminal command with ‘q’

 

git log --all --graph

Exit terminal command with ‘q’

 

or

 

git log --all --graph --decorate --oneline

Exit terminal command with ‘q’

 

git log --author="<developer_name>"

Exit terminal command with ‘q’

 

git log -S "<commit_message_to_search_by>"

Exit terminal command with ‘q’

 


Return to a Previous Commit Instance

git reset <commit_tag>

Commit tags are the yellow string of characters to the left of a commit instance in the git logs

 

 

Tracking Changes to Repository Files

git status

 

A computer screen shot of text

AI-generated content may be incorrect.

 

git diff

 

 

git commit -a -m “<meaningful_message_about_committed_change>”

git status

 

A computer screen with text on it

AI-generated content may be incorrect.



Ignoring Files in the Repository

A computer screen with text on it

AI-generated content may be incorrect.



nano .gitignore

 

A screen shot of a computer

AI-generated content may be incorrect.

 

A computer screen with text on it

AI-generated content may be incorrect.

 

A computer screen shot of text

AI-generated content may be incorrect.

 

Deleting Repository Files



Alternatively,



git rm <file_name_to_delete>.<file_extension> or git rm *.<file_extension>

 

git commit -m "<meaningful_commit_message_about_deletion>"

 

git restore <file_name>

 

 

Repository Branches

git branch

 

git branch <new_branch_name>

 

git switch <branch_name>

 

Or

 

git checkout <branch_name>

 





git switch -c <new_branch_name> or git checkout -b <new_branch_name>

 

git branch -m <old_branch_name> <new_branch_name>



git branch -d <branch_to_delete_name>

 

o    NOTE: The branch that is currently checked out cannot be deleted. Navigate into another branch that is not the one to be deleted first.

o    NOTE:  If a branch in a remote repository tracks the local branch that is being deleted, the remote branch must be deleted before the local branch is deleted.

 

git push -u <remote_server_alias> <branch_name>

 

git checkout <old_branch_name>

 

git branch -m <old_branch_name> <new_branch_name>

NOTE: The branch is now renamed locally. No further steps needed for the local branch rename

 

git push -u <remote_server_alias> <new_branch_name>

 

git push <remote_server_alias> --delete <old_branch_name>

NOTE: The renamed branch does NOT need to be deleted locally - the local branch is directly renamed. The renamed local branch that is pushed to the remote repository will completely replace the remote branch with the old name

 

git push <remote_server_alias> --delete <branch_to_delete_name>

 

or

 

git push <remote_server_alias> :<branch_to_delete_name>

 

 

Merge Branches

·         Changes made in one branch can be reflected in another branch with the 'merge' command, bridging a repository's forked history.

 

A screen shot of a black background

AI-generated content may be incorrect.

 

git switch <branch_B> or git checkout <branch_B>

git merge -m "<meaningful_message_about_merge>" <branch_A>

 

o    Branch (B) (target) will inherit changed features from branch (A) (source), making no changes to branch (A) and merging the repositories' forked history.

 

 

git checkout <destination_branch>

git checkout <source_branch> -- <file_name>

git add <file_name>

git commit -m "<meaningful_commit_message>"

 

 

Rebase

<> 



Setup Remote Repositories

 

A screenshot of a computer

AI-generated content may be incorrect.

A screenshot of a computer

AI-generated content may be incorrect.



Create a New Repository from the Command Line

git init

 

git add README.md

 

git commit -m “<meaningful_message_about_commit>”

 

git branch -M <branch_name>

 

git remote add <remote_server_alias> <repository_url>

 

o    A Remote Server Alias can be thought of as a variable that contains the remote repository's URL so that you don't have to manually input it in commands thereafter

o    A common server alias name is 'origin'

 

git remote rename <old_remote_server_alias> <new_remote_server_alias>

 

git remote -v



Push Existing Local Repository from the Command Line

git remote add <remote_server_alias> <remote_url>

 

git remote -v

 

git branch -M <branch_name>

 

git push -u <remote_server_alias> <branch_name>



Pull Remote Repository's Current State Data Into Local Repository:

git fetch

 

or

 

git fetch <remote_server_alias> <branch_name>

 

This allows you to view available changes without affecting your working directory

 

git merge

 

 

git pull

 

or

 

git pull <remote_server_alias> <branch_name>

 

o    NOTE: 'Pull' cannot be performed if the user is actively editing any tracked files in the local workspace that conflicts with the remote repository's contents.

o    NOTE:  If tracked files are being edited while performing a 'pull', the 'pull' will fail and the remote repository files will, alternatively, be 'fetched'.

 

 

Pull = Fetch + Merge

 

A black and white screen with text

AI-generated content may be incorrect.



Fork vs Clone

git clone <source_repository_url>

 

 

 

Pull Requests

·         Remote repositories are often collaborative spaces with multiple contributors. When a developer is ready merge new code changes with the main project branch, a pull request (also known as a merge request) is initiated to merge the developer's workspace with main. The steps below outline a standard pull request workflow.

 



Change the Remote Repository URL to which a Local Repository is Linked

git remote set-url <current_remote_server_alias> <new_repositoru_url>

 

 

Disconnect a Local Repository from GitHub/Remote Repository

git remote remove <remote_server_alias>



Delete the Local Git Repository

git -rf .git



Personal Access Tokens

git remote set-url <remote_server_alias> https://<repo_contributor_username>:<personal_access_token>@github.com/<repo_owner_username>/<repository>.git

 

git config --global --unset credential.helper

 

 

 

Helpful Resources