[tutorial] Git Survival Kit pt.2: Diggin’ deeper

Welcome to the next post in our Git Survival Kit tutorial series! In this one, I will introduce you to some more advanced topics, such as remotes, branching and undoing your actions. If you haven’t already,  you are encouraged to read our Covering the basics post first. When you’re feeling ready, just dig in.

Undo to the rescue!


 

Git offers a lot of ways to undo changes that you’ve made. If you commit something and then want to add some extra changes to that commit, use:

git commit --amend
$ git commit -m “First commit”
[master ddda2721] First commit
1 file changed, 2 insertions(+)

$ git add changedFile

$ git commit –amend
[master 24d1421b] First commit
Date: Thu Nov 8 13:19:31 2018 +0100
1 file changed, 3 insertions(+)
Note: The –amend option replaces the old commit with the new one.

To unstage a file, use:

git reset HEAD <file>

If you want to revert a modified file to its previous state, use:

git checkout -- <file>


Be careful with the above command – any changes you made to that file are irretrievably gone.

Removing files


 

If you remove a file from your working directory, it won’t dissapear from Git. What’s more, that file shows up as unstaged if you run git status:

$ git status
On branch master

Changes not staged for commit:
(use “git add/rm <file>…” to update what will be committed)
(use “git checkout — <file>…” to discard changes in working directory)

deleted: memeGenerator.cpp

no changes added to commit (use “git add” and/or “git commit -a”)

To avoid that, remove a file both from Git and your working directory with:

git rm <file>

When you commit, Git removes the file and doesn’t track it anymore. If you have to remove a file that is already staged, use the -f option.

To stop tracking a file, use the –cached option:

git rm --cached <file>

Remote action


 

To collaborate on a Git project successfully, you need to deal with remote repositories. Remotes represent versions of projects hosted on the network.

To display configured remote repositories, use:

git remote

If you use the -v option, it will show the corresponding URLs for the remote.

git remote -v
 
$ git remote -v
origin https://github.com/AMBES/importantProject.git (fetch)
origin https://github.com/AMBES/importantProject.git (push)

To add a remote repository, use:

git remote add <shortname> <url> 

where <shortname> denotes the remote name which will be used. Default shortname when you clone a project is origin.

 

To download the newest changes on the remote repository, use:

git fetch <remote>
$ git fetch origin
remote: Counting objects: 19, done.
remote: Compressing objects: 100% (19/19), done.
remote: Total 19 (delta 15), reused 0 (delta 0)
Unpacking objects: 100% (19/19), done.
From https://github.com/AMBES/importantProject
2ba9e4d..ce77ae3 master -> origin/master


Fetching only downloads data to your local repository, it doesn’t merge it with your work.

When you make some changes and want to share it with your team, use:

git push <remote> <branch>

which enables other people to fetch your data from the remote.

 

To display more information about a particular remote, use:

git remote show <remote>

If you want to rename a remote, you can use:

git remote rename <oldName> <newName>

and to remove a remote, use:

git remote rm <remote>

Working with branches


 

Branching in Git means separating the lines of development so you don’t mess up the stable code. A branch is nothing more than a movable pointer to one of the commits. With every commit, the pointer moves forward.

To create a new branch, use:

git branch <branch>

This creates a pointer to the commit you’re currently on.

 

If you want to switch between existing branches, use:

git checkout <branch>

In Git, there is a special pointer called HEAD which points to the branch you’re currently on. The checkout command instructs HEAD to point to a specified branch.

$ git checkout newBranch

There is an even simpler way to create a branch and switch to it at the same time. Just use the -b option with the checkout command:

git checkout -b <branch>

To display all branches, run:

git branch
$ git branch
* topicBranch
master


The * character indicates the branch you’re currently on.

If you run git branch with the -v option, it also displays the last commit on each branch.

$ git branch -v
* topicBranch f7d0b57e First commit
master f7d0b57e First commit

To delete a certain branch, use the -d option:

git branch -d <branch>

And that’s it for this second part of our Git Survival Kit series. You should now have a solid understanding of some Git concepts that you’ll be using a lot throughout your work. But, we still have to cover some important topics like merging and rebasing. Stay tuned for the third part!

Marko Jovic

Post a Comment

Comment
Name
Email
Website