Main menu

Pages


 

How to Use Git The Comprehensive Guide in 5 Minutes


How to Use Git The Comprehensive Guide in 5 Minutes

How to Use Git The Comprehensive Guide , git


Git is a program for tracking changes in any set of files, usually used to coordinate work between programmers who collaboratively develop source code during software development.

 Its goals include speed, data integrity, and support for distributed workflows. Git is free and open-source software distributed under the GPL-2.0-only license.

Git was originally created by Linus Torvalds in 2005 to develop the Linux kernel, with other kernel developers contributing to its initial development. Since 2005, Junio ​​Hamano has been the primary supervisor. 

As with most other distributed version control systems, and unlike most server and client systems, every directory on a computer is a complete repository with history and full version tracking capabilities, regardless of network access or central server.

1. Set up and configure Git

First, you have to install the appropriate version for your device from the following link Download the Git tool and then check your Git version using the following command, which will also confirm that it is installed:


$ git --version


Git allows you to configure several settings that will be applied to all repositories on your local machine. For example, configure a username that Git will use to add you for any changes you make to the local repository:


$ git config --global user.name “first name Lastname”


Configure an email address to be associated with each record tag:

$ git config --global user.email “valid-email”


Configure your favorite text editor as well:

$ git config --global core.editor “nano”

You can configure the current working directory as a git repository using init:

$ git init


To copy an existing git repository hosted remotely, you will use git clone with the URL of the repository or server location:

$ git clone https://www.github.com/username/repo-name


Show the remote repository of the current Git directory:

$ git remote


For more verbose output, use the -v flag:

$ git remote -v

Add Git upstream, which can be a URL or can be hosted on a server (in the latter case, connect to ssh):

$ git remote add upstream https://www.github.com/username/repo-name


2. Starting

When you modify a file and mark it to save the new modifications, it is considered a staging file.


Check the status of your Git repository, including added unpartitioned and phased files:

$ git status


To organize the modified files, use add which you can run multiple times before executing. If you make subsequent changes that you want to include, you have to run add again.

You can specify a specific file using add:

$ git add my_script.py


using. You can add all files in the current directory, including files starting with. :

$ git add.


If you want to add all files in the current directory as well as files in subdirectories, you can use them -all or -A flag:

$ git add -A


You can remove a file from staging but keep the changes within your working directory with reset:

$ git reset my_script.py

3. Record changes

Once you have your updates set up, you are ready to implement them, which will record the changes you made to the repository.


To do a staging file execution, you'll run commit so that you can track changes:

$ git commit -m "Commit message"


You can condense splitting all tracked files by committing them in one step:

$ git commit -am "Commit message"


If you need to modify your commit message, you can do so by –amending:

$ git commit --amend -m "New commit message"


4. Branches

A branch is a moving pointer of a commit in a repository, it allows you to separate work and manage feature development and integration.


A list of all current branches An asterisk (*) will appear next to the currently active branch:

$ git branch


Create a new branch. You will remain in your currently active branch until you switch to the new branch:

$ git branch new-branch


Switch to any existing branch and check it in your current working directory:

$ git checkout another-branch


You can merge the build and pull operation of a new branch with -b:

$ git checkout -b new-branch


Rename your branch name:

$ git branch -m current-branch-name new-branch-name


Merge the history of the selected branch into the one you are currently working in:

$ git merge branch-name


Ignore the merge if there are conflicts:

$ git merge --abort


You can also specify a specific commit to combine with the cherry-pick string that points to the given commit:

$ git cherry-pick f7649d0


When you merge a branch and you no longer need it, you can delete it:

$ git branch -d branch-name


If you haven't merged a branch into main, but are sure you want to delete it, you can force delete the branch:

$ git branch -D branch-name

5. Collaborate and update

To download changes from another repository, such as the remote repository, you would use fetch:

$ git fetch upstream


Consolidate the commitments that have been brought in. Note that some repositories may use master instead of main:

$ git merge upstream/main


Push or move commits of the local branch to the remote repository branch:

$ git push origin main


Fetch and merge any commits from the remote trace branch:

$ git pull

6. View change history

View the commit history of the currently active branch:

$ git log


Show commits that changed a specific file. This file follows regardless of file renaming:

$ git log --follow my_script.py


Show commits on one branch but not the other. This will show the commits of the a-branch that were not in the b-branch:

$ git log a-branch..b-branch


Looking at the reflog to see the last time branches and other references within the repository were updated:

$ git reflog


Display any object in Git via commit or hash string in a more readable format:

$ git show de754f5

7. Show changes

git diff shows changes between commits and branches.


Compare the modified files found in the staging area:

$ git diff --staged


Show the difference between what is in a-branch, but is not in b-branch:

$ git diff a-branch..b-branch


Show the difference between two specific commits:

$ git diff 61ce3e6..e221d9c


Keep track of path changes by deleting a file from your project and doing this remove to commit:

$ git rm file


Or change the path of an existing file and then organize the transfer:

$ git mv existing-path new-path


Check the execution log to see if any paths have been moved:

$ git log --stat -M

8. Save work locally only

Sometimes you'll find that you've made changes to some code, but before you're done, you have to start working on something else. 

You're not quite ready to implement the changes you've made yet, but you don't want to lose your work. The git stash command will allow you to save your local modifications and return to the working directory that aligns with the most recent commit.


Stash your current job:

$ git stash


See what you currently have stored:

$ git stash list


Your caches will be named stash@{0}, stash@{1} and so on.


Show information about a specific cache:

$ git stash show stash@{0}


To get the files in the current cache out of the repository while keeping the repository, use apply:

$ git stash apply stash@{0}

If you want to get files out of the cache, and you no longer need the cache, use pop:

$ git stash pop stash@{0}


If you no longer need files saved in a particular cache, you can drop the storage:

$ git stash drop stash@{0}


If you have many caches saved and you no longer need to use any of them, you can use clear to remove them:

$ git stash clear


9. Ignore files

If you want to keep the files in your local git directory, but don't want to commit them to the project, you can add these files to your gitignore. So as not to cause conflicts.


Use a text editor like nano to add files to gitignore. file:

$ nano .gitignore


10. Re-establishment

Changing the rule allows us to move branches by changing the commit on which they are based. With a re-establishment, you can squash commitments or reformulate them.


You can initiate a base address change either by calling the number of deposits you've made that you want to re-set:

$ git rebase -I HEAD~5


Alternatively, you can return the address based on a specific execution string or hash:

$ git rebase -i 074a4e5


Once you've crushed or refactored commits, you can complete refactoring your branch to the latest version of the project's upstream code.

 Note that some repositories may use master instead of main:

$ git rebase upstream/main

11. Undo and reset

You can undo changes you made to a specific commit using revert. Your working tree must be clean for this to happen:

$ git revert 1fc6665


Sometimes, including after a base change, you need to reset your working tree.


You can reset to a specific commit, and delete all changes, using the following command:

$ git reset --hard 1fc6665

To force push the last known non-conflicting commit to the origin repository, you'll need to use -force:


Warning: forcing it to the master branch (sometimes master) is often frowned upon unless there is a really important reason to do so.

 Use it in moderation when working in your own repositories, and work to avoid it when you collaborate.


$ git push --force origin main


To remove untracked local files and subdirectories from the git directory of a clean working branch, you can use git clean:

$ git clean -f -d


If you need to modify your local repository so that it looks like the current master branch (that is, there are a lot of conflicts), you can do a hard reset:


Note: Executing this command will make your local repository look exactly like the upstream. Any commitments you have made but not withdrawn upstream will be destroyed.


$ git reset --hard upstream/main



Comments