Git add and staging area
Commit is for permanently saving your changes to the project’s history, marking a new point in the development timeline.
git add <file>
# Adds a file to the staging area.git add .
# Adds all files in the current directory to the staging area.git commit -m "<message>"
# Commits the staging area to the repository with the given message.
Git log and history
git log --graph --oneline
# Shows a concise, one-line representation of the commit history with a graphical view.git log --graph --oneline --decorate --all --simplify-by-decoration
# Displays the commit history with a graphical view, showing only commits with tags and branches. The --decorate option adds information about branches and tags.git reflog
# Shows a reference log, which includes all the Git references (branches, tags, etc.) and their associated commit history. Useful for recovering lost commits.
Git head to undo changes
git reset --hard <commit>
# Resets the current branch and working directory to the specified commit. Discards all changes after that commit.git reset --hard origin/master
# Resets the current branch to the state of the remote master branch. Useful for discarding local changes and syncing with the remote repository.git reset --hard HEAD~1
# Resets the current branch and working directory to the previous commit (one commit before the current HEAD).git reset --soft <commit>
# Resets the current branch to the specified commit but keeps the changes in the working directory. The changes will appear as uncommitted modifications.
Git branches
git branch --list
# Lists all the local branches in the current repository.git branch <branch>
# Creates a new branch with the given name.git checkout <branch>
# Switches to the specified branch.git checkout -b <branch>
# Creates a new branch with the given name and switches to it.git branch -d <branch>
# Deletes the specified branch.git branch -m <branch>
# Renames the current branch to the specified name.
Git merge
git merge <branch>
# Merges the specified branch into the current branch (the one that HEAD points to).git stash
# Temporarily stores all modified tracked files.
Git stash
Stash is for temporarily saving your progress when you need to switch contexts without committing unfinished work.
git stash list
# Lists all stashed changesets.git stash show -p <stash>
# Shows the changeset recorded in the stash as a patch.git stash apply <stash>
# Applies the changeset recorded in the stash to the working directory.git stash pop
# Removes and applies the changeset last saved with git stash.
Git mv and rm
git rm <file>
# Removes a file from the working directory and staging area.git rm --cached <file>
# Removes a file from the staging area only. After that do git commitgit mv <file-original> <file-renamed>
# Renames a file.
Git remote
git remote add origin <url>
# Adds a remote repository to the current Git project.git remote -v
# Lists all currently configured remotes.git remote show <remote>
# Shows information about the specified remote.git remote rename <remote> <new-name>
# Renames the specified remote.git remote remove <remote>
# Removes the specified remote.git push -u origin master
# Pushes the master branch to the origin remote and sets it as the default upstream branch for master.git push origin --delete <branch>
# Deletes a remote branch.git push
# Pushes the current branch to the remote repository.git pull
# Pulls the latest changes from the remote repository into the current branch.
Working with Submodules
Let's go through a complete example workflow:
-
Initial Setup:
# Navigate to the main repository cd ~/path/to/nixos-config # Add the submodule git submodule add https://github.com/username/my-other-repo.git my-other-repo # Initialize and update the submodule git submodule init git submodule update # Commit the submodule addition in the main repository git add .gitmodules my-other-repo git commit -m "Add my-other-repo as a submodule" git push origin main
-
Making Changes in the Submodule:
# Navigate to the submodule directory cd my-other-repo # Make changes and commit them git add . git commit -m "Make some changes in the submodule" git push origin main # Navigate back to the main repository cd .. # Update the submodule reference in the main repository git add my-other-repo git commit -m "Update submodule my-other-repo to latest commit" git push origin main
-
Cloning and Updating:
# Clone the main repository git clone https://github.com/username/nixos-config.git # Navigate into the main repository cd nixos-config # Initialize and update submodules git submodule update --init --recursive
By following these steps, you can effectively manage submodules in your Git repository, ensuring that both the main repository and the submodules are kept up to date.
Git WorkTree
Using git worktree can greatly enhance your workflow efficiency by allowing you to manage multiple branches in parallel with ease.
# Clone the current repository as a bare repository
git clone --bare . /path/to/my-bare-repo.git
# Add a worktree for the main branch in the default location
git worktree add main
# Add a worktree for the dev branch
git worktree add dev
# Remove the worktree for the dev branch when it's no longer needed
git worktree remove dev