🗒️ Git/GitHub Tips
Git is a distributed version control system widely used for tracking changes in source code during software development. Below are the basic commands and concepts to get started with Git:
Configuration
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"
Basic Commands
After entering the working directory:
Initialize a Repository
git init # This creates a new Git repository in the current directory.
Clone a Repository
git clone <repository_url> # This copies an existing Git repository (e.g. GitHub repo) to your local machine.
Check Status
git status # This shows the current status of your working directory and staging area.
# Including: which Branch we are at, Changes not staged for commit, and Untracked files.
Add Changes
git add <file_name> # This adds changes to the staging area.
git add . # We can use . to add all changes.
The staging area (also known as the index) in Git is an intermediate area where changes are stored before they are committed to the repository. It acts as a preparatory zone where you can group changes together and decide what will be included in the next commit. When you make changes to files, they are initially tracked in the working directory. To include these changes in your next commit, you use the git add command, which moves them from the working directory to the staging area. Only the changes in the staging area are included in the next commit. This allows you to review and modify what you’re about to commit.
Commit Changes
git commit -m "Commit message" # It records changes in the repository with a descriptive message.
View Commit History
git log # It displays a log of all commits in the current branch.
Back to a Previous Commit
To view or temporarily go back to a previous commit:
git checkout <commit-hash>
This command checks out the specific commit, putting your repository into a “detached HEAD” state, meaning you are not on any branch. This is useful for inspecting the code at that point in time, but any changes made won’t be saved to a branch unless you create one.
To undo a specific commit (i.e., create a new commit that undoes the changes from a previous commit):
git revert <commit-hash> # create a new commit that reverses the changes
To permanently move the HEAD and the current branch pointer to a previous commit:
git reset --soft/mixed/hard <commit-hash>
soft
: Keeps the changes in the working directory and staging area (index).mixed
: Keeps the changes in the working directory but clears the staging area (default behavior).hard
: (Be Cautious!) Discards all changes in the working directory and staging area.
Branching and Merging
Create a New Branch
git branch <branch_name> # It creates a new branch for development.
Switch to a Branch
git checkout <branch_name> # It switches to the specified branch.
Merge a Branch
git merge <branch_name> # It merges changes from the specified branch into the current branch.
# Typical usage:
git checkout <target_branch>
git merge <source_branch>
(Optional) Rebase
Conflit Handling
Remote Repositories
Before starting operating the remote repository, one should notice that the remote repository and local repository are two different repositories. They may have different commits and branches. One should setup which remote branch to track (which is set to origin/main
by default), and track that branch manually by commands such as push
and pull
.
Add a Remote
A remote server should be specified before the first push.
git remote add <name> <repository_url> # It links your local repository to a remote repository.
It adds a reference to a remote repository named name
. This is typically used to push changes to or fetch updates from that remote repository. The URL specified in repository_url
should point to an existing remote repository.
Push Changes
git push <remote_name> <branch_name> # It uploads your local commits to the remote repository.
remote_name
: The name of the remote repository to which you want to push your changes (default: origin
).branch_name
: The name of the local branch you want to push to the remote repository.
Attention: Prior to Git version 2.28 (released in 2020), the default local branch was named
master
. However, remote default branch ismain
. One should usegit push origin master:main
to push local default branch to remote default branch.
For the first push:
git push -u/--set-upstream origin main
This not only pushes your changes but also sets origin/main as the default upstream branch for future push and pull operations.
Use the –all option to push all local branches to the remote repository:
git push origin --all
Use the --force
(or -f
) option to forcefully push changes. This can overwrite commits on the remote branch and should be used with caution.
git push --force origin main
Push a local branch to a different branch on the remote.
git push origin local-branch:remote-branch
Pull Changes
git pull origin <branch_name>
<branch_name>
: The name of the branch on the remote repository you want to pull changes from.
This command performs two actions:
- Fetch: Retrieves changes from the remote repository.
- Merge: Merges the fetched changes into your current local branch.
If you only need specific files from the remote branch without switching branches or merging, one might use a combination of fetch and checkout commands:
git fetch origin
git checkout origin/branch_name -- <file_path>
There might be conflicts during pull
. These conflicts should be resolved and then merge
or rebase
.
To apply changes from the remote branch onto your current branch with a linear history, you can use --rebase
with git pull:
git pull --rebase origin <branch_name>
This rebases your local commits on top of the changes from the remote branch, avoiding a merge commit.
Branch Management
Local and Remote Branch Management