Learning the Git commands can significantly increase your productivity. In this tutorial I'm going to show and explain to you some of the most common git commands, and commands you should probably know.
Let's begin 🚀
Configure Git
Before using Git, you will need to learn how to configure it on your machine. You can do that with the following commands:
# list all the configured values
git config --list
# Set username & email for git globally
git config --global user.name "Progressive Programmer"
git config --global user.email "[email protected]"
# Set committer name & email for git
git config --global committer.name "Programmer"
git config --global committer.email "[email protected]"
Create a new Repository
Next, you 'll need a way to create a new repository. You can always do this in most GUI applications, or you can choose to do this in the command line, like so:
# Create a new folder and initialize git repo
git init name
# Clone a remote repo in your local system
git clone url_of_repo
Add files/folders
Next, after adding files to that folder/project you'll want to add them all. This will tell git that you want to add these files to your local repository.
# Adds mentioned file/folder to the staging area
git add hello.py
# Adds all the files and folders in the current directory
git add .
# Opens the file and let you choose <br/>the portion to be added for next commit
git add -p hello.py
Commit Changes
Ok, now that we told our local repository which files we want to add, we then need to tell git that we want to commit these changes:
# Commit a snapshot of staged changes
git commit
# Commit a snapshot of changes in the working directory.
git commit -a
# Shortcut for commit with a commit message
git commit -m "commit message"
Push Changes
Finally, our new files have been added and our changes have been commited. The last thing we need to do is to push our changs:
# Push all files/folders that have been commited
git push
# Push files/folders commited to specific branch
git push origin master
Git Branching
When working with Git, you will probably be working with a team and you may need to have different branches for your prooject. You can think of a branch as a duplicating the project code and giving it a different name crazy_experiment
, this way you don't conflict with changes from other users. You can create and configure branches like so:
# Create a new branch
git branch crazy_experiment
# List all branches
git branch
# List both remote and local branches
git branch -a
# List only branches that match the pattern mentioned
git branch --list 'pattern here'
Switch | Delete | Rename Branch
You'll also need the ability to switch between branches, delete branches, or rename branches. You can do that with the following commands:
# Move to different branches
git checkout branch_name
git switch branch_name
# Shortcut to create a new branch and switch to the branch
git switch -c new_branch_name
git checkout -b new_branch_name
# Delete the given branch
git branch -d trash
# Force delete the given branch
git branch -D trash
# Rename the current branch
git branch -m new_name
Merge Branch
Whenever you're finished with your feature or code fix, you may want to merge your branch into the master or main branch. You can do that with the following commands:
# Merge given branch name with the working branch
git merge branch_name
# Continue merger after conflict resolution
git merge --continue
Check Status
Whenever you want to see if you have additional files that need to be added, you can run the following git status commands:
# Shows the working tree status
git status
# Shows status of working tree in a short format
git status --short
# Shows status of branch in a short format
git status --branch
Remote Repository
When you are working from your local machine, you are also working from a local
repository. The files that are stored via Github or Gitlab (on a server) is referred to as the remote repository, the following commands will allow you to perform various actions on your remote repository
# Lists remote repo name and url(fetch/push)
git remote -v
# Add a remote repository with local repository
git remote add origin url_remote_repo
# Remove the remote repo with given name
git remote remove repo_name
# Rename the remote repo with given name
git remote rename old_name new_name
# Updates remote repo with local repo
git push
I hope this helps !! 🚀🚀 Follow me on GitHub @Progressive-Programmer
Comments (0)