Git is an essential tool for developers, but it can be complicated to use. Git commands are crucial for tracking changes, collaborating with team members, and maintaining clear and concise project history. With Git, you can enjoy a smooth coding experience.
In this guide, I will take you through the essential Git commands that you need to know. Additionally, I’ll introduce some other commands that can help you keep your codebase under control, particularly when things don’t go as planned. These commands include git revert, which can be a lifesaver, git stash, which is a handy tool, and git clean, which helps with housekeeping. By the end of this guide, you will have the necessary tools to handle your daily coding tasks effectively and efficiently. So, let’s begin and turn you into a Git command wizard, one command at a time.
Starting with the basics: Init, Add, Commit, revert, stash, and clean
Before we dive into the complexities, let’s establish the foundation.
1. Initializing your repository: git init
General Syntax: git init [repository name]
This command is your first step in the Git world. It initializes a new Git repository and begins tracking an existing directory. It adds a hidden subfolder within the existing directory that houses the internal data structure required for version control.
Output:
$ git init my-new-repo
Initialized empty Git repository in /Users/yourusername/my-new-repo/.git/
2. Staging changes: git add
General Syntax: git add <file>
or git add .
Once you’ve made changes to your files, git add
stages them, which means it marks the files for the next commit. I personally prefer using git add .
to stage all changes at once, but you can also add individual files.
Output:
$ git add . $ git status On branch master Changes to be committed: (use "git reset HEAD <file>..." to unstage) new file: index.html modified: styles.css
3. Committing your changes: git commit
General Syntax: git commit -m "Your message"
This command takes your staged changes and commits them to the repository history. Always include a meaningful message with -m
to remember the purpose of the change.
Output:
$ git commit -m "Initial commit with index and styles"
[master (root-commit) 0a1b2c3] Initial commit with index and styles
2 files changed, 52 insertions(+)
create mode 100644 index.html
create mode 100644 styles.css
4. Branching Out: git branch
and git checkout
Branching is where things get interesting. It allows you to diverge from the main line of development and work independently.
4.1 Creating branches: git branch
General Syntax: git branch [branch-name]
A branch is essentially a pointer to a particular commit. The default branch name in Git is master
.
Output:
$ git branch feature-x
$ git branch
feature-x
* master
4.2 Switching branches: git checkout
General Syntax: git checkout [branch-name]
Switch to another branch to work on with git checkout
. It updates files in the working directory to match the version stored in that branch.
Output:
$ git checkout feature-x
Switched to branch 'feature-x'
5. Merging and resolving conflicts: git merge
When you’re done working on a branch and everything looks good, you’ll want to merge those changes back to your main branch.
Merging changes: git merge
General Syntax: git merge [branch-name]
Merging takes the changes from one branch and applies them to another.
Output:
$ git merge feature-x
Updating 34ac2e0..5813c0b
Fast-forward
index.html | 10 ++++++++++
1 file changed, 10 insertions(+)
6. Keeping in sync: git pull
and git push
To work with remote repositories, you’ll need to know how to push and pull data.
6.1 Pulling latest changes: git pull
General Syntax: git pull [remote] [branch]
This command fetches the changes from the remote repository and merges them into your local branch.
Output:
$ git pull origin master
From https://github.com/yourusername/your-repo
* branch master -> FETCH_HEAD
Already up to date.
6.2 Pushing your changes: git push
General Syntax: git push [remote] [branch]
After committing your changes locally, use git push
to upload your commit to a remote repository.
Output:
$ git push origin master
Counting objects: 9, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (5/5), done.
Writing objects: 100% (5/5), 452 bytes | 452.00 KiB/s, done.
Total 5 (delta 3), reused 0 (delta 0)
To https://github.com/yourusername/your-repo.git
1a2b3c4..5d6e7f8 master -> master
7. Finding your way: git status
and git log
Sometimes, you need to check the status or review the history of your repository.
7.1 Checking status: git status
General Syntax: git status
This command displays the state of the working directory and the staging area.
Output:
$ git status On branch master Your branch is up to date with 'origin/master'. nothing to commit, working tree clean
7.2 Viewing commit history: git log
General Syntax: git log
Use git log
to list the version history for the current branch.
Output:
$ git log commit 5d6e7f8defa897f8be47ab6e465d8a8fe0b8d34e (HEAD -> master, origin/master) Author: Your Name <your.email@example.com> Date: Mon Mar 7 21:52:11 2022 -0800 Add user authentication
8. Reverting changes: git revert
Sometimes, we make changes that we wish we hadn’t. That’s where git revert
becomes a lifesaver.
Undoing commits: git revert
General Syntax: git revert <commit>
This command creates a new commit that undoes all of the changes made in a specified commit, essentially “reverting” the repository to a previous state.
Output:
$ git revert 5d6e7f8
[master 00a1b2c] Revert "Add user authentication"
1 file changed, 1 deletion(-)
9. Stashing your work: git stash
Working on something but not quite ready to commit? git stash
is your friend.
Stashing your changes: git stash
General Syntax: git stash [save] [message]
This command temporarily shelves (or stashes) changes you’ve made to your working copy so you can work on something else, and then come back and re-apply them later on.
Output:
$ git stash save "Work in progress on feature Y"
Saved working directory and index state On master: Work in progress on feature Y
HEAD is now at 0a1b2c3 Initial commit
10. Cleaning your working directory: git clean
Untracked files cluttering your working directory? Let’s clean up.
Removing untracked files: git clean
General Syntax: git clean -n
or git clean -f
This command cleans the working directory by removing files that are not under version control.
Output:
$ git clean -n
Would remove untracked-file.txt
$ git clean -f
Removing untracked-file.txt
The -n
option tells Git to show what would be done, and -f
actually does the removal.
These three commands, git revert
, git stash
, and git clean
, are incredibly useful when managing changes and maintaining a tidy repository.
Conclusion
Overall, it is evident that Git’s power lies in its flexibility and comprehensive control over your development workflow. By mastering essential commands like git init, git add, git commit, and advanced capabilities like git revert, git stash, and git clean, you’re not just performing tasks, but sculpting your project’s history and ensuring its integrity. It’s important to remember that these commands are just the starting point. As you continue to explore and integrate them into your daily use, you’ll find that Git is an indispensable tool in your development arsenal. So keep practicing, stay curious, and let Git smoothly guide your journey through code versioning and collaboration.