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
2.Setup your Git username
git config - global user.name "your-name"
3.Setup your Git user email
git config - global user.email "your-email"
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
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
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>
To add all the files in the current directory, use the following command:
git add .
7.Check a repository's status
This command will show the status of the current repository, including staged, unstaged, and untracked files.
git status
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."
You can add and commit files in one step using the following command:
git commit -a –m "your commit message here."
9.See your commit history in Git
To see your commit history for the current repo, use the following command:
git log
To see your commit history, including all the files and their changes, type:
git log –p
10.Remove tracked files from the current working tree in Git
git rm filename
11.Rename files in Git
git mv [old file] [new file]
12.Ignore files in Git
Create a .gitignore file and commit it.
13.Rolling back commits
Use the below command to roll back the last commit:
git revert HEAD
To revert an old commit in git:
git revert [commit_id_here]
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]
15.Switch to a newly created branch
git checkout [branch_name]
16.Delete a branch
git branch –d [branch_name]
17.Merge two branches in git
git merge [branch_name]
18.Show the commit log as a graph
git log –graph –online
19.Add a remote repository
git add remote [https://repo_here]
20.See all remote repositories
git remote –v
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
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
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
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
25.Push a new branch to a remote repo
git push -u origin branch_name
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>
Originally posted at raftlabs.co
Top comments (0)