DEV Community

RaftLabs - AI App Dev Agency
RaftLabs - AI App Dev Agency

Posted on • Originally published at raftlabs.co

26 Must-Know Git Commands in 2021

image

Git is a free and open-source distributed version control system designed to handle small and substantial projects with speed and efficiency. It's a system that tracks changes to our project files over time. It enables us to record project changes and go back to a specific version of the tracked files at any given point in time. This change history lives on your local machine and lets you revert to a previous version of your project with ease in case something goes wrong. Git makes collaboration easy. Knowing how to use Git is one of the essential skills for any developer nowadays - and it's a great addition to your resume!
Here we will look at some of the git commands to get you started with git.

1.Check your Git configuration.
The command below returns a list of information about your git configuration, including user name and email.

git config –l
Enter fullscreen mode Exit fullscreen mode

2.Setup your Git username

git config - global user.name "your-name"
Enter fullscreen mode Exit fullscreen mode

3.Setup your Git user email

git config - global user.email "your-email"
Enter fullscreen mode Exit fullscreen mode

4.Cache your login credentials in Git
You can store login credentials in the cache, so you don't have to type them in each time.

git config - global credential.helper cache
Enter fullscreen mode Exit fullscreen mode

5.Initialize a Git repo
The first step to creating your git repository is initializing it. You can initialize a git repo for a new or existing project using the following command:

git init
Enter fullscreen mode Exit fullscreen mode

6.Add a file to the staging area
The next step is to add the files in the project to the staging area. You can add a file using the following command:

git add <filename>
Enter fullscreen mode Exit fullscreen mode

To add all the files in the current directory, use the following command:

git add .
Enter fullscreen mode Exit fullscreen mode

7.Check a repository's status
This command will show the status of the current repository, including staged, unstaged, and untracked files.

git status
Enter fullscreen mode Exit fullscreen mode

8.Commit changes in the editor
The standard way to commit changes made to your repo is with the –m option, which lets you specify a summary for your commit message.

git commit -m "your commit message here."
Enter fullscreen mode Exit fullscreen mode

You can add and commit files in one step using the following command:

git commit -a –m "your commit message here."
Enter fullscreen mode Exit fullscreen mode

9.See your commit history in Git
To see your commit history for the current repo, use the following command:

git log
Enter fullscreen mode Exit fullscreen mode

To see your commit history, including all the files and their changes, type:

git log –p
Enter fullscreen mode Exit fullscreen mode

10.Remove tracked files from the current working tree in Git

git rm filename
Enter fullscreen mode Exit fullscreen mode

11.Rename files in Git

git mv [old file] [new file]
Enter fullscreen mode Exit fullscreen mode

12.Ignore files in Git

Create a .gitignore file and commit it.
Enter fullscreen mode Exit fullscreen mode

13.Rolling back commits
Use the below command to roll back the last commit:

git revert HEAD
Enter fullscreen mode Exit fullscreen mode

To revert an old commit in git:

git revert [commit_id_here]
Enter fullscreen mode Exit fullscreen mode

14.Create a new branch in git
Branch allows teams to work on the same code base in parallel. If you want to add some functionality to a branch without changing the code, you can create a branch, and if your team likes the changes, you can merge the two branches. To create a new branch, use the command below:

git branch [branch_name]
Enter fullscreen mode Exit fullscreen mode

15.Switch to a newly created branch

git checkout [branch_name]
Enter fullscreen mode Exit fullscreen mode

16.Delete a branch

git branch –d [branch_name]
Enter fullscreen mode Exit fullscreen mode

17.Merge two branches in git

git merge [branch_name]
Enter fullscreen mode Exit fullscreen mode

18.Show the commit log as a graph

git log –graph –online
Enter fullscreen mode Exit fullscreen mode

19.Add a remote repository

git add remote [https://repo_here]
Enter fullscreen mode Exit fullscreen mode

20.See all remote repositories

git remote –v
Enter fullscreen mode Exit fullscreen mode

21.Push changes to a remote repo
When all your work is ready to be saved on a remote repository, you can push all changes using the command below:

git push
Enter fullscreen mode Exit fullscreen mode

22.Pull changes from a remote repo
If other team members are working on your repository, you can retrieve the latest changes made to the remote repository with the command below:

git pull
Enter fullscreen mode Exit fullscreen mode

23.Fetch remote repo changes
This command will download the changes from a remote repo but not perform a merge on your local branch (as git pull does that instead).

git fetch
Enter fullscreen mode Exit fullscreen mode

24.Merge a remote repo with your local repo
If the remote repository has changes you want to merge with your local, then use the following command

git merge origin/main
Enter fullscreen mode Exit fullscreen mode

25.Push a new branch to a remote repo

git push -u origin branch_name
Enter fullscreen mode Exit fullscreen mode

26.Clone a git repository
The git clone command is used to download the source code from a remote repository. When you clone a repo, the code is automatically downloaded to your local machine. To clone a git repo, use the following command:

git clone <https://url-of-the-repository>
Enter fullscreen mode Exit fullscreen mode

Originally posted at raftlabs.co

Top comments (0)