DEV Community

Cover image for Git Commands Cheatsheet: Advanced (20+ Git Commands Advanced )
aashiya123
aashiya123

Posted on

Git Commands Cheatsheet: Advanced (20+ Git Commands Advanced )

If you are working on a project, whether small or large-scale, you need to track every change made within your team space. You can use any software or applications to keep that track, but we will discuss what Git is, how it works, and some advanced Git commands.

What is Git?
Git is an open-source Version Control System that allows you to work within teams in a unique way. You can use a number of commands and functions to work on the project and keep track of the code changes. It will manage all the changed versions of files and directories. As the project expands, the number of tests increases results in changing code with a version number that can be recovered later. It allows the developers and other project members to keep track of- what changes are made, who made those changes, when the changes were made, and why those changes were required.

Git is a distributed version control system commonly used for commercial software development and complete access to every file, directory, and iteration of the project. With Git, you do not have to be connected to the central repository due to which developers can collaborate with the team from anywhere. The main advantage of using Git is its branching capabilities, which are easy to merge.

Working of Git
Git stores everything like files and directories in binary large object forms, which are represented as trees. These trees can contain other trees or large objects along with text files containing information about those files. If a repository transfer contains many files with the same name but different content, it will also get transferred as large objects, which later get expanded to separate files.

Commit object contains the project’s complete history; any change to the project has to be committed. This commit object file will contain the information about that change.

Git Advanced commands

  • How to configure the tooling commands
    The below command will set the name of your commit transactions.
    $ git config –global user.name “[name]”
    The below command will set the email to your commit transactions.
    $ git config –global user.email “[email address]”
    How to create Repositories
    The below command will create a new repository with a specified name.
    $ git init [project-name]
    The below command will allow you to download a project and its complete version.
    $ git clone [url]

  • How to refactor the file name
    The below command will delete the file from the working directory and stages the deletion.
    $ git rm [file]
    The below command will allow you to delta the file from the version control but saves it locally.
    $ $ git rm –cached [file]
    The below command will change the file name and prepare it for commit.
    $ git mv [file-original] [file-renamed]

  • How to suppress the tracking
    The below command will list all the ignored files within the project.
    $ git ls-files –others –ignored –exclude-standard
    How to shelve and restore incomplete changes
    The below command will temporarily store all the modified tracked files.
    $ git stash
    The below command will restore the most recently stashed file.
    $ git stash pop
    The below command will list all the stashed changesets.
    $ git stash list
    The below command will discard the most recently stashed changeset.
    $ git stash drop
    Commands to erase mistakes
    The below command will undo all the commits but preserve the changes locally.
    $ git reset [commit]
    The below command will discard all the history and changes back to the specific commit.
    $ git reset –hard [commit]
    Commands for group changes
    The below command will display all the local branches in the current repository.
    $ git branch
    The below command will allow you to create a new branch.
    $ git branch [branch-name]
    The below command will delete the specified branch.
    $ git branch -d [branch-name]

  • Miscellaneous commands
    The below command will allow you to connect your local repository to the remote server.
    $ git remote add [variable name] [Remote Server Link]

    To Split a subfolder out in a new repository.
    $ git filter-branch --prune-empty --subdirectory-filter master
    To remove untracked files
    $ git clean -f

    To remove untracked files/directories
    $ git clean -fd

    list all files/directories that would be removed
    $ git clean -nfd

    The below command will tar the project files without .git directory.
    $ tar cJf .tar.xz / --exclude-vcs

Top comments (18)

Collapse
 
gjorgivarelov profile image
gjorgivarelov

git init and git clone and ...git config are advanced?! Well, what's the beginner level then?

Collapse
 
dkassen profile image
Daniel Kassen

git help

Collapse
 
aashiya123 profile image
aashiya123

Will be coming with those commands also..i mentioned them as per the usage perspective

Collapse
 
aashiya123 profile image
aashiya123
Collapse
 
morphzg profile image
MorphZG

Helpful post, would be better with proper formating. Thanks

Collapse
 
aashiya123 profile image
aashiya123

Sure..will take care next time..

Collapse
 
jnareb profile image
Jakub Narębski • Edited

About the last command: which tar and which version it requires for it to have --exclude-vcs, and why not simply use the git archive command?

Collapse
 
aashiya123 profile image
aashiya123

for using -exclude-vcs, tar 1.34: 6.4.
also, if you want to tar a folder without having the .git files then we use the "--exclude-vcs" instead of git archive. archive will help in archiving the git tracked files. If you want the archive to include the files tracked by git, but not the git repository itself or any generated or otherwise untracked file, then use git archive.

Hope it helps

Collapse
 
jnareb profile image
Jakub Narębski

Ah, so the choice between using tar and using git-archive is whether we want to include untracked files, like generated files, in the archive.

Thread Thread
 
aashiya123 profile image
aashiya123

right..

Collapse
 
andrewbaisden profile image
Andrew Baisden

There are always so many GIT commands to remember 😂

Collapse
 
saroj8455 profile image
Saroj Padhan

Thank you

Collapse
 
yassine_dotma profile image
web developer

Thanx

Collapse
 
joelwmulongo profile image
Joel Mulongo Wambulwa

Nice

Collapse
 
mustapha3341 profile image
arewa_coder

Nice article

Collapse
 
rajshiikhar1691999 profile image
Rajshiikhar1691999

I wanted to know what happens if I delete the local git repository...

Collapse
 
shivamjoker profile image
Shivam

Please change the title to git basics 🙏