Git Commands for Beginners and Intermediate
Git is a significant and helpful skill that is required in many organizations. While hunting software developer opportunities, you’ll discover that understanding Git is nearly as important as knowing a real programming language. All things considered, many people still forget to discover how git works and which commands are responsible for which activity.
This article will guide you through the basic git commands that you can view and use if you just forgot what to type on the command line or if you need to refresh your memory. I also want to go over some basic terms that every developer should know to understand the git-flow
Let’s get started 🕺🏼
Some basic terms that will help you understand the Git commands.
- Repository: stores all your projects, as well as commits and your project branches.
- Branch: is a copy of the repository with the specific version. The main branch in Git is Master.
- Commit: can be taken as single storage of changes in a particular branch.
- Checkout: is the process of switching between the current branch and the branch specified in the command.
- Master: is the main branch of the repository.
- Merge: is an action that adds changes from one branch to another.
- Fork: simply means a copy of the repository.
- Head: is the latest commit of the repository you are working with.
Basic Git Commands You Should Know
Git config commands
git config --global user.email [user_email]
git config --global user.name [user_name]
The commands above are used to set current user email and name configuration.
git config --global --edit
And this command is very useful, as it allows for editing user configuration in a text editor.
If you don’t have Git installed, follow this guide “How to setup Git on your PC for Multiple GitHub Accounts.” Now, Let’s look at some of the most used basic commands.
git init | git init [folder]
Git init is used to initialize an empty repository from the folder you are currently using with this command or via the project folder path. Both ways, you are correct. It is used when starting a new project or when you want to initialize the git repo within the existing project.
git clone [repo URL] [folder]
Git clone is used to copy the existing repository to the specified folder on your computer. Git clone can only be used with the repo URL as a parameter. The repository is then copied to the folder from which you used the command. If you want to copy the repository to another location on your computer, add a folder path as the second parameter.
git add [directory | file]
Git add stage any changes in the directory or in the file and it depends on what you add as a parameter. In most cases, the git commit and git push commands follow.
git commit -m "[message]"
This command is used to commit all deployed changes with the custom message passed as a string. By changing the parameter -m to -am, changes can be added and committed immediately.
This is the command that pushes the changes to the source branch.
Git status is used to check the status of the changed files and to indicate which files are deployed, not specified, and not tracked.
Git log is used to display the history of the commit in the default format
Git diff shows any unavailable differences between the index and the current directory. This command can be used with -staged to show differences between staging files and the latest versions. Another option is to use the file name command to show the differences between the file and the last commit.
Git Pull is used to get changes from the original branch, and it merges the changes into the local branch. This is a very important command in the Git dictionary. If you are working on a project and working in a team, it is recommended that you use Git pull before doing anything on your own to avoid merge conflicts when you are done writing your code and about to push what you have written into the project repository
Also, when you encounter the “refusing to merge merge unrelated histories error” which usually occur when you are trying to push your most recent changes, the git pull command below is the solution
git pull origin master –allow-unrelated-histories
This command gets the most recent changes from the source branch but does not merge them.
This command displays the list of all branches in the repository. It can also create a non-existing branch if you add a branch name as a parameter.
git branch -d [branchname]
Using -d flag will delete the branch with the specified branch name.
git checkout [branchname]
This command switch to the branch named [branchnamed]. If you add -b flag before branch name, it will checkout to a new branch, which will be created automatically.
git merge [branchname]
It merges the branch with the specified branch name to the current branch.
Commands for Undoing Git Changes
git revert [commit]
This command creates a new commit that undoes changes made in the specified commit and applies it to the current branch.
git reset [filename]
You can think of this as a -rollback , It points your local environment back to previous changes.
Advanced Git Commands
Navigate to the previous branch
git checkout [prevBranch]
and create / navigate to a new branch
git checkout -b [newBranch]
Git the history
Logs commit messages
Here all commits that contain ‘homepage’
git log --all --grep='homepage'
Retrieve all commits by author
git log —author="authorname"
Clean Up Your Repo
git fetch origin
git checkout master
git reset –hard origin/master
Get the difference between your branch and master branch
git diff master..my-branch
Edit the last commit message
git commit --amend -m "A better message"
Add something to the last commit without writing message again
git add. && git commit --amend --no-edit
Empty commit – can be useful to re-trigger CI build…
git commit --allow-empty -m "chore: re-trigger build"
How to Move changed files to another branch
Have you ever written so many lines of codes and made a lot of significant changes to your code, only for you to realize that you are not in the proper branch to check in your changes?
git stash is your plug! 😃
So If you have not made the commit yet, you can run git stash. This will save away all of your changes.
Then switch to the branch you want the changes on and run
git stash pop
git checkout correct-branch
git checkout -b new-branch
git stash pop
In a nutshell,
git stash pop takes a stashed change, removes it from the “stash” stack in your previous working, and applies it to your current working tree.
I’ve talked about the most basic and advanced git commands to keep them in one place and make it easier for you to review and use. I’ve also listed some terms that can help you understand what is going on behind some commands.