9 min read•august 14, 2024
Git fundamentals and best practices are essential for effective version control. This section covers everything from setting up Git and managing repositories to performing basic operations like staging, committing, and pushing changes. It also dives into branching, merging, and resolving conflicts.
The best practices outlined here emphasize clear communication through , following branching conventions, and maintaining a stable main . These practices, along with effective collaboration techniques and organization, form the backbone of successful Git usage in development projects.
brew install git
)apt-get install git
on Debian-based systems or yum install git
on Red Hat-based systems)git config
commandgit config --global user.name "Your Name"
and git config --global user.email "your@email.com"
git config --global core.editor "editor-name"
(e.g., vim
, nano
, code
for Visual Studio Code)--system
), user-level (--global
), or repository-specific (no flag)ssh-keygen -t rsa -b 4096 -C "your@email.com"
, which creates a private key and a public keyid_rsa.pub
) should be added to the Git hosting platform's SSH key settings, while the private key remains on the local machinegit init
command or cloned from a remote repository using git [clone](https://www.fiveableKeyTerm:Clone)
git init
, which creates a hidden .git
directory containing the repository's metadatagit clone <repository-url>
, which creates a new directory with the repository's name and copies the entire and filesgit remote
command is used to manage connections to remote repositories, allowing developers to add, remove, or modify remote URLsgit remote add <name> <url>
, where <name>
is a shorthand name for the remote (e.g., origin
) and <url>
is the remote repository's URLgit fetch <remote>
, which downloads the latest changes without merging them into the local branchgit pull <remote> <branch>
, which fetches the latest changes and merges them into the specified local branchgit add
moves modified files from the working directory to the staging area, preparing them for committinggit add <file>
or all modified files can be staged at once using git add .
git commit
records a snapshot of the staged changes in the local repository's commit history, along with a commit message describing the changes-m
flag, as in git commit -m "Commit message here"
git status
command provides an overview of the current state of the repository, showing modified, staged, and untracked filesgit [push](https://www.fiveableKeyTerm:push)
uploads the committed changes from the local repository to a remote repository, making them available to other collaboratorsgit push <remote> <branch>
, where <remote>
is the name of the remote repository (e.g., origin
) and <branch>
is the name of the branch being pushedgit pull
fetches the latest changes from a remote repository and merges them into the current branch of the local repositorygit pull <remote> <branch>
git [log](https://www.fiveableKeyTerm:Log)
command is used to view the commit history of a repository, displaying information such as the commit hash, author, date, and commit messagegit log
displays the commits in reverse chronological order, with the most recent commit appearing firstgit log
to customize the output, such as --oneline
for a condensed view, --graph
for a visual representation of the branch structure, and --decorate
for showing branch and tag namesgit show <commit>
command can be used to view the details of a specific commit, including the changes made in that commitgit branch
command is used to create, list, rename, and delete branchesgit branch <branch-name>
, where <branch-name>
is the desired name for the new branchgit branch
or git branch --list
, which displays all local branches and highlights the current branch with an asteriskgit branch -m <old-name> <new-name>
, while deleting a branch is done using git branch -d <branch-name>
git checkout
command, which updates the working directory to match the selected branchgit checkout <branch-name>
, where <branch-name>
is the name of the branch you want to switch togit checkout -b <branch-name>
, which creates the new branch and immediately switches to itgit merge
commandgit merge <source-branch>
, where <source-branch>
is the name of the branch containing the changes to be merged<<<<<<<
, =======
, and >>>>>>>
)git add
and create a new commit to complete the merge using git commit
git mergetool
can be used to launch a graphical merge tool, which can help visualize and resolve conflicts more easilymain
and develop
) and short-lived branches for features (feature/
), releases (release/
), and hotfixes (hotfix/
)main
branch and short-lived feature branches, and the GitLab Flow, which introduces environment branches (e.g., production
, staging
) for deploymentfeature/user-authentication
for a feature branch or hotfix/login-bug
for a hotfix branchv1.0.0
, release-2021-03-15
)