Certainly, here's the expanded list of commands for the "Getting Started" chapter, including how to install Git on Debian, Fedora, and Windows:
Getting started with Git
- Getting Started:
-
Installing Git:
- Debian/Ubuntu:
sudo apt update sudo apt install git
- Fedora:
sudo dnf install git
- Windows: Download the Git installer from git-scm.com (opens in a new tab) and follow the installation instructions.
- Debian/Ubuntu:
-
Configuring Git:
git config --global user.name "Your Name" git config --global user.email "your.email@example.com"
-
Initializing a repository:
git init
-
Cloning a repository:
git clone <repository-url>
-
Basic Git workflow:
- Add changes to the staging area:
git add <file>
- Commit changes to the repository:
git commit -m "Commit message"
- Push changes to a remote repository:
git push <remote> <branch>
- Pull changes from a remote repository:
git pull <remote> <branch>
- Add changes to the staging area:
-
First time set-up
Now that you have Git on your system, you’ll want to customize your Git environment. These configurations are typically done once and persist between upgrades. You can modify them at any time by running the appropriate commands.
Git provides a tool called git config
to manage configuration variables controlling Git's behavior. These variables are stored in different places:
-
[path]/etc/gitconfig: Applies to every user on the system and all repositories. Use
--system
option to interact with this file. Administrative privileges are required to modify it. -
~/.gitconfig or ~/.config/git/config: Personal configurations for the user. Use
--global
option to interact with this file, affecting all repositories. -
.git/config in the repository directory: Repository-specific configurations. This is the default, and you need to be inside a repository for this option to work.
Each level of configuration overrides the previous one, with repository configurations taking precedence over global configurations.
On Windows, Git looks for .gitconfig
in the $HOME
directory (typically C:\Users\$USER
). It also checks [path]/etc/gitconfig
, relative to the MSys root. For Git versions 2.x and later, there's a system-level config file at C:\ProgramData\Git\config
on Windows Vista and newer.
To view all settings and their sources:
$ git config --list --show-origin
## Setting Your Identity
Upon installing Git, it's crucial to configure your user name and email address. These details are integral as they are attached to every Git commit you make:
```bash
$ git config --global user.name "John Doe"
$ git config --global user.email johndoe@example.com
This setup needs to be done just once with the --global
option. Subsequently, Git will utilize this information for all actions on that system. However, if you wish to use different credentials for specific projects, you can omit the --global
option when configuring.
Most GUI tools offer assistance with this configuration upon first use.
Choosing Your Text Editor
After setting up your identity, you can define the default text editor Git should employ when it requires input for a message. By default, Git uses the system's default editor.
To specify a different text editor, like Emacs for instance, execute the following command:
$ git config --global core.editor emacs
On Windows, if you opt for a different text editor, you must provide the full path to its executable file. The path can vary based on the packaging of your editor. Fpr notepadd++
git config --global core.editor "'C:/Program Files/Notepad++/notepad++.exe' -multiInst -notabbar -nosession -noPlugin"
Note Given that Git may read the same configuration variable value from multiple files, it's possible to encounter unexpected values for certain configurations without understanding their source. In such scenarios, you can utilize Git to determine the origin of a value. Git will furnish details about which configuration file ultimately influenced that value.
To inquire about the origin of a specific configuration value, utilize the following command:
$ git config --show-origin rerere.autoUpdate
Getting Help
When you require assistance with Git, there are three methods to access the comprehensive manual page (manpage) help for any Git command:
$ git help <verb>
$ git <verb> --help
$ man git-<verb>
For instance, to access the manpage help for the git config
command, execute:
$ git help config
These commands offer convenience as they are accessible anywhere, including offline environments. If the manpages and available resources are insufficient, and you seek in-person guidance, consider joining the #git, #github, or #gitlab channels on the Libera Chat IRC server, accessible at Libera Chat (opens in a new tab). These channels boast numerous knowledgeable Git enthusiasts willing to provide assistance.
Furthermore, if you only require a brief overview of available options for a Git command, you can request concise "help" output using the -h
option, for example:
Renaming Git Branches
To rename the "master" branch in your local Git repositories, follow these steps:
$ git branch -m master main
Verify the rename:
$ git status
On branch main
Your branch is up to date with 'origin/master'.
nothing to commit, working tree clean
Renaming the Remote Branch
Since Git doesn't allow directly renaming a remote branch, follow these steps to rename the remote branch:
- Create a new branch named "main" on the remote:
$ git push -u origin main
- Delete the old "master" branch on the remote:
$ git push origin --delete master
With these steps, you've successfully renamed both the local and remote branches from "master" to "main".
Git Basics
Getting a Git Repository
You typically obtain a Git repository in one of two ways:
- You can initialize a Git repository in a local directory that is currently not under version control.
- Alternatively, you can clone an existing Git repository from elsewhere.
In either case, you end up with a Git repository on your local machine, ready for work.
Initializing a Repository in an Existing Directory
If you have a project directory that is currently not under version control and you want to start controlling it with Git, you first need to navigate to that project’s directory. Depending on your operating system, the command varies:
- For Linux:
$ cd /home/user/my_project
- For macOS:
$ cd /Users/user/my_project
- For Windows:
$ cd C:/Users/user/my_project
Then, you should initialize the directory as a Git repository by typing:
$ git init
This creates a new subdirectory named .git
that contains all necessary repository files. At this point, none of your project files are tracked yet. You can use git add
to start tracking files and then commit them:
$ git add *.c
$ git add LICENSE
$ git commit -m 'Initial project version'
Cloning an Existing Repository
If you want to obtain a copy of an existing Git repository, you can use the git clone
command. This command retrieves a full copy of nearly all data from the server. For example, to clone the Git linkable library called libgit2, you would use:
$ git clone https://github.com/libgit2/libgit2
This command initializes a .git
directory inside a directory named libgit2
, pulls down all the data for that repository, and checks out a working copy of the latest version.
If you prefer to clone the repository into a directory with a different name, you can specify the new directory name as an additional argument:
$ git clone https://github.com/libgit2/libgit2 mylibgit
Git supports various transfer protocols, such as https://
, git://
, or user@server:path/to/repo.git
(SSH transfer protocol). Each has its pros and cons, which you can explore further in documentation on setting up Git on a server.
Common Git Commands
This table summarizes common Git commands along with their subcommands and example usages:
Git Command | Subcommands (what they do) | Description | Example Usages |
---|---|---|---|
git init | - | Initializes a directory for a Git project. | - |
git add <file/dir> | - | Starts tracking files and adds them to the staging area. | git add myfile.txt , git add . |
git status | - | Checks status of your project. | - |
git commit -m <message> | -m <message> (specify commit message) | Creates a local snapshot with a message describing the changes. | git commit -m "Added new feature" , git commit -m "Fix typo" |
git push <params> | - | Pushes local snapshot (commits) to a remote repository. | git push origin master , git push origin main |
git pull <params> | - | Patches and merges changes from the remote repository (combines git fetch and git merge ). | git pull origin master , git pull origin main |
git clone <params> | - | Copies another (remote) project to your local machine. | git clone https://github.com/example/repository.git , git clone git@github.com:example/repository.git |
git diff <params> | - | Similar to the Linux diff command, git diff shows changes between commits, working tree, and other files and objects. | git diff , git diff myfile.txt |
git checkout <branch> | <branch> (specify the branch to switch to or file to restore) | Switches branches or restores working tree files. | git checkout develop , git checkout myfile.txt |
git branch <name> | <name> (specify the name of the branch to create or delete) | Lists, creates, or deletes branches. | git branch , git branch feature , git branch -d feature |
git merge <branch> | <branch> (specify the branch to merge into the current branch) | Joins two or more development histories together. | git merge feature , git merge hotfix |
git log | - | Displays commit logs. | - |
git reset <file> | <file> (specify the file to reset) | Resets current HEAD to the specified state. | git reset myfile.txt |
git revert <commit> | <commit> (specify the commit to revert) | Reverts one or more commits. | git revert abc123 , git revert HEAD~2 |
git stash | - | Temporarily shelves changes. | git stash , git stash save "Work in progress" |
git remote <params> | <params> (e.g., add , remove , show ) | Manages set of tracked repositories. | git remote -v , git remote add origin https://github.com/example/repository.git |
git fetch <params> | <params> (e.g., origin , --all , --tags ) | Retrieves commits, files, and refs from a remote repository. | git fetch origin , git fetch --tags |
git tag <params> | <params> (e.g., create , list , delete , verify ) | Creates, lists, deletes or verifies a tag object signed with GPG. | git tag , git tag -l , git tag -d v1.0 , git tag -v v1.0 |
git config <params> | <params> (e.g., --global , --local , --system , --unset ) | Sets configuration values for your user, your repo, or Git in general. | git config --global user.name "John Doe" , git config --local core.autocrlf true |
git remote show <remote> | <remote> (specify the remote to show information for) | Displays information about a given remote. | git remote show origin , git remote show upstream |
git submodule <params> | <params> (e.g., add , init , update ) | Manages project dependencies stored as Git repositories. | git submodule add https://github.com/example/submodule.git , git submodule update --init --recursive |
git rebase <branch> | <branch> (specify the branch to rebase onto) | Reapplies commits on top of another base tip. | git rebase master , git rebase feature |
Git Log: Useful specifiers for git log --pretty=format
This table provides descriptions of output specifiers for formatting git log
output.
Specifier | Description of Output |
---|---|
%H | Commit hash |
%h | Abbreviated commit hash |
%T | Tree hash |
%t | Abbreviated tree hash |
%P | Parent hashes |
%p | Abbreviated parent hashes |
%an | Author name |
%ae | Author email |
%ad | Author date (format respects the --date=option ) |
%ar | Author date, relative |
%cn | Committer name |
%ce | Committer email |
%cd | Committer date |
%cr | Committer date, relative |
%s | Subject |
example
git log --pretty=format:"%h - %an, %ar : %s"
Common options to git log
This table outlines common options for customizing the output of git log
.
Option | Description |
---|---|
-p | Show the patch introduced with each commit. |
--stat | Show statistics for files modified in each commit. |
--shortstat | Display only the changed/insertions/deletions line from the --stat command. |
--name-only | Show the list of files modified after the commit information. |
--name-status | Show the list of files affected with added/modified/deleted information as well. |
--abbrev-commit | Show only the first few characters of the SHA-1 checksum instead of all 40. |
--relative-date | Display the date in a relative format (for example, “2 weeks ago”) instead of using the full date format. |
--graph | Display an ASCII graph of the branch and merge history beside the log output. |
--pretty | Show commits in an alternate format. Option values include oneline , short , full , fuller , and format (where you specify your own format). |
--oneline | Shorthand for --pretty=oneline --abbrev-commit used together. |
Git Branching
Managing Git Branches
In software development, Git branches are essential for managing new features and bug fixes. Developers create separate branches for each new feature or bug fix. After completing their work, they push the branch to a remote repository and create a pull request to merge their changes into the main branch.
Teams often conduct peer reviews before merging a branch, ensuring adherence to project standards and discussing improvements. Automated tests verify that new code doesn't break existing functionality. Once peer review and tests pass, the pull request is approved, and changes are merged into the main branch, integrating new features or fixes into the codebase.
To manage branches effectively in larger projects, consider the example of Cisco IOS Software. Branches can contain relevant code for specific features or bug fixes and be merged into the main branch when ready.
Branch Navigation and Creation
In Git, use commands like git branch
, git checkout
, and git merge
. To navigate among branches:
$ git branch
* main
Create a new branch and switch to it:
$ git checkout -b fix_aaa_bug
Switched to a new branch 'fix_aaa_bug'
To fix a bug, create a branch and navigate to it:
$ git checkout -b fix_aaa_bug
$ git branch
* fix_aaa_bug
main
Workflow for Branch Management
After fixing a bug in a branch, merge it back into the main branch:
$ git checkout main
$ git merge fix_aaa_bug
Confirm the merge:
$ ls
aaa.cfg newdir s1.cfg s2.cfg
Forking and Collaboration
On Git hosting services, forking allows individuals to obtain a full replica of a project they don't own. Forking creates a copy of the original repository, enabling contributions without ownership.
Collaboration in Git repositories involves pull requests, enabling contributions to projects. After pushing changes to a forked repository, a pull request can be opened to contribute back to the original project. Code reviews facilitate communication between project owners and contributors, ensuring quality contributions.