SETUP (Configuring user information used across all local repositories):
Set global username and email for git [locally].
git config --global
user.name
“[firstname lastname]”
git config --global
user.email
“[valid-email]”
SETUP & INIT: Configuring user information, initializing and cloning repositories
git init
Initialize an existing directory as a Git repository.
git clone [url]
Retrieve an entire repository from a hosted location via UR.
STAGE & SNAPSHOT: Working with snapshots and the Git staging area
git status
Show modified files in working directory, staged for your next commit.
git add [file]
Add a file as it looks now to your next commit (stage).
git reset [file]
Unstage a file while retaining the changes in working directory.
git diff
Diff of what is changed but not staged.
git diff --staged
Diff of what is staged but not yet commited.
git commit -m “[descriptive message]”
Commit your staged content as a new commit snapshot
BRANCH & MERGE: Isolating work in branches, changing context, and integrating changes
The product is the same, so there is one repository but different tasks. Each task has a separate branch. Finally, merge all branches so changes are present in the particular branch. The default branch is "Master". A file created in the workspace will be visible in any branch's workspace until you commit; once you commit, that file belongs to that particular branch. After finishing the code, merge other branches with 'Master'. This concept is useful for parallel development. You can create any number of branches. When a new branch is created, data from the existing branch is copied to the new branch.
git branch
List your branches. a * will appear next to the currently active branch.
git branch [branch-name]
Create a new branch at the current commit.
git checkout
Switch to another branch and check it out into your working directory.
git merge [branch]
Merge the specified branch’s history into the current one.
git log
Show all commits in the current branch’s history.
SHARE & UPDATE: Retrieving updates from another repository and updating local repos
git remote add [alias] [url]
Add a git URL as an alias.
git fetch [alias]
Fetch down all the branches from that Git remote.
git merge [alias]/[branch]
Merge a remote branch into your current branch to bring it up to date.
git push [alias] [branch]
Transmit local branch commits to the remote repository branch.
git pull
Fetch and merge any commits from the tracking remote branch.
REWRITE HISTORY: Rewriting branches, updating commits and clearing history
git rebase [branch]
Apply any commits of current branch ahead of specified one.
git reset --hard [commit]
Clear staging area, rewrite working tree from specified commit.
TRACKING PATH CHANGES: Versioning file removes and path changes
git rm [file]
Delete the file from project and stage the removal for commit.
git mv [existing-path] [new-path]
Change an existing file path and stage the move.
git log --stat -M
Show all commit logs with indication of any paths that moved.
What is Cherry-Picking in Git?
Cherry-Picking means taking a commit from one branch and applying it to another. The git cherry-pick
command is useful for undoing changes.
For example, if a commit is accidentally made to the wrong branch, you can switch to the correct branch and use cherry-pick to move the commit to the right place.
git cherry-pick <hash_id>
Conclusion:
Mastering Git commands is essential for efficient version control and collaboration in software development. By understanding and utilizing commands for setup, initialization, staging, branching, merging, sharing, updating, rewriting history, and tracking path changes, developers can streamline their workflow and maintain a well-organized codebase. Additionally, advanced techniques like cherry-picking allow for precise control over commit history, ensuring that changes are accurately applied across branches. With these skills, developers can confidently manage their projects and contribute effectively to team efforts.